i was wondering if its possible to ckeck if serialcommunication exists every time in the void loop before executing a set of statements. i used mills function and also wrote two diffrent functions inside the loop but not sucsessfull.
below i am posting code with diffrent functions. let me now if my way of code is wrong or should i use any library that makes the task easier.
i have already looked in to serial input basic examples in forum.
Thank you in advance for your suggestions and valuable time
Bw
David_24
// Global Variables
int RS1_IL = 3; // intializig names to Pins.
int RS2_KL30 = 4;
int RS3_KL30C = 5;
int RS4_KL31_Mio = 6;
int RS5_KL31_Extern = 7;
int interuptpin = 2;
char command; // a variable to hold the command from serial monitor.
char saftey;
int Relaya = 0; // Variables to follow the relay status.
int Relayb = 0;
int Relayc = 0;
int Relayd = 0;
int Relaye = 0;
void setup() {
//serialport begin
Serial.begin(9600);
Serial.println("<Arduino is ready>");
// set output pins
pinMode(RS1_IL, OUTPUT);
pinMode(RS2_KL30, OUTPUT);
pinMode(RS3_KL30C, OUTPUT);
pinMode(RS4_KL31_Mio, OUTPUT);
pinMode(RS5_KL31_Extern, OUTPUT);
pinMode(interuptpin, OUTPUT);
digitalWrite(interuptpin, LOW);
attachInterrupt(0, safetytrigger, HIGH); //seting up interupt
}
void loop()
{
safteycheck();
relay();
}
void safteycheck(){
//Have the arduino wait to receive input
while (Serial.available() == 0);
Serial.println("communication check: to proceed presses y");
// Read input
saftey = Serial.read();
if (saftey == 'y')
{
Serial.println("communication successfull");
delay(100);
}
else if (saftey == '\n' || saftey =='\r')
{
Serial.println("enter the command ");
}
else
{
Serial.println("Communication unsuccessfull: Reset Mode is on");
digitalWrite(2, HIGH);
delay(100);
}
}
void relay(){
while (Serial.available() == 0);
command = Serial.read();
switch (command)
{
case '1':
digitalWrite(RS1_IL, HIGH);
Serial.println("RS1_IL switch is open");
Relaya = Relaya + 1;
delay(100);
break;
case '0':
digitalWrite(RS1_IL, LOW);
Serial.println("RS1_IL switch is close");
Relaya = Relaya - 1;
delay(100);
break;
case '3':
digitalWrite(RS2_KL30, HIGH);
Serial.println("RS2_KL30 switch is open");
Relayb = Relayb + 1;
delay(100);
break;
case '2':
digitalWrite(RS2_KL30, LOW);
Serial.println("RS2_KL30 switch is close");
Relayb = Relayb - 1;
delay(100);
break;
case '5':
digitalWrite(RS3_KL30C, HIGH);
Serial.println("RS3_KL30C switch is open");
Relayc = Relayc + 1;
delay(100);
break;
case '4':
digitalWrite(RS3_KL30C, LOW);
Serial.println("RS3_KL30C switch is Close");
Relayc = Relayc - 1;
delay(100);
break;
case '7':
digitalWrite(RS4_KL31_Mio, HIGH);
Serial.println("RS4_KL31_Mio switch is open");
Relayd = Relayd + 1;
delay(100);
break;
case '6':
digitalWrite(RS4_KL31_Mio, LOW);
Serial.println("RS4_KL31_Mio switch is close");
Relayd = Relayd - 1;
delay(100);
break;
case '9':
digitalWrite(RS5_KL31_Extern, HIGH);
Serial.println("RS5_KL31_Extern switch is open");
Relaye = Relaye + 1;
delay(100);
break;
case '8':
digitalWrite(RS5_KL31_Extern, LOW);
Serial.println("RS5_KL31_Extern switch is close");
Relaye = Relaye - 1;
delay(100);
break;
case 'a':
Serial.println("saftey Mode- all relays are reseted");
digitalWrite(2, HIGH);
delay(100);
break;
default:
if ((Relaya > 1) || (Relayb > 1) || (Relayc > 1) || (Relayd > 1) || (Relaye > 1))
{
Serial.println("saftey mode: All relays are reseted ");
digitalWrite(2, HIGH);
delay(100);
}
else if (command == '\n' || command == '\r')
{
// Ignore line ending characters
}
else
{
Serial.println("Invalid command received");
}
}
}
void safetytrigger()
{
Relaya = 0;
Relayb = 0;
Relayc = 0;
Relayd = 0;
Relaye = 0;
digitalWrite(2, LOW);
digitalWrite(RS1_IL, LOW);
digitalWrite(RS2_KL30, LOW);
digitalWrite(RS3_KL30C, LOW);
digitalWrite(RS4_KL31_Mio, LOW);
digitalWrite(RS5_KL31_Extern, LOW);
}
after the saftey check function execution the relay function is not at all executing
@johnwasser
if i dont use the above code my serialprint command executes many times. my aim ist to print a command and ask the user to typ y for relay function to be executed for each time in the void loop. if no response from the user for a minute then go to reset mode. the time can be programmed using millis() but i am not able to find way to check the user reply in my sketch the safteycheck function. may be i should use so libraries.
So, the user must send a 'y', and then they have one minute to send an 'a' or '0' through '9' to switch relays. If nothing has been entered in 1 minute after the 'y', do a reset?
If the user sends a 'y' and a relay command, do they have to send another 'y' before another command even if the minute has not expired?
If the user sends a 'y' and, before the minute expires, sends another 'y' is the second 'y' ignored or is the time set back to 1 minute?
So, the user must send a 'y', and then they have one minute to send an 'a' or '0' through '9' to switch relays. If nothing has been entered in 1 minute after the 'y', do a reset?
exactly this is what i want the programm to do. ask every 1 minute for 'y' from the user to check if some one is operating or if the comunication has not terminated.
If the user sends a 'y' and a relay command, do they have to send another 'y' before another command even if the minute has not expired?
no once 'y' is enterd means comunication is sucess and for 1 minute the relay comand from 0 to 9 can be entered. only after a 1 minute the user must again entere 'y' to operate relays. if the user fails to do so the relays should be reseted.
If the user sends a 'y' and, before the minute expires, sends another 'y' is the second 'y' ignored or is the time set back to 1 minute?
The user should only enter y when asked or else the 'y' should be ignored. so every one minute it should be asked.
OK. I think I understand. If the user sends a 'y' when prompted, they have one minute to control relays until all the relays are reset and they have to enter 'y' again. If they don't enter 'y' within one minute they will be prompted again.
// Global Variables
const byte RS1_IL = 3; // intializig names to Pins.
const byte RS2_KL30 = 4;
const byte RS3_KL30C = 5;
const byte RS4_KL31_Mio = 6;
const byte RS5_KL31_Extern = 7;
int Relaya = 0; // Variables to follow the relay status.
int Relayb = 0;
int Relayc = 0;
int Relayd = 0;
int Relaye = 0;
boolean RelayControlEnabled = false;
unsigned long RelayControlTime = 0;
const unsigned long ONE_MINUTE = (60ul * 1000ul);
void setup()
{
//serialport begin
Serial.begin(9600);
Serial.println("<Arduino is ready>");
// set output pins
pinMode(RS1_IL, OUTPUT);
pinMode(RS2_KL30, OUTPUT);
pinMode(RS3_KL30C, OUTPUT);
pinMode(RS4_KL31_Mio, OUTPUT);
pinMode(RS5_KL31_Extern, OUTPUT);
// Initial prompt
Serial.println("communication check: to proceed presses y");
}
void loop()
{
safteycheck();
relay();
}
void safteycheck()
{
unsigned long currentMillis = millis();
if (RelayControlEnabled)
{
// If enabled for a minute, disable and prompt for another 'y'
if (currentMillis - RelayControlTime >= ONE_MINUTE)
{
RelayControlEnabled = false;
RelayControlTime = currentMillis;
Serial.println("Communication unsuccessfull: Reset Mode is on");
safetytrigger();
Serial.println("communication check: to proceed presses y");
}
}
else
{
// RELAY CONTROL DISABLED
// If disabled for another minute, prompt again for 'y'
if (currentMillis - RelayControlTime >= ONE_MINUTE)
{
RelayControlTime = currentMillis; // Start the timer again
Serial.println("communication check: to proceed presses y");
}
// Look for a 'y' in the input
if (Serial.available())
{
char c = Serial.read();
if (c == 'y')
{
Serial.println("communication successfull");
RelayControlEnabled = true;
RelayControlTime = currentMillis; // Start the timer
}
}
}
}
void relay()
{
if (!RelayControlEnabled)
return; // Don't bother looking for commands
if (Serial.available())
{
char command = Serial.read();
switch (command)
{
case '\n': break; // Ignore newline
case '\r': break; // Ignore return
case 'y': break; // Ignore 'y' since relays are already enabled.
case '1':
digitalWrite(RS1_IL, HIGH);
Serial.println("RS1_IL switch is open");
Relaya = Relaya + 1;
delay(100);
break;
case '0':
digitalWrite(RS1_IL, LOW);
Serial.println("RS1_IL switch is close");
Relaya = Relaya - 1;
delay(100);
break;
case '3':
digitalWrite(RS2_KL30, HIGH);
Serial.println("RS2_KL30 switch is open");
Relayb = Relayb + 1;
delay(100);
break;
case '2':
digitalWrite(RS2_KL30, LOW);
Serial.println("RS2_KL30 switch is close");
Relayb = Relayb - 1;
delay(100);
break;
case '5':
digitalWrite(RS3_KL30C, HIGH);
Serial.println("RS3_KL30C switch is open");
Relayc = Relayc + 1;
delay(100);
break;
case '4':
digitalWrite(RS3_KL30C, LOW);
Serial.println("RS3_KL30C switch is Close");
Relayc = Relayc - 1;
delay(100);
break;
case '7':
digitalWrite(RS4_KL31_Mio, HIGH);
Serial.println("RS4_KL31_Mio switch is open");
Relayd = Relayd + 1;
delay(100);
break;
case '6':
digitalWrite(RS4_KL31_Mio, LOW);
Serial.println("RS4_KL31_Mio switch is close");
Relayd = Relayd - 1;
delay(100);
break;
case '9':
digitalWrite(RS5_KL31_Extern, HIGH);
Serial.println("RS5_KL31_Extern switch is open");
Relaye = Relaye + 1;
delay(100);
break;
case '8':
digitalWrite(RS5_KL31_Extern, LOW);
Serial.println("RS5_KL31_Extern switch is close");
Relaye = Relaye - 1;
delay(100);
break;
case 'a':
Serial.println("saftey Mode- all relays are reseted");
safetytrigger();
break;
default:
if ((Relaya > 1) || (Relayb > 1) || (Relayc > 1) || (Relayd > 1) || (Relaye > 1))
{
Serial.println("saftey mode: All relays are reseted ");
safetytrigger();
}
else
{
Serial.println("Invalid command received");
}
}
}
}
void safetytrigger()
{
Relaya = 0;
Relayb = 0;
Relayc = 0;
Relayd = 0;
Relaye = 0;
digitalWrite(RS1_IL, LOW);
digitalWrite(RS2_KL30, LOW);
digitalWrite(RS3_KL30C, LOW);
digitalWrite(RS4_KL31_Mio, LOW);
digitalWrite(RS5_KL31_Extern, LOW);
}
@johnwasser
:
once the user enter 'Y' they have one minute time to control relay thats correct. after one minute the user should again enter 'y' to control the relay if he fails only then it will be reseted.during the 1 min time if user has turrend the relay open using the command it will be open if he succedes to press 'y' once asked to do so. (the idea is the relay can controlled only under Surveillance by the user if he fails or the serial communication is interrupted the relay will be reseted )
@johnwasser:
i guess nw the sketch works perfectly. i added a deafault command 'd'. when d is pressed by user it means comunication exist so set the timer again from begining. if d is not pressed and one minute is gone then the relays will be reseted. do you think there is better way for doing it? or there is any libraries which makes the sketch more effecient .
thank you in advance.
// Global Variables
const byte RS1_IL = 3; // intializig names to Pins.
const byte RS2_KL30 = 4;
const byte RS3_KL30C = 5;
const byte RS4_KL31_Mio = 6;
const byte RS5_KL31_Extern = 7;
int Relaya = 0; // Variables to follow the relay status.
int Relayb = 0;
int Relayc = 0;
int Relayd = 0;
int Relaye = 0;
boolean RelayControlEnabled = false;
unsigned long RelayControlTime = 0;
const unsigned long ONE_MINUTE = (60ul * 1000ul);
unsigned long currentMillis;
void setup()
{
//serialport begin
Serial.begin(9600);
Serial.println("<Arduino is ready>");
// set output pins
pinMode(RS1_IL, OUTPUT);
pinMode(RS2_KL30, OUTPUT);
pinMode(RS3_KL30C, OUTPUT);
pinMode(RS4_KL31_Mio, OUTPUT);
pinMode(RS5_KL31_Extern, OUTPUT);
// Initial prompt
Serial.println("communication check: to proceed presses y");
}
void loop()
{
safteycheck();
relay();
}
void safteycheck()
{
currentMillis = millis();
if (RelayControlEnabled)
{
// If enabled for a minute, disable and prompt for another 'y'
if (currentMillis - RelayControlTime >= ONE_MINUTE)
{
RelayControlEnabled = false;
RelayControlTime = currentMillis;
Serial.println("Communication unsuccessfull: Reset Mode is on");
safetytrigger();
Serial.println("communication check: to proceed presses y");
}
}
else
{
// RELAY CONTROL DISABLED
// If disabled for another minute, prompt again for 'y'
if (currentMillis - RelayControlTime >= ONE_MINUTE)
{
RelayControlTime = currentMillis; // Start the timer again
Serial.println("communication check: to proceed presses y");
}
// Look for a 'y' in the input
if (Serial.available())
{
char c = Serial.read();
if (c == 'y')
{
Serial.println("communication successfull");
RelayControlEnabled = true;
RelayControlTime = currentMillis; // Start the timer
}
}
}
}
void relay()
{
if (!RelayControlEnabled)
return; // Don't bother looking for commands
if (Serial.available())
{
char command = Serial.read();
switch (command)
{
case '\n': break; // Ignore newline
case '\r': break; // Ignore return
case 'y': break; // Ignore 'y' since relays are already enabled.
case '1':
digitalWrite(RS1_IL, HIGH);
Serial.println("RS1_IL switch is open");
Relaya = Relaya + 1;
delay(100);
break;
case '0':
digitalWrite(RS1_IL, LOW);
Serial.println("RS1_IL switch is close");
Relaya = Relaya - 1;
delay(100);
break;
case '3':
digitalWrite(RS2_KL30, HIGH);
Serial.println("RS2_KL30 switch is open");
Relayb = Relayb + 1;
delay(100);
break;
case '2':
digitalWrite(RS2_KL30, LOW);
Serial.println("RS2_KL30 switch is close");
Relayb = Relayb - 1;
delay(100);
break;
case '5':
digitalWrite(RS3_KL30C, HIGH);
Serial.println("RS3_KL30C switch is open");
Relayc = Relayc + 1;
delay(100);
break;
case '4':
digitalWrite(RS3_KL30C, LOW);
Serial.println("RS3_KL30C switch is Close");
Relayc = Relayc - 1;
delay(100);
break;
case '7':
digitalWrite(RS4_KL31_Mio, HIGH);
Serial.println("RS4_KL31_Mio switch is open");
Relayd = Relayd + 1;
delay(100);
break;
case '6':
digitalWrite(RS4_KL31_Mio, LOW);
Serial.println("RS4_KL31_Mio switch is close");
Relayd = Relayd - 1;
delay(100);
break;
case '9':
digitalWrite(RS5_KL31_Extern, HIGH);
Serial.println("RS5_KL31_Extern switch is open");
Relaye = Relaye + 1;
delay(100);
break;
case '8':
digitalWrite(RS5_KL31_Extern, LOW);
Serial.println("RS5_KL31_Extern switch is close");
Relaye = Relaye - 1;
delay(100);
break;
case 'a':
Serial.println("saftey Mode- all relays are reseted");
safetytrigger();
break;
case 'd':
Serial.println("Dummy Command Recieved timer is reseted ");
RelayControlTime = currentMillis; // Start the timer again
break;
default:
if ((Relaya > 1) || (Relayb > 1) || (Relayc > 1) || (Relayd > 1) || (Relaye > 1))
{
Serial.println("saftey mode: All relays are reseted ");
safetytrigger();
}
else
{
Serial.println("Invalid command received");
}
}
}
}
void safetytrigger()
{
Relaya = 0;
Relayb = 0;
Relayc = 0;
Relayd = 0;
Relaye = 0;
digitalWrite(RS1_IL, LOW);
digitalWrite(RS2_KL30, LOW);
digitalWrite(RS3_KL30C, LOW);
digitalWrite(RS4_KL31_Mio, LOW);
digitalWrite(RS5_KL31_Extern, LOW);
}
david_24:
i added a deafault command 'd'. when d is pressed by user it means comunication exist so set the timer again from begining. if d is not pressed and one minute is gone then the relays will be reseted. do you think there is better way for doing it?
Another option would be to re-start the timer each time a valid relay command arrives.
Your code looks straight forward using single char commands.
If you want a simple way to use real command words instead check out my tutorial Serial Text I/O for the Real World