Hello everyone!
I just started with the arduino and I have a big question.
I am building a 'light system'. With this light system you can insert text (serial-monitor).
So when I say: #light1on%, then the first light goes on and when I say #light2on% the second light goes on, etc.
There is one problem, I wanna have 'alarm'-button on my arduino. But it isnt working! And when it works, i cant reset him using a code. The alarm most go on when I push the button and when I say #alarmoff% it most go off so i can use my system again. Can someone help me? I put some code below!
Thanks!
int sensorPin = A0;
int sensorValue = 0;
int firstlight = 13;
int secondlight = 12;
int alarmlight = 6;
int incomingByte = 0;
String invoer;
int buttonState = 0;
int buttonPin = 5;
//Setup
void setup() {
Serial.begin(9600);
pinMode(firstlight, OUTPUT);
pinMode(secondlight, OUTPUT);
pinMode(alarmlight, OUTPUT);
}
//Read next message
char readNextCharacterFromSerial()
{
int value = -1;
do {
value = Serial.read();
}
while(value == -1);
return (char) value;
}
//Create loop
void loop() {
buttonState = digitalRead(buttonPin);
String a = "";
//Find # symbool
while (readNextCharacterFromSerial()!='#')
{
}
while (invoer.indexOf("%") == -1){
invoer += readNextCharacterFromSerial();
//Turn first led on/off using serial communication
if(invoer == "first-light-on%")
{
digitalWrite(firstlight, HIGH);
invoer = "";
Serial.flush();
readNextCharacterFromSerial();
}
if(invoer == "first-light-off%")
{
digitalWrite(firstlight, LOW);
invoer = "";
Serial.flush();
readNextCharacterFromSerial();
}
//Turn second led on/off using serial communication
if(invoer == "second-light-on%")
{
digitalWrite(secondlight, HIGH);
invoer = "";
Serial.flush();
readNextCharacterFromSerial();
}
if(invoer == "second-light-off%")
{
digitalWrite(secondlight, LOW);
invoer = "";
Serial.flush();
readNextCharacterFromSerial();
}
//THE ALARM
//Schakel alarm in / uit
if(buttonState == HIGH)
{
Serial.println("#Alarm ingeschakeld%");
digitalWrite(firstlight, HIGH);
digitalWrite(secondlight, HIGH);
do {
sensorValue = analogRead(sensorPin);
digitalWrite(alarmlight, HIGH);
delay(sensorValue);
digitalWrite(alarmlight, LOW);
delay(sensorValue);
} while(0 < 9); //forever going loop
}
}
}
lightsystem.ino (1.98 KB)