How do I make this Arduino-code work correctly??

The following is my code for a setup, with my CANON Rebel and an amplified microphone into my arduino-board, as substitute for a remote-control, so that when the mic picks up a sound loud enough it triggers the camera to take a picture. If there is none such sound, the camera will take a pic every 3 sec(3000) anyway. However, when run for about 30 secs or approximately 10 shoots the serial monitor and the whole code will just go berserk. Any idea why this happens? ? ? ? Thanks!!!

THE CODE:

// these constants won't change: const int micPin = 0; // the piezo is connected to analog pin 0 const int threshold = 1; // threshold value to decide when the detected sound is a knock or not const int optoPin = 10; // the camera is connected to digital pin 10 const int shortest = 1000; // interval between pictures when the sound is lowest const int longest = 3000; // interval between pictures when the sound is highest

// these variables will change: int micRead = 0; // variable to store the value read from the sensor pin int ledState = LOW; // variable used to store the last LED status, to toggle the light

// variables for the shutter interval timer int shutterInterval = longest; // time between trigger / event int oldTime = 0; // variable to hold the old time

void setup() { pinMode(optoPin, OUTPUT); // sets the digital pin as output // Opto Serial.begin(9600); // use the serial port }

void loop() { micRead = analogRead(micPin); // read the sensor and store it in the variable micLevel: int shutterInterval = map(micRead, 0, 1023, longest, shortest); // Serial.println(shutterInterval); if(millis() - oldTime > shutterInterval){ oldTime = millis(); // define new oldTime //takePic(shutterInterval); // take picture Serial.println(micRead); } }

void takePic(int _printthis){ digitalWrite(optoPin, HIGH); // sets the OPTO on // Serial.println(_printthis); delay(300); digitalWrite(optoPin, LOW); // sets the OPTP to off }

oldTime = millis();  // define new oldTime

The oldTime variable is declared as an int. The millis function returns an unsigned long.

the serial monitor and the whole code will just go berserk

Can you explain how the code goes berserk? Can you explain how the serial monitor goes berserk?

We have the serial monitor going and in the beginning it returns values fine (each time the interval is reached), but after a certain amount of time, arduino prints values to the serial monitor constantly.

But it seemed to solve the problem to have it declared as a unsigned long, now we just have to fix a recurring problem with our mic :(

Is your code a direct cut and paste?

The reason I ask is because:

 [glow]//  [/glow]Serial.println(shutterInterval);

and:

[glow]//[/glow]takePic(shutterInterval);

and:

[glow]//[/glow] Serial.println(_printthis);

:-?

I assume you commented out these lines for troubleshooting.

It appears you declared shutterInterval an int twice; once globally and once locally. I can't remember if that's allowed, or not...

// these constants won't change:
const int micPin = 0;      // the piezo is connected to analog pin 0
const int threshold = 1;   // threshold value to decide when the detected sound is a knock or not
const int optoPin = 10;    // the camera is connected to digital pin 10 
const int shortest = 1000;     // interval between pictures when the sound is lowest
const int longest = 3000;    // interval between pictures when the sound is highest

// these variables will change:
int micRead = 0;      // variable to store the value read from the sensor pin
int ledState = LOW;         // variable used to store the last LED status, to toggle the light

// variables for the shutter interval timer
[glow]int shutterInterval [/glow]= longest; // time between trigger / event
int oldTime = 0; // variable to hold the old time

void setup() {
 pinMode(optoPin, OUTPUT); // sets the digital pin as output   // Opto
 Serial.begin(9600);       // use the serial port
}

void loop() {
 micRead = analogRead(micPin);     // read the sensor and store it in the variable micLevel:
 [glow]int shutterInterval [/glow]= map(micRead, 0, 1023, longest, shortest);
 //  Serial.println(shutterInterval);            
 if(millis() - oldTime > shutterInterval){
   oldTime = millis();  // define new oldTime
   //takePic(shutterInterval);   // take picture
   Serial.println(micRead);            
 }
}

void takePic(int _printthis){
 digitalWrite(optoPin, HIGH);    // sets the OPTO on
 // Serial.println(_printthis);
 delay(300);
 digitalWrite(optoPin, LOW);     // sets the OPTP to off   
}