definitions and analog pins

so ive been completely perplexed today by a bunch of issues, and would appreciate some help on them please

all these observations ive seen in codes around, but will post the code in reference by the end of this post.

first of all, why do i find it in some codes that people define things in the #define CAPITAL_LETTERS, does arduino understand this format, and if i add the #define THING_PIN, does it understand it as a digital pin? and if i do it as #define THING_ANALOG_PIN it also understand that it is an analog one? i found nothing of this in the documentation. and one last thing, if i dont write the number of the pin, does it read from all of the pins and give a sum?

why would i use this kind of define vs the traditional int sth = something? my guess is that it is defined as a constant? but still, how does arduino read it exactly?

in the setup, i only have to define pins that are out? why? why dont i define the ins?

finally, all this wondering is because i added a piece of code to an old one i got off the internet to read some input from a potentiometer, but after adding that, the value of the soundThreshold is always incremented by the value of the potentiometer, even though there is no connection that way

here are the codes. btw, the code is to trigger a flash at a specific sound threshold coming from an amplifier, it closes the circuit in an optioscillator when the sound reaches the threshhold

there goes the codes

#define ENABLE_SOUND_TRIGGER

#define SOUND_THRESHHOLD 15

#define CAMERA_FLASH_PIN 4

#define SOUND_TRIGGER_ANALOG_PIN 1

void setup()
{
  pinMode(CAMERA_FLASH_PIN, OUTPUT);
  digitalWrite(CAMERA_FLASH_PIN, LOW);
  Serial.begin(9600); // open serial

}

void loop()
{
  int soundVal;

  soundVal = analogRead(SOUND_TRIGGER_ANALOG_PIN);
  if (soundVal > SOUND_THRESHHOLD)
  {
     digitalWrite(CAMERA_FLASH_PIN, HIGH);

//     Serial.println("yey");
     Serial.println(soundVal);
     delay(1000);

     digitalWrite(CAMERA_FLASH_PIN, LOW);
   }
}
#define ENABLE_SOUND_TRIGGER

//#define SOUND_THRESHHOLD 200

#define CAMERA_FLASH_PIN 4

#define SOUND_TRIGGER_ANALOG_PIN 1

#define DELAY_ANALOG_PIN 5

//int potPin = 5;    // select the input pin for the potentiometer

void setup()
{
  pinMode(CAMERA_FLASH_PIN, OUTPUT);
  digitalWrite(CAMERA_FLASH_PIN, LOW);
  Serial.begin(9600); // open serial

}

void loop()
{

  int deelay = analogRead(DELAY_ANALOG_PIN);    // read the value from the sensor
  int soundThreshhold = 125;
  int soundVal;

  soundVal = analogRead(SOUND_TRIGGER_ANALOG_PIN);
  if (soundVal > soundThreshhold)
  {
     delay(deelay);
     digitalWrite(CAMERA_FLASH_PIN, HIGH);

//     Serial.println("yey");
     Serial.println(soundVal);
     delay(1000);

     digitalWrite(CAMERA_FLASH_PIN, LOW);
   }
}

any ideas please?

thanks

The preprocessor doesn't care about upper or lowercase. It has no knowledge whatsoever of analogue or digital pins It is a simple text replacement macro processor, if it sees

define MY_PIN 3

Then anytime in the body of the code it sees MY_PIN, it replaces it with a 3, and passes the result to the compiler.

must reader - http://en.wikipedia.org/wiki/C_preprocessor -

Thanks a lot for the replies guys, will read the cpp tonight, gave it an overview already

but one question still lies, for example in this piece of code

void setup()
{
  pinMode(CAMERA_FLASH_PIN, OUTPUT);
  digitalWrite(CAMERA_FLASH_PIN, LOW);
  Serial.begin(9600); // open serial

}

how does it understand if it should communicate with the analog or digital pin?

on the other hand, the question of incrementing, why is arduino reading the wrong signal when i add the potentiometer, i mean i dont like to think its erratic behavior, because it is computing, what am i doing wrong in that added code?

thanks again people

how does it understand if it should communicate with the analog or digital pin?

Because you have this previously:

#define CAMERA_FLASH_PIN 4

Pin 4 is by default digital pin 4. If you want analog pin 4, either of these will work:

#define CAMERA_FLASH_PIN A4
#define CAMERA_FLASH_PIN 18

How did you connect the potmeter?
There must be one side to +5V one side to GND and the middle to the analog port like in - http://www.arduino.cc/en/Tutorial/Potentiometer - ?

what am i doing wrong in that added code?

determine the range of your potmeter first, it might be too “trigger happy”, if so the threshhold might need to be adapted.

#define SOUND_TRIGGER_ANALOG_PIN A1

void setup()
{
  Serial.begin(9600); // open serial
}

void loop()
{
  int soundVal = 0;
  for (int r=0; r<16; r++) soundVal += analogRead(SOUND_TRIGGER_ANALOG_PIN);
  soundVal/= 16;
  
  Serial.println(soundVal);
  delay(1000);
}

The reason to add the loop around reading the analog port is to cancel out noise.
By using a power of 2 (16) the division will be optimized by the compiler.

optionally you could use:
(soundVal+8)/= 16; // add a half unit for proper rounding

same trick of course for deelay

this is great stuff thanks again mate

a- i hooked it up correctly, tested it with a code that gave out the value in realtime, it was between 0-1024

my question is, since the board is trying to pick up the spikes that happen when loud noise comes up, wouldnt this averager function kill the core function?

another is, the reading value that shld be read by both codes should be the same, for the soundVal, so how come just because i added another parameter, that it is adding the value of it to soundVal??

ps. what about how does the compiler understand whether it is an analog or digital pin it is that i am talking about??

The compiler knows nothing of analogue or digital pins, just like the pre processor doesn't. The function you call to read the pin knows the difference though.