Help me please!
hello everyone!
i started arduino , have big question.
i building 'light system'. light system can insert text (serial-monitor).
so when say: #light1on%, first light goes on , when #light2on% second light goes on, etc.
there 1 problem, wanna have 'alarm'-button on arduino. isnt working! , when works, cant reset him using code. alarm go on when push button , when #alarmoff% go off can use system again. can me? put 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;
{
value = serial.read();
}
while(value == -1);
return (char) value;
}
//create loop
void loop() {
buttonstate = digitalread(buttonpin);
string = "";
//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);
{
sensorvalue = analogread(sensorpin);
digitalwrite(alarmlight, high);
delay(sensorvalue);
digitalwrite(alarmlight, low);
delay(sensorvalue);
} while(0 < 9); //forever going loop
}
}
}
i started arduino , have big question.
i building 'light system'. light system can insert text (serial-monitor).
so when say: #light1on%, first light goes on , when #light2on% second light goes on, etc.
there 1 problem, wanna have 'alarm'-button on arduino. isnt working! , when works, cant reset him using code. alarm go on when push button , when #alarmoff% go off can use system again. can me? put 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;
{
value = serial.read();
}
while(value == -1);
return (char) value;
}
//create loop
void loop() {
buttonstate = digitalread(buttonpin);
string = "";
//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);
{
sensorvalue = analogread(sensorpin);
digitalwrite(alarmlight, high);
delay(sensorvalue);
digitalwrite(alarmlight, low);
delay(sensorvalue);
} while(0 < 9); //forever going loop
}
}
}
code: [select]
//read next message
char readnextcharacterfromserial()
{
int value = -1;
{
value = serial.read();
}
while(value == -1);
return (char) value;
}
this whole function mess. serial.available() function tells how data there data read.
code: [select]
while(serial.available() < 1) { /* nothing */ };
should go in place of do/while loop.
value should typed char, then, there no need cast.
of course, blocking until there data read poor idea. it's far better collect data arrives, storing until end of packet marker arrives ('%' in case).
code: [select]
{
sensorvalue = analogread(sensorpin);
digitalwrite(alarmlight, high);
delay(sensorvalue);
digitalwrite(alarmlight, low);
delay(sensorvalue);
} while(0 < 9); //forever going loop
don't crap if expect able disable alarm.
Arduino Forum > Using Arduino > Programming Questions > Help me please!
arduino
Comments
Post a Comment