I am using arduino UNO board. Need simple help here
I am trying to read status from UART . I want to latch UART value to particular case executed.
Whenever change of Switch statement LED must flicker for Few minute and later it should be steady..
This cycle true for each Switch chage statement.
If incoming byte is 49 I.e 0 it should always flicker RED LED . Green LED Should be off.
Can someone help how to latch here.
static int State = 0;
int IncomeByte = 0;
static int Counter = 0;
static int Fault_Input = 1;
unsigned char Flag_status = 0;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
}
void loop() {
Recieve_UART();
Check_Status();
if (Fault_Input == 49) {
Red_Led_Toggle();
} else if (Fault_Input == 50) {
if (Counter < 100) {
State = 0;
} else {
Counter = 0;
Green_Led_High();
}
} else if (Fault_Input == 51) {
if (Counter < 100) {
State = 1;
} else {
Counter = 0;
Green_Led_High();
}
}
else if (Fault_Input == 52) {
if (Counter < 100) {
State = 2;
} else {
Counter = 0;
Green_Led_High();
}
}
}
void Red_Led_Toggle() {
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
void Green_LED_Toggle() {
digitalWrite(3, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(3, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void Green_Led_High() {
digitalWrite(3, HIGH); // turn the LED on (HIGH is the voltage level)
}
void Green_Led_Low() {
digitalWrite(3, LOW); // turn the LED off by making the voltage LOW
}
void Check_Status() {
switch (State) {
case 0:
Serial.println("Case0 is executing");
Green_LED_Toggle();
Flag_status = 1;
//State = 1;
break;
case 1:
Serial.println("Case1 is executing");
Green_LED_Toggle();
Flag_status = 1;
//State = 2;
break;
case 2:
Serial.println("Case2 is executing");
Green_LED_Toggle();
Flag_status = 1;
//State = 0;
break;
default: break;
}
}
void Recieve_UART() {
if (Serial.available() > 0) {
// read the incoming byte:
IncomeByte = Serial.read();
Fault_Input = IncomeByte;
// say what you got:
Serial.print("IncomeByte: ");
Serial.println(IncomeByte, DEC);
}
}
Can you share me link.. How we can impiment this here using timer.
UART has to send /Receive command. There only 2 cases
case 1: if 0 Send RED led should keep flicker
other case
whenever change in case ,Green_LED should flicker for Few minute and then it must be steady.
I dont know timer interrupt will help. if we use timer interrupt it will keep flickering LED.
We need to enable and disable timerinterrupt in that case.
I have made changes like this,still not working as expected
static int State = 0;
int IncomeByte = 0;
static int Counter = 0;
static int Fault_Input = 1;
unsigned char Flag_status = 0;
unsigned char Current_state = 48;
unsigned char Previous_State = 49;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
}
void loop() {
Recieve_UART();
//Check_Status();
if ((Current_state - Previous_State) >= 1)
Serial.print("Current_state:");Serial.println(Current_state, DEC);
Serial.print("Previous_State:");Serial.println(Previous_State, DEC);
Previous_State = Current_state;
if (Previous_State == 1) {
Red_Led_Toggle();
} else if (Previous_State == 2) {
if (Counter < 100) {
State = 0;
Check_Status();
} else {
Counter = 0;
Green_Led_High();
}
} else if (Previous_State == 3) {
if (Counter < 100) {
State = 1;
Check_Status();
} else {
Counter = 0;
Green_Led_High();
}
}
else if (Previous_State == 4) {
if (Counter < 100) {
State = 2;
Check_Status();
} else {
Counter = 0;
Green_Led_High();
}
}
}
void Red_Led_Toggle() {
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
void Green_LED_Toggle() {
digitalWrite(3, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(3, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void Green_Led_High() {
digitalWrite(3, HIGH); // turn the LED on (HIGH is the voltage level)
}
void Green_Led_Low() {
digitalWrite(3, LOW); // turn the LED off by making the voltage LOW
}
void Check_Status() {
switch (State) {
case 0:
Serial.println("Case0 is executing");
//Green_LED_Toggle();
Flag_status = 1;
//State = 1;
break;
case 1:
Serial.println("Case1 is executing");
//Green_LED_Toggle();
Flag_status = 1;
//State = 2;
break;
case 2:
Serial.println("Case2 is executing");
//Green_LED_Toggle();
Flag_status = 1;
//State = 0;
break;
default: break;
}
}
void Recieve_UART() {
if (Serial.available() > 0) {
// read the incoming byte:
Current_state = Serial.read();
// say what you got:
// Serial.print("Current_state: ");
//Serial.println(Current_state, DEC);
}
}
Code modified as below .let me know if any suggestion
static int State = 0;
int IncomeByte = 0;
static int Counter = 0;
static int Fault_Input = 1;
unsigned char Flag_status = 0;
char Current_state = 0;
char Previous_State = 0;
boolean newData = false;
char receivedChar;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
}
long value;
void loop() {
recvOneChar();
showNewData();
value=(Current_state - Previous_State);
map(value, 0, -2, 0, 2);
Serial.print("Balue:");
Serial.println(value);
delay(1000);
if ( value>= 1) {
Serial.print("Current_state:");
Serial.println(Current_state);
Serial.print("Previous_State:");
Serial.println(Previous_State);
Previous_State = Current_state;
}
/* if (Previous_State == 1) {
Red_Led_Toggle();
} else if (Previous_State == 2) {
if (Counter < 100) {
State = 0;
Check_Status();
} else {
Counter = 0;
Green_Led_High();
}
} else if (Previous_State == 3) {
if (Counter < 100) {
State = 1;
Check_Status();
} else {
Counter = 0;
Green_Led_High();
}
}
else if (Previous_State == 4) {
if (Counter < 100) {
State = 2;
Check_Status();
} else {
Counter = 0;
Green_Led_High();
}
}*/
}
void Red_Led_Toggle() {
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
void Green_LED_Toggle() {
digitalWrite(3, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(3, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void Green_Led_High() {
digitalWrite(3, HIGH); // turn the LED on (HIGH is the voltage level)
}
void Green_Led_Low() {
digitalWrite(3, LOW); // turn the LED off by making the voltage LOW
}
void Check_Status() {
switch (State) {
case 0:
Serial.println("Case0 is executing");
//Green_LED_Toggle();
Flag_status = 1;
//State = 1;
break;
case 1:
Serial.println("Case1 is executing");
//Green_LED_Toggle();
Flag_status = 1;
//State = 2;
break;
case 2:
Serial.println("Case2 is executing");
//Green_LED_Toggle();
Flag_status = 1;
//State = 0;
break;
default: break;
}
}
void recvOneChar() {
if (Serial.available() > 0) {
Current_state = Serial.read();
newData = true;
}
}
void showNewData() {
if (newData == true) {
Serial.print("RCV-->CurrentState:");
Serial.println(Current_state);
newData = false;
}
}