简体   繁体   中英

controlling 8**3 rgb led cube by computer

I am now in process of creating led cube that should be controlled by arduino bud ewery arduino i know does not have large enough memory to do all functions I want (it would be capable only of algorytmical or little programs) So I am trying to process all date in my computer by Python and send it to arduino ewerything works just fine except when I make delay between serial writes smaller than 1 second cube should be 10 Hz to make some cool animations but 5 Hz would be good enough, but with my code I am only capable of 1 Hz

python:

import serial
import time

arduinoData = serial.Serial('com3', 9600)

time.sleep(2)

while 1:
    arduinoData.write(b'81')
    time.sleep(1)
    arduinoData.write(b'80')
    time.sleep(1)

arduino:

String serialData;
int data;
int pin, value

void setup() {
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}


void loop() {

  if(Serial.available() > 0){
    serialData = Serial.readString();
    pin = serialData.substring(0, 1).toInt();
    value = serialData.substring(1, 2).toInt();
    digitalWrite(pin, value);
  }
}

I want to read/send data throgh serial port faster

Note: I am testing it with 2 regural leds

In the Arduino code, you are using Serial.readString() which reads from the buffer during a preset timeout (default is 1000ms). See Serial.setTimeout() for more informations.

I recommend you to use something like this instead:

void loop()
{
    string data = "";
    while(Serial.available()) // Read the buffer until it's empty
    {
        data += Serial.read();
    }
    if(data.length()>0) // Process the data then
    {
        pin = data[0].toInt();
        value = data[1].toInt();
    }
}

I suggest you to always refer to the Arduino documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM