Need help with code -serial

Hi all guys!
At the beginning, I would like to apologize that my English is very weak :cold_sweat: . I have arduino UNO and big problem with program .I wanted it to control a value (HIGH/LOW) for example on pin 4 and 5.
The problem is that I need to control the values on the pins over the serial monitor in this style:

Send 'X' and '1' to digitalWrite(pinA, HIGH);

and if i send 'X' and '0' soo i need digitalWrite(pinA, LOW);

it the same to pin 5 but instead letter X use Y. It is possible? Or something like that?Can you help me with this please?

Here is an approach that you can try.

Create an empty global char array with space for 3 chars (X / Y, 1 / 0, NULL terminate).

char buffer[3];

Try using Serial.readBytes() method described here: Stream.readBytes() - Arduino Reference.

Serial.readBytes(buffer, 2);

Then, use conditional statements to check each char stored in the array, then perform an action based on that.

if(buffer[0] == 'X' && buffer[1] == '1') digitalWrite(pinA, HIGH);

Type "X1", "X0", etc. into the Serial Monitor to send the info.
Note: there is a drop-down menu in the Serial Monitor that says "Newline" or "Carriage Return", etc. Change that to "No Line Ending" first.

Does this help? I never tried it before. It might not be the best method.
Note: this halts your program for 1 second to wait for Serial data. You set that timeout using Serial.setTimeout() method (Stream.setTimeout() - Arduino Reference). I will come up with a different approach later. Have a look at Serial.read crazy results - #20 by system - Programming Questions - Arduino Forum as well.

Even simpler than dkl65's example is to use 4 different single letters (A instead of X0, B instead of X1, C instead of Y0, and D instead of Y1). Then, receive one letter, and know what pin to set to what state.

If there is some application sending the data, you could have it send binary data (40, 41, 50, 51) and still get both pieces of information in one byte.

Yes it is but i need this: x1 and y1. Because A,B,C,D,E,F is letter for another function. And this project must be the easiest control method for understanding.

Dkl65 :I tried what you advised me but it but it did not work. it's still complicated for me. :~
If you have time.Could you create at least part of my code :blush:. example for X1 = high x0 = low

Thanks for your time

Even simpler than dkl65's example is to use 4 different single letters

I predict that PaulS will say "Then use G,H,I,J!". :grin:

I will have time on Saturday to test codes on my Arduino UNO SMD edition. That is why I am so unsure of my examples!

If you have time.Could you create at least part of my code

I think that I have already done that. :wink: Look at one of my other posts through the last link on my first post here (Serial.read crazy results - #20 by system - Programming Questions - Arduino Forum). It's not only you that's having problems reading multiple bytes of Serial data! Serial data arrives asynchronously (one byte at a time [correct me if I'm wrong]). Therefore, we have to use a while loop to loop until all the bytes of Serial data is read. In the while loop, inserted each byte into a string as a character. Outside the while loop, check if the string is empty. If it isn't, use further conditions to check is "X0", etc. and do something if it is. After all the conditionals, clear the string for new input. Looking at the link, you should know how to modify it to do the aforementioned (most steps are already done for you there).

P.S. I'm a Full Member! :smiley:

Thank you so much. You saved my life :smiley: :smiley:

int VstupA=3;
int VstupB=4;
int incomingByte = 0;   
String inString = ""; 
void setup() {
  
  pinMode(VstupA, OUTPUT);
  pinMode(VstupB, OUTPUT);
  Serial.begin(9600);     
}

void loop() {
  
  while (Serial.available() > 0) {
    incomingByte = Serial.read();
    inString += (char)incomingByte; 
  }
  if (inString == "X1"){ 
    digitalWrite(VstupA, HIGH); 
    Serial.print(" INPUT A:1");
    inString = ""; 
  }
  if (inString == "X0"){ 
    digitalWrite(VstupA, LOW); 
    Serial.print("INPUT A:0 ");
    inString = ""; 
  }
  if (inString == "Y1"){ 
    digitalWrite(VstupB, HIGH); 
    Serial.print(" INPUT B:1");
    
    inString = ""; 
  }
  if (inString == "Y0"){ 
    digitalWrite(VstupB, LOW); 
    Serial.print(" INPUT B:0");
    
    inString = ""; 
  }
}
  if (inString == "X1"){ 
  if (inString == "X0"){ 
  if (inString == "Y1"){ 
  if (inString == "Y0"){

What are the chances that more than one of these blocks will be executed? None. So, save some processing time, and use else if for the last three.

One more :sweat_smile:
I created two source code and I need to merge them together. The manual code is for three button.
First switch:obvodNOT->->->obvodRS.Next two send HIGHT / LOW on pin 2 and 3.

Same thing does second code but with the difference: if i send
X1-- pin 2=HIGH X0 pin 2 =LOW
Y1-- pin 3= HIGH Y0 pin3 =LOW
NOT,AND,OR,XOR,XNOR,RS == pin ***HIGH

it is possible to combine these codes into one?Can you explain how? :open_mouth:
I need controls
First:

int VstupA =2;
int VstupB =3;

int TlacitkoA=5;
int TlacitkoB=6;

int prepinac = 7;              
int obvodNOT = 13;              
int obvodAND = 12;              
int obvodOR = 11;              
int obvodXOR =10;              
int obvodXNOR = 9;            
int obvodRS = 8;              
int val;                        
int val2;                       
int akt_obvod = 0;              

int buttonState;                
int buttonStateTlacitkoA; 
int buttonStateTlacitkoB; 

void setup() {              
  pinMode(VstupA, OUTPUT);    
  pinMode(VstupB, OUTPUT);
  
  pinMode(TlacitkoA,INPUT);
  pinMode(TlacitkoB,INPUT);
  
  pinMode(prepinac, INPUT);    
  pinMode(obvodNOT, OUTPUT);   
  pinMode(obvodAND, OUTPUT);   
  pinMode(obvodOR, OUTPUT);    
  pinMode(obvodXOR, OUTPUT);   
  pinMode(obvodXNOR, OUTPUT);  
  pinMode(obvodRS, OUTPUT);    
  
  buttonState = digitalRead(prepinac);   
  
}

void loop(){                    
  val = digitalRead(prepinac);       
  delay(10);                         
  val2 = digitalRead(prepinac); 
  if (val == val2) {            
  if (val != buttonState) {     		     
  if (val == LOW) {             
  if (akt_obvod == 0) {         
       akt_obvod = 1;           
     } else {
         if (akt_obvod == 1) {      
               akt_obvod = 2;         
     } else {
         if (akt_obvod == 2) {      
               akt_obvod = 3;      
     } else {
         if (akt_obvod == 3) {     
               akt_obvod = 4;    
     } else {
         if (akt_obvod == 4) {      
               akt_obvod = 5;           
            }          
       else {
         if (akt_obvod == 5) {     
               akt_obvod = 6;            
      } else {
         if (akt_obvod == 6) {       
               akt_obvod = 0;       
            }  
	     }
          }	
        }
       }
    }
   }
  }
    buttonState = val;              
  }  

  if (akt_obvod == 0) {            
    digitalWrite(obvodNOT, LOW);
    digitalWrite(obvodAND, LOW);
    digitalWrite(obvodOR, LOW);
    digitalWrite(obvodXOR, LOW);
    digitalWrite(obvodXNOR, LOW);
    digitalWrite(obvodRS, LOW);
  }
  if (akt_obvod == 1) {           
    digitalWrite(obvodNOT, HIGH);
    digitalWrite(obvodAND, LOW);
    digitalWrite(obvodOR, LOW);
    digitalWrite(obvodXOR, LOW);
    digitalWrite(obvodXNOR, LOW);
    digitalWrite(obvodRS, LOW);
  }
  if (akt_obvod == 2) {          
    digitalWrite(obvodNOT, LOW);
    digitalWrite(obvodAND, HIGH);
    digitalWrite(obvodOR, LOW);
    digitalWrite(obvodXOR, LOW);
    digitalWrite(obvodXNOR, LOW);
    digitalWrite(obvodRS, LOW); 
  }
  if (akt_obvod == 3) {         
    digitalWrite(obvodNOT, LOW);
    digitalWrite(obvodAND, LOW);
    digitalWrite(obvodOR, HIGH);
    digitalWrite(obvodXOR, LOW);
    digitalWrite(obvodXNOR, LOW);
    digitalWrite(obvodRS, LOW); 
  }  
if (akt_obvod == 4) {           
    digitalWrite(obvodNOT, LOW);
    digitalWrite(obvodAND, LOW);
    digitalWrite(obvodOR, LOW);
    digitalWrite(obvodXOR, HIGH);
    digitalWrite(obvodXNOR, LOW);
    digitalWrite(obvodRS, LOW); 
  }
if (akt_obvod == 5) {          
    digitalWrite(obvodNOT, LOW);
    digitalWrite(obvodAND, LOW);
    digitalWrite(obvodOR, LOW);
    digitalWrite(obvodXOR, LOW);
    digitalWrite(obvodXNOR, HIGH);
    digitalWrite(obvodRS, LOW);    
  }
if (akt_obvod == 6) {         
    digitalWrite(obvodNOT, LOW);
    digitalWrite(obvodAND, LOW);
    digitalWrite(obvodOR, LOW);
    digitalWrite(obvodXOR, LOW);
    digitalWrite(obvodXNOR, LOW);
    digitalWrite(obvodRS, HIGH);
  }  
 }
{
buttonStateTlacitkoA = digitalRead(TlacitkoA);
buttonStateTlacitkoB = digitalRead(TlacitkoB);
if (buttonStateTlacitkoA == HIGH) {      
    digitalWrite(VstupA, HIGH); 
  } 
 if (buttonStateTlacitkoA == LOW){
    digitalWrite(VstupA, LOW);  
  }    
 if (buttonStateTlacitkoB == HIGH) {      
    digitalWrite(VstupB, HIGH);  
  } 
 if (buttonStateTlacitkoB == LOW) {    
    digitalWrite(VstupB, LOW);  
  } 
}
}

Second:

int VstupA=2;      
int VstupB=3; 

int obvodNOT = 13;              
int obvodAND = 12;              
int obvodOR = 11;              
int obvodXOR =10;              
int obvodXNOR = 9;            
int obvodRS = 8;  


int incomingByte = 0;   
String inString = "";   
void setup() {          
  
  pinMode(VstupA, OUTPUT);   
  pinMode(VstupB, OUTPUT);
  pinMode(obvodNOT, OUTPUT);   
  pinMode(obvodAND, OUTPUT);   
  pinMode(obvodOR, OUTPUT);    
  pinMode(obvodXOR, OUTPUT);   
  pinMode(obvodXNOR, OUTPUT);  
  pinMode(obvodRS, OUTPUT);    
  Serial.begin(9600);       
}

void loop () {     
  
  while (Serial.available() > 0) {  
    incomingByte = Serial.read();   
    inString += (char)incomingByte;     
  }
  if (inString == "Y1"){         
    digitalWrite(VstupA, HIGH);  
    Serial.println(" INPUT B:1");  
    inString = "";               
  }
  if (inString == "Y0"){         
    digitalWrite(VstupA, LOW);   
    Serial.println("INPUT B:0 ");  
    inString = "";               
  }
  if (inString == "X1"){        
    digitalWrite(VstupB, HIGH); 
    Serial.println(" INPUT A:1"); 
    
    inString = "";             

  }
  if (inString == "X0"){        
    digitalWrite(VstupB, LOW);  
    Serial.println(" INPUT A:0"); 
    
    inString = "";             

  }
  
 if (inString == "NOT"){        
    digitalWrite(obvodNOT, HIGH);
    digitalWrite(obvodAND, LOW);
    digitalWrite(obvodOR, LOW);
    digitalWrite(obvodXOR, LOW);
    digitalWrite(obvodXNOR, LOW);
    digitalWrite(obvodRS, LOW);
    Serial.print('\n'); 
    Serial.println("NOT: On"); 
    inString = "";             

  }
 if (inString == "AND"){        
    digitalWrite(obvodNOT, LOW);
    digitalWrite(obvodAND, HIGH);
    digitalWrite(obvodOR, LOW);
    digitalWrite(obvodXOR, LOW);
    digitalWrite(obvodXNOR, LOW);
    digitalWrite(obvodRS, LOW);
    Serial.print('\n');  
    Serial.println("AND: On"); 
    inString = "";             

  }
 if (inString == "OR"){        
    digitalWrite(obvodNOT, LOW);
    digitalWrite(obvodAND, LOW);
    digitalWrite(obvodOR, HIGH);
    digitalWrite(obvodXOR, LOW);
    digitalWrite(obvodXNOR, LOW);
    digitalWrite(obvodRS, LOW); 
    Serial.print('\n'); 
    Serial.println("OR: On"); 
    inString = "";             

  }
 if (inString == "XOR"){        
    digitalWrite(obvodNOT, LOW);
    digitalWrite(obvodAND, LOW);
    digitalWrite(obvodOR, LOW);
    digitalWrite(obvodXOR, HIGH);
    digitalWrite(obvodXNOR, LOW);
    digitalWrite(obvodRS, LOW);
    Serial.print('\n');  
    Serial.println("XOR: On"); 
    inString = "";             

  }
  if (inString == "XNOR"){        
    digitalWrite(obvodNOT, LOW);
    digitalWrite(obvodAND, LOW);
    digitalWrite(obvodOR, LOW);
    digitalWrite(obvodXOR, LOW);
    digitalWrite(obvodXNOR, HIGH);
    digitalWrite(obvodRS, LOW); 
    Serial.print('\n'); 
    Serial.println("XNOR: On"); 
    inString = "";             

  }
  if (inString == "RS"){        
    digitalWrite(obvodNOT, LOW);
    digitalWrite(obvodAND, LOW);
    digitalWrite(obvodOR, LOW);
    digitalWrite(obvodXOR, LOW);
    digitalWrite(obvodXNOR, LOW);
    digitalWrite(obvodRS, HIGH); 
    Serial.print('\n'); 
    Serial.println("RS: On"); 
    inString = "";             

  } 
}

What is this mess?

  if (akt_obvod == 0) {         
       akt_obvod = 1;           
     } else {
         if (akt_obvod == 1) {      
               akt_obvod = 2;         
     } else {
         if (akt_obvod == 2) {      
               akt_obvod = 3;      
     } else {
         if (akt_obvod == 3) {     
               akt_obvod = 4;    
     } else {
         if (akt_obvod == 4) {      
               akt_obvod = 5;           
            }          
       else {
         if (akt_obvod == 5) {     
               akt_obvod = 6;            
      } else {
         if (akt_obvod == 6) {       
               akt_obvod = 0;       
            }  
	     }
          }	
        }
       }
    }
   }
  }

Regardless of the value of akt_obvod, add one to it, unless it is 6, in which case make it 0.

akt_obvod++;
if(akt_obvod >= 6)
  akt_obvod = 0;

does the same thing in a bit fewer lines of code.

if (buttonStateTlacitkoA == HIGH) {      
    digitalWrite(VstupA, HIGH); 
  } 
 if (buttonStateTlacitkoA == LOW){
    digitalWrite(VstupA, LOW);  
  }

If the first test is not true, the second one MUST be true. So, an else statement is more appropriate AND clearer.

First switch:obvodNOT->->->obvodRS.Next two send HIGHT / LOW on pin 2 and 3.

I have no clue what all those arrows mean.

NOT,AND,OR,XOR,XNOR,RS == pin ***HIGH

I don't understand this, either.

it is possible to combine these codes into one?

Yes, it is.

Can you explain how?

A text editor and nimble fingers.

I need controls

I do to.

PaulS:

akt_obvod++;

if(akt_obvod >= 6)
  akt_obvod = 0;

Should just be greater than, because in his code, if it's 5, it goes to 6. In your example, it would be set to 0

if (buttonStateTlacitkoA == HIGH) {      

digitalWrite(VstupA, HIGH);
  }
if (buttonStateTlacitkoA == LOW){
    digitalWrite(VstupA, LOW); 
  }

Or cut it down even further:

digitalWrite(VstupA, buttonStateTlacitkoA);

Should just be greater than, because in his code, if it's 5, it goes to 6. In your example, it would be set to 0

Right you are! Thanks for catching that.