Encoding Problem, lagging exponentially

Hi

We're trying to have an Arduino Uno talk to our raspberry pi and vice versa through the serial port. Arduino to the Pi works but when we try to encode and send to the Arduino it lags and throws off the loop (python) above the encoding line; without encoding it works as intended.

Although we need to use the encoding so that the Pi can successfully talk back to the Arduino in order to operate LEDs. Below is the code, both the Arduino code and the Python code on the Pi, we've also included a representation of our breadboard we're using to operate the buttons and LEDs.

We are new to Arduino, any help would be greatly appreciated.
Thanks

Here is the Arduino Code;

#define LED_1_PIN 8
#define LED_2_PIN 9
#define LED_3_PIN 10
#define LED_4_PIN 11

#define BUTTON_1_PIN 2
#define BUTTON_2_PIN 3
#define BUTTON_3_PIN 4
#define BUTTON_4_PIN 5

static const int numOfButtons = 4;

byte lastButtonState1 = HIGH;
byte currentButtonState1 = HIGH;
byte lastButtonState2 = HIGH;
byte currentButtonState2 = HIGH;
byte lastButtonState3 = HIGH;
byte currentButtonState3 = HIGH;
byte lastButtonState4 = HIGH;
byte currentButtonState4 = HIGH;
byte state[numOfButtons];

void powerOffAllLEDs()
{
    digitalWrite(LED_1_PIN, LOW);
    digitalWrite(LED_2_PIN, LOW);
    digitalWrite(LED_3_PIN, LOW);
    digitalWrite(LED_4_PIN, LOW);
}

void setup()
{
  Serial.begin(9600);
  pinMode(LED_1_PIN, OUTPUT);
  pinMode(LED_2_PIN, OUTPUT);
  pinMode(LED_3_PIN, OUTPUT);
  pinMode(LED_4_PIN, OUTPUT);
  pinMode(BUTTON_1_PIN, INPUT_PULLUP);
  pinMode(BUTTON_2_PIN, INPUT_PULLUP);
  pinMode(BUTTON_3_PIN, INPUT_PULLUP);
  pinMode(BUTTON_4_PIN, INPUT_PULLUP);
  
  powerOffAllLEDs();
}

void loop()
{
  byte readValue1 = digitalRead(BUTTON_1_PIN);
  byte readValue2 = digitalRead(BUTTON_2_PIN);
  byte readValue3 = digitalRead(BUTTON_3_PIN);
  byte readValue4 = digitalRead(BUTTON_4_PIN);
   
  currentButtonState1 = readValue1;
  currentButtonState2 = readValue2;
  currentButtonState3 = readValue3;
  currentButtonState4 = readValue4;
  byte resetState[numOfButtons] = {0,0,0,0};
  
  if (!currentButtonState1 && lastButtonState1) {
      resetState[0] = 1;
      if (state[0] == 0) {
        state[0] = 1;
        
      }
      else {
        state[0] = 0; 
      }
  lastButtonState1 = LOW;
  }
  else if (currentButtonState1 && !lastButtonState1) {
  lastButtonState1 = HIGH;      
  }
  
  if (!currentButtonState2 && lastButtonState2) {
      resetState[1] = 1;
      if (state[1] == 0) {
        state[1] = 1;
      }
      else {
        state[1] = 0; 
      }
  lastButtonState2 = LOW;
  }
  else if (currentButtonState2 && !lastButtonState2) {
  lastButtonState2 = HIGH;      
  }
  
  if (!currentButtonState3 && lastButtonState3) {
      resetState[2] = 1;
      if (state[2] == 0) {
        state[2] = 1;
      }
      else {
        state[2] = 0; 
      }
  lastButtonState3 = LOW;
  }
  else if (currentButtonState3 && !lastButtonState3) {
  lastButtonState3 = HIGH;      
  }
  
  if (!currentButtonState4 && lastButtonState4) {
      resetState[3] = 1;
      if (state[3] == 0) {
        state[3] = 1;
      }
      else {
        state[3] = 0; 
      }
  lastButtonState4 = LOW;
  }
  else if (currentButtonState4 && !lastButtonState4) {
  lastButtonState4 = HIGH;      
  }
  
 
  for (int i = 0; i < numOfButtons; i ++){
     Serial.print(resetState[i]);
  }
 
  String ledString = "";
 
  if (Serial.available() > 0) {
    if (Serial.read() == 'S') {
    ledString = Serial.readStringUntil('E');
  }
  }

      Serial.println(ledString);
      if(ledString.charAt(0) == '1'){
        digitalWrite(LED_1_PIN, HIGH);
        }
        else if(ledString.charAt(0) == '0'){
        digitalWrite(LED_1_PIN, LOW);
        }
      
      if(ledString.charAt(1) == '1'){
        digitalWrite(LED_2_PIN, HIGH);
        }
        else if(ledString.charAt(1) == '0'){
        digitalWrite(LED_2_PIN, LOW);
        }
        
      if(ledString.charAt(2) == '1'){
        digitalWrite(LED_3_PIN, HIGH);
        }
        else if(ledString.charAt(2) == '0'){
        digitalWrite(LED_3_PIN, LOW);
        }
        
      if(ledString.charAt(3) == '1'){
        digitalWrite(LED_4_PIN, HIGH);
        }
        else if(ledString.charAt(3) == '0'){
        digitalWrite(LED_4_PIN, LOW);
        } 
}

Here is the Python code;

#!/usr/bin/env python3
import serial

if __name__ == '__main__':
	ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
	ser.flush()

	while True:
		county = 1
		number = ser.readline()
		#print(number)
		if number != b'':
			moddedNum = str(number.decode()) 
			#print(moddedNum)
			for element in moddedNum:
				if element == '1':
					print("Button " + str(county) + " has been pressed.")
				county += 1
			moddedNum = ('S' + moddedNum + 'E')
			#Code works perfectly until you uncomment below line...the encode somehow even breaks
                           the above for loop
			#ser.write(moddedNum.encode('utf-8'))

Full4Test_ino.ino (3.53 KB)

Please read the sticky post at the top of the forum about how to properly post your code using code tags rather than attaching it. It will help people help you.

Why in the world do you need to encode ascii 1's and 0's into UTF-8? Just use plain text.

On the Uno side, get rid of the String class variables and learn how to use C strings. You should also read Robin2's excellent tutorial Serial Input Basics to see how to do good, reliable Serial communication

blh64:
Why in the world do you need to encode ascii 1's and 0's into UTF-8? Just use plain text.

When I send a byte array over serial from the arduino, it automatically encodes it in UTF-8. I assumed it would be best to send back the same form it sent.

How would I send plain text from the arduino?

mkvicala:
When I send a byte array over serial from the arduino, it automatically encodes it in UTF-8.

No, it does not.