I have bluetooth (HC-05) connected on Serial port of Arduino Mega and two TM1637, I am receiving Strings from an Android phone with an app with two Time picker (Fajar and Zohar)
I am sending time which is set by time picker in a formatted way.
The format is
< : Start of number delimiter.
ID : is the number 1 or 2 (1 for Fajar and 2 for Zohar), it indicates from which time picker the data has come.
HH : Hours, set from time picker.
MM : Minutes, set from time picker.
> : End of number delimiter.
Here in the example, first data which comes in is <10320> , which is starting delimiter "<" first digit "1" is the ID from Fajar time picker,03 Hours and 20 minutes and ending delimiter ">". Second data is <21830>, which is from Zohar time picker and vice versa.
I am parsing the incoming string and converting them into integers using the code found on Arduino Forum.
const char startOfNumberDelimiter = '<'; const char endOfNumberDelimiter = '>'; void setup () { Serial.begin (9600); Serial.println ("Starting ..."); } // end of setup void processNumber (const long n) { Serial.println (n); } // end of processNumber void processInput () { static long receivedNumber = 0; static boolean negative = false; byte c = Serial.read (); switch (c) { case endOfNumberDelimiter: if (negative) processNumber (- receivedNumber); else processNumber (receivedNumber); // fall through to start a new number case startOfNumberDelimiter: receivedNumber = 0; negative = false; break; case '0' ... '9': receivedNumber *= 10; receivedNumber += c - '0'; break; case '-': negative = true; break; } // end of switch } // end of processInput void loop () { if (Serial.available ()) processInput (); // do other stuff here } // end of loop with the code above, i can convert string to integers and remove delimiters.
See output of the code in serial monitor. 
Problem is : I have to separate ID and Hours and Minutes from that integer and wrap Hours and Minutes that up in
int8_t Digits [] = {1, 2 , 3 , 4 }; Here {1,2,3,4} is {H,H,M,M} Hours and Minutes which displays digits on one of the TM1637.
and also ID with "1" should change the digits on one of the TM167 and the ID with "2" should change digits on another TM1637.
Here is my full code :
#include "TM1637.h" #define CLK1 2 #define DIO1 3 #define CLK2 4 #define DIO2 5 TM1637 Display1 (CLK1, DIO1); TM1637 Display2 (CLK2, DIO2); const char startOfNumberDelimiter = '<'; const char endOfNumberDelimiter = '>'; void setup () { Display1.set (); Display1.init (); Display2.set(); Display2.init(); Serial.begin(9600); Serial.println ("Starting ..."); } void loop () { if (Serial.available ()) processInput (); fajar(); zohar(); } void processNumber (const long n) { Serial.println (n); } // end of processNumber void processInput () { static long receivedNumber = 0; static boolean negative = false; byte c = Serial.read (); switch (c) { case endOfNumberDelimiter: if (negative) processNumber (- receivedNumber); else processNumber (receivedNumber); // fall through to start a new number case startOfNumberDelimiter: receivedNumber = 0; negative = false; break; case '0' ... '9': receivedNumber *= 10; receivedNumber += c - '0'; break; case '-': negative = true; break; } // end of switch } // end of processInput void fajar () { int8_t Digits [] = {1, 2 , 3 , 4 }; Display1.display (Digits); Display1.point(POINT_ON); } void zohar () { int8_t Digits [] = {1, 2 , 3 , 4 }; Display2.display (Digits); Display2.point(POINT_ON); } Please Help
EDIT : Here is my edited code with Majenko shown technique.
#include "TM1637.h" #define CLK1 2 #define DIO1 3 #define CLK2 4 #define DIO2 5 uint8_t fajar[4]; uint8_t zohar[4]; uint8_t address = 0; uint8_t ptr = 0; TM1637 Display1 (CLK1, DIO1); TM1637 Display2 (CLK2, DIO2); void setup () { Display1.set (); Display1.init (); Display2.set(); Display2.init(); Serial.begin(9600); } void loop () { int c = Serial.read(); switch (c) { case '<': // Start of string - reset everything address = 0; ptr = 0; break; case '>': // End of string fajartime (); // Edited zohartime (); // Edited break; default: if ((c >= '0') && (c <= '9')) { if (address == 0) { address = c - '0'; } else if (ptr < 4) { switch (address) { case 1: fajar[ptr] = c - '0'; break; case 2: zohar[ptr] = c - '0'; break; } ptr++; } } } } void fajartime () { Display1.display (fajar); Display1.point(POINT_ON); } void zohartime () { Display2.display (zohar); Display2.point(POINT_ON); } Thank you
