Hi,
I’ve very recently got into coding and am doing a piece of it for my college project. I have attached an Adafruit Neo_Pixel Shield to an Arduino Uno and want the LEDs to flash the SOS sequence when the button is pushed, and to stop the sequence when the button is pushed a second time.
For some reason unknown to me, the hardware shows absolutely no sign of working when the button is pressed however I know that its the software incorrect as I’ve loaded other pieces of code onto it that are correct and it works just fine. The code I’m uploading is as follows:
#include <Adafruit_NeoPixel.h> //includes NeoPixel Shield Library
#define PIXELS 6 //sets pin 6 as routing for Neo_Pixels in strip definition of Neopixels
#define NUM_PIX 40
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIX, PIXELS, NEO_RGB + NEO_KHZ800); //defines parameters of neopixel shield
boolean Latch = true; //sets the latch norm as true
const int ButtonPin = 9; //sets pin number 9 as the pushbutton pin
const int PixPin = 6; //sets pin number 6 as the neopixel pin
volatile int ButtonState = 0; //sets button state norm as 0 (LOW)
void setup() {
strip.begin();
strip.show(); //initialise all pins to OFF
strip.setBrightness(100); //sets pixel brightness as 100%
pinMode(ButtonPin,INPUT); //sets button pin as an input
pinMode(PixPin, OUTPUT); //sets pixel pin as an output
attachInterrupt(digitalPinToInterrupt(ButtonPin),pixOFF,RISING); //creates interrupt so that 2nd press of button will turn all pixels off
}
void loop() {
strip.begin();
strip.show();
ButtonState = digitalRead(ButtonPin); //reads the state of button value
if (ButtonState = HIGH) {
if (Latch = true)
Latch = false; //turns latch to OFF if button is pressed and latch is already ON
else
Latch = true; //turns latch to ON if button is pressed and latch is already OFF
}
if (Latch = true)
SOS_Signal(); //plays SOS loop if latch is ON
else
pixOFF(); //keeps pixels OFF if latch is OFF
}
void pixOFF(){
int pix;
for(pix=0; pix<40; pix++){
strip.setPixelColor(pix, 0, 0, 0); //reverts all pixels back to OFF
}
}
void pixON(){
int pix;
for(pix=0; pix<40; pix++){
strip.setPixelColor(pix, 255, 0, 0); //turns on every pixel
}
}
void SOS_Signal() {
int shortdelay=300; //one time unit in morse code
int longdelay=900; //three time units in morse code
pixON();
delay(shortdelay);
pixOFF();
delay(shortdelay);
pixON();
delay(shortdelay);
pixOFF();
delay(shortdelay);
pixON();
delay(shortdelay);
pixOFF();
delay(longdelay); //3 dots (S) and a space
pixON();
delay(longdelay);
pixOFF();
delay(shortdelay);
pixON();
delay(longdelay);
pixOFF();
delay(shortdelay);
pixON();
delay(longdelay);
pixOFF();
delay(longdelay); //3 dashes (O) and a space
pixON();
delay(shortdelay);
pixOFF();
delay(shortdelay);
pixON();
delay(shortdelay);
pixOFF();
delay(shortdelay);
pixON();
delay(shortdelay);
pixOFF();
delay(longdelay); //3 dots (S) and a space
strip.show();
}
It’s quite messy but please could anyone help with this as I don’t know where I’ve gone wrong at all!
Thanks a lot,
NoobCoderTom