I cant read bytes from Python

I am rendering an image for an 8x8 matrix using Pyserial libraries. The image appears for a very short time, and then disappears. But the image of the letter L appears again, which I set in the Loop() function. Next, I provide the Arduino code first, and then the Python code.

#include "LedControl.h"

LedControl lc=LedControl(12,10,11,1);

void setup() {
  lc.shutdown(0,false);
  lc.setIntensity(0,1);
  lc.clearDisplay(0);

  Serial.begin(9600);
}

void draw(byte* matrix) {
  lc.setRow(0,0,matrix[0]);
  lc.setRow(0,1,matrix[1]);
  lc.setRow(0,2,matrix[2]);
  lc.setRow(0,3,matrix[3]);
  lc.setRow(0,4,matrix[4]);
  lc.setRow(0,5,matrix[5]);
  lc.setRow(0,6,matrix[6]);
  lc.setRow(0,7,matrix[7]);
}

void clear() {
  lc.setRow(0,0,0);
  lc.setRow(0,1,0);
  lc.setRow(0,2,0);
  lc.setRow(0,3,0);
  lc.setRow(0,4,0);  
  lc.setRow(0,5,0);
  lc.setRow(0,6,0);
  lc.setRow(0,7,0);
}

byte data[8] = {B00000000,B00100000,B00100000,B00100000,B00100000,B00100000,B00111100,B00000000};

void loop() {
  if (Serial.available() >= 8) {
      for (int i = 0; i < 8; i++) {
        data[i] = Serial.read();
      }
    } else if (Serial.available() > 0) {
      Serial.flush();
    }
    draw(data);
    Serial.println(data[2], BIN);
    delay(450);
}
import serial
import time

ser = serial.Serial('COM3', 9600, timeout=1)
time.sleep(2)

ser.write(bytes([0b00001111, 0b00000111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b00001111]))

time.sleep(5)

ser.close()

Why does the image not remain on the matrix? I am using MAX7219 driver

How do you power your matrix? It sounds like your arduino is rebooting, possibly due to excessive power draw if you are trying to power your matrix from the arduino.

Also,

is not doing what you think. Serial.flush() blocks until the Tx buffer is empty. It does not empty the Rx buffer. You have to read any excessive bytes to empty the Tx buffer.

Here is how I flush the input buffer. It is for an ESP8266 so you may want to make inbuffer[100] smaller on an AVR processor.

// Flush serial input
void flushInput() {
  char inbuffer[100];
  size_t bytesRead;
  do {
    bytesRead = Serial.readBytes(inbuffer, sizeof(inbuffer));
  } while (bytesRead > 0); 
}
#include "LedControl.h"

LedControl lc = LedControl(12, 10, 11, 1);

void setup() {
  lc.shutdown(0, false);
  lc.setIntensity(0, 1);
  lc.clearDisplay(0);

  Serial.begin(9600);
  while (Serial.available() > 0) Serial.read();
}

void draw(byte* matrix) {
  lc.setRow(0, 0, matrix[0]);
  lc.setRow(0, 1, matrix[1]);
  lc.setRow(0, 2, matrix[2]);
  lc.setRow(0, 3, matrix[3]);
  lc.setRow(0, 4, matrix[4]);
  lc.setRow(0, 5, matrix[5]);
  lc.setRow(0, 6, matrix[6]);
  lc.setRow(0, 7, matrix[7]);
}

void clear() {
  lc.setRow(0, 0, 0);
  lc.setRow(0, 1, 0);
  lc.setRow(0, 2, 0);
  lc.setRow(0, 3, 0);
  lc.setRow(0, 4, 0);
  lc.setRow(0, 5, 0);
  lc.setRow(0, 6, 0);
  lc.setRow(0, 7, 0);
}

void loop() {
  static byte data[8] = {B00000000, B00100000, B00100000, B00100000, B00100000, B00100000, B00111100, B00000000};
  draw(data);
  Serial.println(data[2], BIN);
  delay(50);

  if (Serial.available() > 7) {
    for (int i = 0; i < 8; i++) {
      data[i] = Serial.read();
    }
  }
}

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