Is arduino not recieving my byte sent

sorry for the newbie question but I spent the last couple days trying to figure out pyserial but her goes:
ok so I have this problem where I send a byte over serial to the arduino from python and I know it recives it because of the flashing lights on the arduino board itself, but it doesn't turn on the LED strip like it is supposed to do Python code:

if "turn on lights" in sound:
        
        ser = serial.Serial('COM3', 9600) # Establish the connection on a specific port
        
        ser.write(bytes("b", 'utf-8'))
        
        ser.close()

Arduino Code:

void setup() {
  Serial.begin(9600);
   FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

}
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }

void loop() {
    static uint8_t hue = 0;

 if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    if (incomingByte == 'b'){
      for(int i = 0; i<NUM_LEDS; i++){
        leds[i] = CRGB(0,0,225);
      }
      FastLED.show();
      }

CodeDetected_ClickToSendToGist
1

[10:11 PM]

If you use the Arduino IDE serial monitor and send a 'b' at 9600 baud does it work?

I know it recieves it because of the flashing lights on the arduino board itself, but it doesn't turn on the LED strip like it is supposed to do

Since the problem appears to be on the Arduino side, I would recommend that you use a FastLED.h library example to get things working on the Arduino side with your lights.

Then get that demo program to run when you send the letter b from the Serial monitor.

Finally get the serial input of b from python on your computer.

If you change the serial write in your Python code from:

ser.write(bytes("b", 'utf-8'))

to

ser.write(b'b')

do you see any change in behavior?

so everything works from serial monitor. and if I change it on the python side to ser.write(b'b') the problem still is there. I am using an arduino uno r3

Please post the code which compiles and runs from the serial monitor when entering "b". Was the monitor set with any line ending?

If you use python code which just sends the command on a periodic basis but does not involve your sound reception does the Arduino run the lights?

I have tested, in a simplified fashion, almost every version of python code you have used in your different threads, and I can always see what you have sent, with all you different methods.

yep I have tried without voice recog and still nothing

when I use serial montior from the arduino IDE itself it works????

yep I have tried without voice recog and still nothing

Please post complete python code without the voice recognition which runs and sends a value, and some complete Arduino code which does not recognize the value sent. If you have two complete pieces of code which replicate the problem your issue can be resolved.

As I said, I have never been able to duplicate your issue with different snippets you have supplied, and I can send and receive data between pyserial and an Arduino without issue.

If you can run code on the Arduino with input from the serial monitor, you can run code with input from pyserial over usb.

Plain Python code:

[color=#bbbbbb][color=#f92aad]import[/color] serial

[color=#f92aad]def[/color] [color=#36f9f6]lights[/color] ():

    ser [color=#ffffff]=[/color] serial.[color=#36f9f6]Serial[/color]([color=#8a2dc0]'COM3'[/color], [color=#8a2dc0]9600[/color]) [color=#495495][i]# Establish the connection on a specific port[/i][/color]
        
    ser.[color=#36f9f6]write[/color]([color=#f92aad]b[/color][color=#8a2dc0]'b'[/color])
[color=#36f9f6]lights[/color]()[/color]

Plain Arduino code:

#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 300 

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 7
#define CLOCK_PIN 13

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() { 
	Serial.begin(57600);
	Serial.println("resetting");
	LEDS.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
	LEDS.setBrightness(84);
}

void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }

void loop() { 
	static uint8_t hue = 0;
	Serial.print("x");
	// First slide the led in one direction
	for(int i = 0; i < NUM_LEDS; i++) {
		// Set the i'th led to red 
		leds[i] = CHSV(hue++, 255, 255);
		// Show the leds
		FastLED.show(); 
		// now that we've shown the leds, reset the i'th led to black
		// leds[i] = CRGB::Black;
		
		// Wait a little bit before we loop around and do it again
	
	}
	Serial.print("x");

	// Now go in the other direction.  
	for(int i = (NUM_LEDS); i >= 0; i--) {
		// Set the i'th led to red 
		leds[i] = CHSV(hue++, 255, 255);
		// Show the leds
		FastLED.show();
		// now that we've shown the leds, reset the i'th led to black
		// leds[i] = CRGB::Black;
		
		// Wait a little bit before we loop around and do it again

	}
}

Can you please post the python code as plain text in code tags without all the color statements. Thanks.

There is nothing in the posted arduino code which reads from Serial.

python:

import serial

def lights ():

    ser = serial.Serial('COM3', 9600) # Establish the connection on a specific port
        
    ser.write(b'b')
lights()

Arduino:

void loop() {
    static uint8_t hue = 0;

 if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    if (incomingByte == 'b'){
    while(1){

  for(int i = 0; i < NUM_LEDS; i++) {
    // Set the i'th led to red 
    leds[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show(); 
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    
    // Wait a little bit before we loop around and do it again
  
  }
  Serial.print("x");

  // Now go in the other direction.  
  for(int i = (NUM_LEDS); i >= 0; i--) {
    // Set the i'th led to red 
    leds[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    
    // Wait a little bit before we loop around and do it again
  }
  
}
    } }}

Here is the last Arduino code you supplied with incomingByte added as a variable so it will compile. I have added some additional Serial printing to confirm the python send.

#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 300

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 7
#define CLOCK_PIN 13

// Define the array of leds
CRGB leds[NUM_LEDS];

char incomingByte= 'z';

void setup() {
  Serial.begin(57600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
  LEDS.setBrightness(84);
}

void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }


void loop() {
  static uint8_t hue = 0;

  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    if (incomingByte == 'b') {
      Serial.println("received 'b' ");
      while (1) {

        for (int i = 0; i < NUM_LEDS; i++) {
          // Set the i'th led to red
          leds[i] = CHSV(hue++, 255, 255);
          // Show the leds
          FastLED.show();
          // now that we've shown the leds, reset the i'th led to black
          // leds[i] = CRGB::Black;

          // Wait a little bit before we loop around and do it again

        }
        Serial.println("x");
       
        // Now go in the other direction.
        for (int i = (NUM_LEDS); i >= 0; i--) {
          // Set the i'th led to red
          leds[i] = CHSV(hue++, 255, 255);
          // Show the leds
          FastLED.show();
          // now that we've shown the leds, reset the i'th led to black
          // leds[i] = CRGB::Black;

          // Wait a little bit before we loop around and do it again
        }
      }
    }
  }
}

Here is a working version of your python code. Open the serial port at the correct baud rate for the Arduino program(57600). Open it at the start of the program and give some time for the serial to connect. I have added some reading code to the python sketch so you can see the response for the Arduino as the serial monitor can not be open at the same time as the python connection.

import serial
import time

ser = serial.Serial('COM3', 57600, timeout=.1)
time.sleep(2) #allow time for Serial port to open


def lights ():
    ser.write(b'b')

lights()

while True:                
    #read echo back from arduino verify receipt of message
    #will show printing of x from arduino program
    data = ser.readline()[:-2] #the last bit gets rid of the new-line char
    if data:  
        print (data.decode())#eliminate the 'b' bytes indicator

Here is what appears in the python shell when running the code

resetting
received 'b' 
x
x
x

Thx sm it works

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.