Serial receiver

This commit is contained in:
giuliof 2018-12-18 17:45:19 +01:00
parent b5741db5b1
commit bb777a91cc
1 changed files with 53 additions and 40 deletions

View File

@ -72,51 +72,64 @@ void setup()
void loop()
{
inputString = ""; // Clear inputString
if (Serial.available())
{
/* ** Buffer to store received char **
* Data is an integer string terminated with newline '\n'
* */
static String inputString = "";
// number string is received. If '\n' terminate parsing.
while ((Serial.available()))
char digit = Serial.read(); // Read serial buffer
// Check if string terminator
if (digit == '\n')
{
char digit = Serial.read(); // Read serial buffer
if (digit != '\n')
{
inputString += digit;
}; // Add new character to the string inputString
delay(2); // Let the serial buffer catch its breath.
bufferPtr = 0;
// Make string conversion
serialvalue = inputString.toInt();
};
inputString = "";
//16 bits data received.
//first bit is telling if the data is about traffic lights and ABCD lights (true). or about the digits of the countdown (false).
//second bit is telling:
// -in case of digits (first bit is 0)if the digits are from the right side countdown (true) or left side countdown (false).
// -in case of trafic/ABCD lights: not implemented jet. Proposal is to use this for ABCD light depending on sequence (For example CDAB is also possible to show)
// Decode command
if ((serialvalue & 0x01) && not(serialvalue & 0x06))
{
trafficvalue = serialvalue;
}; //traficlights
// if((serialvalue&0x03) && not(serialvalue&0x0C)){ abcdvalue=serialvalue;}; //ABCD details
if ((serialvalue & 0x01) && (serialvalue & 0x02) && (serialvalue & 0x04) && not(serialvalue & 0x08))
{
endnrvalue = serialvalue;
}; //endnr info
if ((serialvalue & 0x01) && (serialvalue & 0x02) && (serialvalue & 0x04) && (serialvalue & 0x08))
{
statevalue = serialvalue;
}; //state info
//16 bits data received.
//first bit is telling if the data is about traffic lights and ABCD lights (true). or about the digits of the countdown (false).
//second bit is telling:
// -in case of digits (first bit is 0)if the digits are from the right side countdown (true) or left side countdown (false).
// -in case of trafic/ABCD lights: not implemented jet. Proposal is to use this for ABCD light depending on sequence (For example CDAB is also possible to show)
if ((not(serialvalue & 0x01)) && ((serialvalue & 0x02) or (serialvalue & 0x04)))
// Traffic light infos
// status info
// Right
// Left
if ((serialvalue & 0x01) && not(serialvalue & 0x06))
{
trafficvalue = serialvalue;
}; //traficlights
// if((serialvalue&0x03) && not(serialvalue&0x0C)){ abcdvalue=serialvalue;}; //ABCD details
if ((serialvalue & 0x01) && (serialvalue & 0x02) && (serialvalue & 0x04) && not(serialvalue & 0x08))
{
endnrvalue = serialvalue;
}; //endnr info
if ((serialvalue & 0x01) && (serialvalue & 0x02) && (serialvalue & 0x04) && (serialvalue & 0x08))
{
statevalue = serialvalue;
}; //state info
if ((not(serialvalue & 0x01)) && ((serialvalue & 0x02) or (serialvalue & 0x04)))
{
rightcountvalue = serialvalue;
}; //determine the digits value of the rightside
if ((not(serialvalue & 0x01)) && ((not(serialvalue & 0x02)) or (serialvalue & 0x04)))
{
leftcountvalue = serialvalue;
}; //determine the digits value of the leftside
}
else
{
rightcountvalue = serialvalue;
}; //determine the digits value of the rightside
if ((not(serialvalue & 0x01)) && ((not(serialvalue & 0x02)) or (serialvalue & 0x04)))
{
leftcountvalue = serialvalue;
}; //determine the digits value of the leftside
};
inputString += digit;
}
}
//Trafic lights and ABCD lights switching (independent to left/right switch connected to A5)
//Traficvalue bit 3 -14 show buzzer, green right side, orange right side, red right side, green left side, orange left side, red left side, A, B, C and D.
@ -132,15 +145,15 @@ void loop()
digitalWrite(15, (trafficvalue & 0x1000) ? HIGH : LOW); //C
digitalWrite(14, (trafficvalue & 0x2000) ? HIGH : LOW); //D
digitalWrite(6, (((statevalue >> 7) & B00000111) != 2) and (trafficvalue & 0x4000)); //E
digitalWrite(6, (((statevalue >> 7) & B00000111) != 2) and (trafficvalue & 0x4000)); //E
digitalWrite(7, (((statevalue >> 7) & B00000111) != 2) and (trafficvalue & 0x8000)); //F
digitalWrite(A15, (((statevalue >> 7) & B00000111) == 2) and (trafficvalue & 0x4000)); //green right arrow for fita finals
digitalWrite(39, (((statevalue >> 7) & B00000111) == 2) and (trafficvalue & 0x8000) ); //green left arrow for fita finals
digitalWrite(39, (((statevalue >> 7) & B00000111) == 2) and (trafficvalue & 0x8000)); //green left arrow for fita finals
//(((((statevalue>>7)&B00000111)=2)?LOW:HIGH) and )
if (digitalRead(A4))
{ //buzzer yes/no switch
{ //buzzer yes/no switch
digitalWrite(18, trafficvalue & 0x0008); //buzzer
}
else
@ -217,7 +230,7 @@ void loop()
digitalWrite(20, trafficvalue & 0x0100); //orange left side
digitalWrite(21, trafficvalue & 0x0200); //red left side
//digits
digitalWrite(23, (leftcountvalue & 0x0008) and (segment[leftdigit] != 0x040) ); //dots to indicate minutes or seconds indication
digitalWrite(23, (leftcountvalue & 0x0008) and (segment[leftdigit] != 0x040)); //dots to indicate minutes or seconds indication
rightdigit = ((leftcountvalue >> 4) & B00001111);
middigit = ((leftcountvalue >> 8) & B00001111);
leftdigit = ((leftcountvalue >> 12) & B00001111);