[sdiy] Arduino questions

Olav Martin Kvern okvern at ix.netcom.com
Fri May 15 08:38:34 CEST 2009


Hi Ian,

re: "1) how does the Arduino know the difference between say Analog pin 2 
and Digital pin 2 when it doesn't seem like there's a difference when 
calling them in the program."

It's context, really--the libraries take care of "knowing" which pin is 
which. If you use analogRead(0), it's going to read analog pin 0; if you use 
digitalWrite(0), it's going to use digital pin 0. There are ways to use the 
analog pins as digital pins, and ways to use some of the digital pins as PWM 
output pins, but it's not something you have to worry about at first. You 
don't have to declare analog pins, but you should declare digital pins as 
being input or output (or switch as you use them).

One thing to note: if you want to use attachInterrupt, you'll want to 
reserve digital pins 2 & 3 for that purpose (they are interrupt 0 and 1, 
respectively). Note that attachInterrupt is a huge feature for things like 
sequencers.

re: "2) Is it possible to run more than one void loop() in the same 
program."

No.

re: "// all at the same time and not one after the other?"

No. While you can run multiple functions from inside the loop, they run one 
after the other, *not* at the same time, with the exception of interrupt 
routines (which interrupt the current process, do something, and then return 
to that process). There are at least two finite state machine libraries 
available--search the playground and the forum and you'll find them. If you 
use these, you can *almost* make it seem like you have two processes going 
on at once (but  you don't). But using attachInterrupt will do most of the 
things that you'd want to use parallel processing for, in any case.

re: "int potpin = 17; // analog pot to be read for a value between 0-255 
(yup, divide by 4 down in the  loop)"

Don't bother. Use map() instead to translate from 10-bit to 8-bit (it's 
faster and easier):

value = map(analogRead(0), 0, 1023, 0, 255);

Thanks,

Ole 




More information about the Synth-diy mailing list