0
votes

I'm new to Arduino, and I'm trying to make a LED strip controller. I'm sending the serial data with Python to the Arduino. I use three Scales to control the LEDs color.

My questions are:

  • Should I send the color bytes like in Python sample code, or should I send the color bytes separatly or any otherway? The sent byte looks like this: b'255 32 28 '.

  • How can I convert this byte to separate integers or to a list? For example: b'255 32 28 ' , to int list[4]={255,32,28}

Python sample code:

from tkinter import *
import serial
import serial.tools.list_ports as ports

master = Tk()
master.geometry('400x300')

for ee in list(ports.comports()):
                if ee.serial_number=='557363134383519112E0':
                    usb=ee.device
ser=serial.Serial(usb,baudrate=9600)

def getThrottle():
    data=str(r.get())+' '+str(g.get())+' '+str(b.get())+' '
    data=bytes(str(data),'utf8')
    ser.write(data)

r = Scale(master,from_=255,to=0)
r.place(x=50,y=100)

g = Scale(master, from_=255, to=0)
g.place(x=150,y=100)

b=Scale(master, from_=255, to=0)
b.place(x=250,y=100)

gomb=Button(master,command=getThrottle,text='Send to led strip')
gomb.place(x=150,y=250)

master.mainloop()

Arduino sample code:

#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, 12, NEO_GRB + NEO_KHZ800);

String inByte;
char str;
int sa[4], r=0, t=0;

void setup() 
{
  Serial.begin(9600);
  strip.begin();
  strip.show();
}

void loop() {
  if (Serial.available() > 0)
  {  
       inByte = Serial.readString();  
       int str_len = inByte.length() + 1; 
       char char_array[str_len];
       inByte.toCharArray(char_array, str_len);
           for (int i=0; i < inByte.length(); i++)
  { 
   if(inByte.charAt(i) == ' ') 
    { 
      sa[t] = inByte.substring(r, i).toInt(); 
      r=(i+1); 
      t++; 
    }
   }
  } 
  strip.setPixelColor(1, sa[0],sa[1],sa[2]); 
  strip.show();
}  

Any good suggestion?

1

1 Answers

1
votes

Right now you're sending this: RRR GGG BBB

I'd recommend adding some delimiters that would allow your Arduino to decode this easier. EG: &RRR GGG BBB!

When your arduino sees a "&", it knows to store data until it sees a "!". When it does, you know you've got a fully formed data set. From there, split the data on " ".