0

I'm really new to Arduino and have some doubts about how Arduino reads data. Lets suppose I have 3 variables a,b,c. I need these variables in my Arduino to make whatever. Since all variables are read using Serial.read() how can I distinguish between them in my Arduino?

Example:

Variable a: 2 Variable b: 3 Variable c: 4

Arduino example code:

void loop() {
int a = Serial.read();
int b = Serial.read();
int c = Serial.read();

if(a == '0'){
digitalWrite(yellowLed, HIGH);
}

if(b == '0'){
digitalWrite(blueLed, HIGH);
}

if(c == '0'){
digitalWrite(redLed, HIGH);
}

In every loop, my Arduino does serial.read() 3 times, if I only send value b from my app, since int a = Serial.read() executes first, will my b value be assigned to a? If yes, how can I avoid that problem and force the correct assignment? Aditionally, in every loop, if Serial.read() doesnt read anything, will my values be changed to null or zero?

closed as too broad by Juraj, Greenonline, sempaiscuba, gre_gor, MatsK Jan 8 at 14:45

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

0

This is a classic problem that is solved either by uniquely identifying each value or by agreeing on a pre-defined sequence of data. However, it can be said that many solutions exist for this problem & that these are only 2 examples.

In the first case, the sender and receiver need to agree that all data will be sent in pairs. A pair of bytes, for example, where the first identifies the data and the second is the data. In this case the order of data does not matter. And much more of one type of data can be sent than another. This is good to use when the data rate is irregular and does not need to be fast. Stock prices for example.

In the second case, the sender and receiver need to agree that all data will be sent in the same sequence even if there is no data to be sent. Such a scheme may employ a special synchronizing bit to identify the beginning of a sequence of data. Usually, when there is no data to be sent, a zero is sent. But if zeros are significant, then special null characters will have to be agreed upon. This is good when the data rate is regular and needs to be fast. Digitized audio for example.

  • I think I will use the first option. But I have another doubt, since in every loop my variables try to read from Serial, if Serial is empty, will their value be changed to 0? if I use Serial.avalaible() in an if statement, will I get rid of that problem? – Keka Bron Dec 26 '18 at 18:31
  • The Serial.available() tests if there is new data. Use it to tell if there is no data and skip reading data for this iteration. – st2000 Dec 26 '18 at 19:31
0

Here is a simple sketch that could work for your LED example:

const byte blueLED = 2;
const byte yellowLED = 3;

void setup(){
  Serial.begin(9600);
  pinMode(blueLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
}

void loop(){

  if(Serial.available()){

    switch(Serial.read()){
      case 'a':
        digitalWrite(yellowLED, HIGH);
        break;

      case 'b':
        digitalWrite(yellowLED, LOW);
        break;

      case 'c':
        digitalWrite(blueLED, HIGH);
        break;

      case 'd':
        digitalWrite(blueLED, LOW);
        break;
    }
  }
}

If you send the letter a, the yellow LED is turned on. Sending the letter b will turn the yellow LED off. There is nothing stopping you from sending 2 letters at the same time, such as ac. This would turn both LEDs on.

Not the answer you're looking for? Browse other questions tagged or ask your own question.