Help me please!

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)

//Read next message
char readNextCharacterFromSerial()
{
  int value = -1;
  do {
    value = Serial.read();
  }
  while(value == -1);

  return (char) value;
}

This whole function is a mess. The Serial.available() function tells you how much data there is data to read.

while(Serial.available() < 1) { /* Do nothing */ };

should go in place of your do/while loop.

value should be typed char, then, so there is no need to cast.

Of course, blocking until there is data to read is a poor idea. It's far better to collect data as it arrives, storing it until the end of packet marker arrives ('%' in your case).

 do {
        sensorValue = analogRead(sensorPin);   
        digitalWrite(alarmlight, HIGH); 
        delay(sensorValue);             
        digitalWrite(alarmlight, LOW);   
        delay(sensorValue);     
      } while(0 < 9); //forever going loop

Don't do crap like this if you expect to be able to disable the alarm.

Look at the examples in serial input basics. They receive data without blocking.

The third example should meet your need.

...R

Welcome to the Forum. It will help you get more responses if you read the posting rules by Nick Gammon that appears at the top of this Forum. Following those rules makes it easier for us to help you.

this page is for the beginners on arduino
www.induimax.es

induimax:
this page is for the beginners on arduino
www.induimax.es

. . . who speak Spanish