I have an arduino nano which is hooked up to a distance sensor in analog pins A5 and A6, a 18-led strip on D6, and Rx and Tx hooked up to a raspberry pi. The raspberry pi sends colours to the arduino and the arduino dims to the colour in a non-blocking manner. I have a problem with the arduino freezing when fading to the sent rgb values, which usually happens when dimming to 142,142,142 (white). The arduino will freeze for 5-10 seconds and then reset. Another solution which I found is to pull out the power from the LED strip and put it back in, which will cause the arduino to immediately work again. Is this a problem with serial or electricity?
It's worth mentioning that the pi sends to the arduino every 50ms.
LED strips freezing with arduino nano
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: LED strips freezing with arduino nano
sounds like a software issue with your sketch. Can you post it?
- pacebrian0
- Posts: 9
- Joined: Fri Jul 21, 2017 10:13 am
Re: LED strips freezing with arduino nano
As a brief explanation, I have a raspberry pi sending data to the arduino (a string containing sensor data, and rgb values, which are correctly parsed by the arduino). The arduino adds sensor data to the string and sends it back to the pi, or a second arduino. The arduino parses the rgb values and lights up the neopixel strip with that colour. The code runs without any hitches when I comment out strip.show(), so probably it's something with timings.
Also I noticed that when the arduino freezes, the L lights starts flickering alot.
Also I noticed that when the arduino freezes, the L lights starts flickering alot.
Code: Select all
#include <LiquidCrystal.h>
#include <stdio.h>
#include <stdlib.h>
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <VL53L0X.h>
VL53L0X sensor;
#include <elapsedMillis.h>
elapsedMillis timeElapsed; //declare global if you don't want it reset every time loop runs
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(18, PIN, NEO_GRB + NEO_KHZ800);
#define LONG_RANGE
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char tmp[2] = "";
const int numChars = 500;
char receivedChars[numChars];
int init_index = 0;
int permanent_rgb[3] = {0,0,0};
int signal_rgb[3] = {0, 0, 0};
int sensor_location = 0;
bool chk = false;
boolean newData = false;
int strLength = 0;
int counter = 0;
bool mutex = true;
void setup() {
timeElapsed = 0;
strip.begin();
//strip.show() causes I/O problems with serial -> interrupts
strip.show(); // Initialize all pixels to 'off'
Serial.begin(115200);
Wire.begin();
sensor.init();
sensor.setTimeout(500);
sensor.setMeasurementTimingBudget(33000);
sensor.setSignalRateLimit(0.1 * 65536);
sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
sensor.startContinuous();
}
void changeatonce(){
int count = 0;
int diff_rgb = 0;
for (int i = 0; i < 3; i++) {
diff_rgb = signal_rgb[i] - permanent_rgb[i];
if (diff_rgb !=0 ) {
permanent_rgb[i]=signal_rgb[i];
}
else count++;
}
if (count >= 3)return;
else{
for (int j = 0; j < 18; j++) {
strip.setPixelColor(j, permanent_rgb[0], permanent_rgb[1], permanent_rgb[2]);
}
if(!Serial.available()) strip.show(); //this is breaking my signal sent back
}
}
void changecolours() {
byte count = 0;
int diff_rgb = 0;
for (int i = 0; i < 3; i++) {
diff_rgb = signal_rgb[i] - permanent_rgb[i];
if (diff_rgb > 0) {
if (permanent_rgb[i]<254){
permanent_rgb[i] += 1;
}
else count++;
}
else if (diff_rgb < 0) {
if (permanent_rgb[i]>=0){
permanent_rgb[i] -= 1;
}
else count++;
}
else count++;
}
if (count >= 3)return;
else{
for (int j = 0; j < 18; j++) {
strip.setPixelColor(j, permanent_rgb[0], permanent_rgb[1], permanent_rgb[2]);
}
lcd.clear();
lcd.print(permanent_rgb[0]);
lcd.print(":");
lcd.print(permanent_rgb[1]);
lcd.print(":");
lcd.print(permanent_rgb[2]);
strip.show(); //this is breaking my signal sent back
}
}
void loop() {
recvWithStartEndMarkers(); //parse signal
if (newData == true) {
signalBuild(); //send new signal
newData = false;
}
if (timeElapsed > 5) {
//this is breaking my signal sent back
changecolours();
timeElapsed = 0;
}
}
void parseSign() { //parse the sign into a string
//bool chk = false;
sensor_location = init_index;
}
void buildIndex() { //building of index into new sign
char tmp[3];
tmp[0] = receivedChars[0];
tmp[1] = receivedChars[1];
init_index = atoi(tmp);
itoa(init_index + 1, tmp, 10);
if ((init_index + 1) < 10) {
receivedChars[0] = '0';
receivedChars[1] = tmp[0];
} else {
receivedChars[0] = tmp[0];
receivedChars[1] = tmp[1];
}
//Serial.print(sign);
}
void buildSensor() { //building of sensor values into new sign
char sens;
int newValue;
int range = sensor.readRangeContinuousMillimeters();
//newValue = range;
newValue = range / 26 + 97;
//bring newValue to ASCII/Alphabet character
sens = newValue;
receivedChars[sensor_location] = sens;
}
void readRGB() {
while (receivedChars[sensor_location] != '%') {
sensor_location++;
}
sensor_location += 3 * init_index + 1;
for (int i = 0; i < 3; i++) {
if ((int)receivedChars[sensor_location + i] * 2 >= 0 && (int)receivedChars[sensor_location + i] * 2 < 255)
signal_rgb[i] = ((int)receivedChars[sensor_location + i] * 2) % 255;
//Serial.print((int)receivedChars[sensor_location + i] * 2);
}
//rgb_values[sensor_location]='\0';
}
void signalBuild() {
if (strLength <= 3) {
buildIndex();
Serial.print("<");
Serial.print(receivedChars);
Serial.print(">");
Serial.flush();
}
else {
parseSign(); //parse signal string
//buildIndex(); //build new index
buildSensor(); //read and build new sensor
Serial.print("<");
Serial.print(receivedChars);
Serial.print(">");
Serial.flush();
readRGB(); //read RGB values
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static int ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
strLength = ndx;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
- pacebrian0
- Posts: 9
- Joined: Fri Jul 21, 2017 10:13 am
Re: LED strips freezing with arduino nano
I have found a solution to the above, is that the strip.show() is called before the transmission so that the raspberry pi does not send back until the neopixels are done. The dimming is now happening every serial event, which happens around every 50ms, which provides a good transition.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: LED strips freezing with arduino nano
Glad to hear you figured it out!
Please be positive and constructive with your questions and comments.