I am having some trouble getting my board to perform the function I want it to perform. This is a project I have due in a few days, and I have hit a wall I just can't seem to get past. Now, I will say the issue is definitely in the way I coded the program, but I don't really know enough about Arduino (and coding) to understand why it is not working. My board is properly hooked up to use 3LEDs, a speaker, a photoresistor, and an ultrasonic sensor (detects distances). My Arduino program takes serial input from a GUI that I created in MATLAB. I have verified the MATLAB code is good, and the serial port is communicating properly. I have made an alarm that goes off when light or motion is detected. It has 3 basic functions:
- Set the alarm
- Turn off the alarm
- "snooze" which just delays for 5 minutes and then the alarm goes off
The snooze and turn off alarm parts of my code are working great, but the set alarm is where I am having trouble. The snooze option sets off the yellow LED to verify it is on "snooze". The red LED comes on when the alarm is currently going off. The green LED comes on when the alarm is set. When I try to activate the "Set Alarm" function, which is case 'O' in a switch case, the light does come on, but the sensors are not picking up any input as far as I can tell. Again I have verified the pins and everything are set up correctly as I have isolated those and verified they work. I have a suspicion that the problem is because I have the sensors initialed within the switch case of the "Set Alarm" (case 'O', not 0). I don't really know how to get the sensors to run continually within that switch case until the "turn alarm off" case is activated. I need the sensors to be activated/reset by "Set Alarm" case.
**I am also new to this forum so please let me know if I am missing anything!
here is my code:
void setup()
{
Serial.begin(115200);
pinMode(12,OUTPUT); //RED
pinMode(13,OUTPUT); //GREEN
pinMode(14,OUTPUT); //YELLOW
pinMode(16,OUTPUT); //SPEAKER
pinMode(4,INPUT); //Echo
pinMode(2,OUTPUT); //Trig
}
void loop()
{
if(Serial.available() >0)
{
char b = Serial.read();
switch(b)
{
//Turn Alarm off
case 'A' :
{
digitalWrite(12,LOW);//red off
digitalWrite(13,LOW); // green off
digitalWrite(14,LOW); //yellow off
noTone(16);
delay(500);
}
break;
// Set alarm
case 'O' :
{
int light;
light = analogRead(0);
unsigned long distance;
digitalWrite(2,LOW);
delayMicroseconds(2);
digitalWrite(2,HIGH);
delayMicroseconds(10);
digitalWrite(2,LOW);
distance = pulseIn(4,HIGH);
distance = distance/58; //centimeters
delay(500);
digitalWrite(12,LOW); //red off
digitalWrite(14,LOW); //yellow off
digitalWrite(13,HIGH); //green on
if (light < 400)
{
digitalWrite(12,HIGH);//red on
digitalWrite(13,LOW); // green off
digitalWrite(14,LOW); //yellow off
tone(16,832);
delay(500);
}
if (distance < 50)
{
digitalWrite(12,HIGH);//red on
digitalWrite(13,LOW); // green off
digitalWrite(14,LOW); //yellow off
tone(16,832);
delay(500);
}
}
break;
// snooze option
case 'Z' :
{
digitalWrite(14,HIGH); //yellow on
digitalWrite(12,LOW); //red off
digitalWrite(13,LOW); // green off
noTone(16);
delay(300000UL);
digitalWrite(12,HIGH); //red on
digitalWrite(13,LOW); // green off
digitalWrite(14,LOW); //yellow off
tone(16,832);
delay(500);
}
break;
}
}
}
Any help would be very much appreciated!