I'm attempting to control about 12 LEDs using two 74HC595 shift registers. I have the LEDs and shift registers wired up correctly. Then using the Serial monitor I can send a string with either a 1 or a 0. This then turns on the LEDs if its a 1. The code is all working in the Arduino and when I use the desktop serial monitor. However, I wanted to next have the string be sent from a raspberry pi using a serial connection. This also works but only on the first attempt, if I run the python script on the raspberry pi again with a different string of 1s or 0s the LEDs do not change. I have to fully power cycle the Arduino and then reconnect it to the serial connection with the Pi for the Arduino to accept the new string. Is there a way to code in python on the Pi script or in the Arduino IDE to clear the previous serial message sent to the Arduino so it can read a new one? Code for both is below
In my experience this is the easiest way to tidy up the code and add the code tags
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
This is nice, but I guess I have a more specific question as to why the Arduino is only receiving one serial message, which is a string when I run the code, Then it I run it again it doesn't receive another string/serial message?
Apologies, I wasnt exactly sure how to do it but I think I posted it correctly now. So Im running the python code with python3 on a raspberry pi. Im running this code first. This then sends a message through a serial connection to an arduino uno. It works sending one string message to the arduino, but the arduino has to be reset for it to read another message from the Pi if I run the python script on the py again. Do you know why that is?
your python code does not loop, so it sends one message and that's it
As explained in my tutorial every time you open the Serial line your arduino will reboot so some data might or will be lost if your python scripts sends stuff right away
So I looked over your tutorial, while very handy it still doesn't full answer my question, how do I rerun the python script which sends a serial message to the Arduino a second time after the I ran the script once after the connection was made? Like how do I clear the first serial message sent so that way I can send a new one?
Hello, I am attempting to use a raspberry pi 3 to control LEDs on an Arduino uno. I am using shift registers on the Arduino uno to expand the outputs. I am connecting the Arduino to the Raspberry Pi through a serial connection cable. I control the Raspberry pi from my laptop using SSH and PUTTY. I am sending a string from the raspberry pi through the serial connection to the Arduino. The Arduino then reads the string and lights up the corresponding LEDs. When I have everything connected it all works. Except if I keep everything connected and run the script on the raspberry pi again this time changing the string, nothing happens. I have to fully disconnect and power cycle the Arduino and then reconnect it for it to read and accept my new string from the python code. Is there a reason for this? Is there someway to code in python to clear the initial string message sent to the Arduino so that way it can read the second one when I run the script a second time? I have attached both the python and the Arduino code below.
Arduino Code
#include <Shifty.h>
Shifty reg;
void setup() {
reg.setBitCount(16);
reg.setPins(11 , 12, 8);
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) {
String data = Serial.readStringUntil('\n');
Serial.print(data);
reg.batchWriteBegin();
for( int i = 1; i <= 12 ; i++){
if (data.substring(i,i+1) == "1"){
reg.writeBit((i-1), HIGH);
}
if (data.substring(i,i+1) == "0"){
reg.writeBit((i-1), LOW);
}
}
reg.batchWriteEnd();
}
}
Python code
red_lightboard = serial.Serial('/dev/ttyACM0',9600, timeout=1) # Sets up serial comm with arduino uno using usb connection
red_lightboard.reset_output_buffer() # clears any data in the input buffer
testLED = 'r000000000001\n'
red_lightboard.write(testLED.encode('utf-8'))
line = red_lightboard.readline().decode().rstrip()
print(line)
```