M
M
Max_Drevo2017-08-16 10:18:14
Arduino
Max_Drevo, 2017-08-16 10:18:14

How to set 2 values ​​​​for two variables in Arduino through a serial?

Can you please tell me why the code below fails to set the hour and minute variables?

int h=0,m=0, s=0;
char c1,c2;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.println("write Hour");
  delay (5000);
 while (Serial.available() > 0) {
                // считываем принятый байт:
                c1 = Serial.read();
                 h =atoi(c1);
  }
  
  Serial.println("write Min");
    delay (5000);
  while (Serial.available() > 0) {
                // считываем принятый байт:
                c2 = Serial.read();
                m =atoi(c2);
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DrJarold, 2017-08-16
@DrJarold

byte h = 0, m = 0, s = 0;
byte BUFFER = 255;
byte index = 0;
String inString;
bool isEndMessage = false;

void setup() {

  Serial.begin(9600);
  Serial.println("Write Hour!");
  while (!Serial.available())
    delay(1);

  h = atoi(GetData());
  Serial.println("Write Min!");
  while (!Serial.available())
    delay(1);

  m = atoi(GetData());
}
String GetData() {
  inString = "";
  isEndMessage = false;
  while (Serial.available()) {
    char ch = Serial.read();
    switch (ch) {
    case '\n':
    case '\r':
      isEndMessage = true;
      break;
    }
    if (!isEndMessage)
      inString += Serial.read();
    delay(1);
  }
  return inString;
}

byte atoi(String data) {
  byte result = 0;
  //дописываем логику
  return result;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question