I am working on a project about controlling led bands with brainwave values. When I add the led's program into the if, the program goes into a dead loop.
void loop()
{
setColor();
read_serial_data();
if(attention>10&attention<50){
for (int i=0; i < NUMPIXELS; i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));
// This sends the updated pixel color to the hardware.
pixels.show();
// Delay for a period of time (in milliseconds).
delay(delayval);
}
}
}
How should I go about changing this code?
Thanks
kolaha
May 31, 2022, 11:08am
2
void loop(){
setColor();
read_serial_data();
if (attention > 10 & attention < 50) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));
}
pixels.show();
delay(delayval);
}
}
if(attention>10&attention<50)
& does a bitwise AND
&& does a logical AND
I suspect that you are using the wrong one
What does that look like?
What does setColor() do?
How long is 'delayval'?
Hi,johnwasser
This is my entire program. The program stops when the if statement is met and the serial monitor no longer prints any data.
#include <Adafruit_NeoPixel.h>
#define PIN 7 // input pin Neopixel is attached to
#define NUMPIXELS 12 // number of neopixels in strip
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 100; // timing delay in milliseconds
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
#define BAUDRATE 57600
#define DEBUGOUTPUT 0
int generatedChecksum = 0;
byte checksum = 0;
byte payloadLength = 0;
byte payloadData[32] = {0};
byte signalquality = 0;
byte attention = 0;
byte meditation = 0;
void setup()
{
Serial.begin(BAUDRATE);
pixels.begin();
}
byte ReadOneByte()
{
int ByteRead;
while(!Serial.available());
ByteRead = Serial.read();
return ByteRead;
}
//读取串口数据
void read_serial_data()
{
if(ReadOneByte() == 0xAA)
{
if(ReadOneByte() == 0xAA)
{
payloadLength = ReadOneByte();
if(payloadLength == 0x20)
{
generatedChecksum = 0;
for(int i = 0; i < payloadLength; i++)
{
payloadData[i] = ReadOneByte();
generatedChecksum += payloadData[i];
}
checksum = ReadOneByte();
generatedChecksum = (~generatedChecksum)&0xff;
if(checksum == generatedChecksum)
{
signalquality = 0;
attention = 0;
signalquality = payloadData[1];
attention = payloadData[29];
meditation = payloadData[31];
#if !DEBUGOUTPUT
Serial.print("SignalQuality: ");
Serial.print(signalquality, DEC);
Serial.print("Attation: ");
Serial.print(attention, DEC);
Serial.print("Meditation: ");
Serial.print(meditation, DEC);
Serial.print("\n");
#endif
}
}
}
}
}
void loop(){
setColor();
read_serial_data();
if (attention > 10 & attention < 50) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));
}
pixels.show();
delay(delayval);
}
}
void setColor(){
redColor = random(0, 255);
greenColor = random(0,255);
blueColor = random(0, 255);
}
Did you do as suggested in post #3 ?
haorann:
The program stops
Put a serial print at the bottom of loop() and perhaps elsewhere to see if it actually "stops".
haorann:
if (attention > 10 & attention < 50) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));
}
pixels.show();
delay(de
I have tried to replace for loop with serial print,like this:
if (attention > 10 & attention < 50) {
serial.print("test done")
}
it works. So I guess the problem is that the for loop is used in the if statement
I see that you still have a single & in that if statement. Is that deliberate ?
You still have this mistake:
Change that line to:
if (attention > 10 && attention < 50) {
system
Closed
November 27, 2022, 8:13pm
10
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.