I need to create an iterative light sequence. This project will be interfacing with a camera via Python hence the need to use Python and have the Arduino act as a NeoPixel driver.
I am trying to configure the Arduino to read the serial port and then run the code if it is flagged to do so. However, I am unsure on what's going wrong here - it simply won't run when inside the Serial.available() > 0
loop.
Arduino UNO Code:
// NeoPixel program showing use of the WHITE channel for RGBW
// pixels only (won't look correct on regular RGB NeoPixel strips).
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN 6
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 6
// NeoPixel brightness, 0 (min) to 255 (max)
#define BRIGHTNESS 10 // Set BRIGHTNESS to about 1/5 (max = 255)
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
void setup() {
// put your setup code here, to run once:
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(BRIGHTNESS);
// Open serial connection.
Serial.begin(9600);
Serial.write('1');
}
void loop() {
// put your main code here, to run repeatedly:
// colorWipe(strip.Color( 0, 0, 0, 255), 50); // True white(not RGB white)
if(Serial.available() > 0){
colorWipe(strip.Color( 0, 0, 0, 255), 50); // True white(not RGB white)
Serial.write('0');
}
}
// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.clear(); // Clear after every iteration.
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(100); // Pause for a moment
}
}
Python Code
#Import the necessary packages.
import serial
import time
#Establish a serial connection with the Arduino.
arduino = serial.Serial(port='COM5', baudrate=9600, timeout=0.1)
#Boolean variable that will represent whether or not
#we are connected
connected = False
#Loop until we are connected
while not connected:
serial_in = arduino.read()
connected = True
#Tell the arduino to begin the light sequence.
arduino.write(1)
#Wait until the arduino tells us it is finished.
while arduino.read() == '1':
arduino.read()
#Close the port and end the program
arduino.close()