WS2812 & SW 420 code help

Hello, first & foremost, apologies if I'm not using this forum correctly. I tried finding a 'user guide' for it before I went for it.

Now, my question;

I'm trying to make a WS2812 8 LED strip change colors with a pushbutton, (thats been achieved), WHILE it's on one particular color, I want it to flash another color IF it detects a vibration form a SW 420 sensor. I can make the sensor work by itself.. though trying to marry the codes together isn't working out.. I've tried a few different ways.. so far this seems to be the most logical, to me, though I cant get the vibe sensor to register a HIGH input while connected and coded with the rest of it.. What am I missing??.. here's what I've come up with so far...

#include <FastLED.h>
#define Button 2
int Count = 0;
#define NUM_LEDS 8
#define LED_PIN 3

CRGB leds[NUM_LEDS];

const int sensorPin = 9; // SW 420 Vibration sensor
int sensorState = 0;

int vibeState = (digitalRead(sensorPin));


void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(5);
pinMode (Button, INPUT);
Serial.begin(9600);
pinMode(sensorPin,INPUT);

           Serial.println(vibeState);
           delay(500);

           
}

void loop() {
         


if(digitalRead(Button)==HIGH){
    Count ++;
    if(Count == 1 ){
  
  fill_solid (leds, NUM_LEDS, CRGB::Green);
FastLED.show();
delay(250);
             

             switch (vibeState) 
             {case 1:
             for (int i = 0; i < 5; i++) {
             fill_solid(leds,NUM_LEDS, CRGB::Orange); 
             FastLED.show();  
             delay(100);  
             fill_solid(leds, NUM_LEDS, CRGB::Black); 
             FastLED.show();
            delay(100);
            case 0: 
            break;
            default:if(digitalRead(Button)==HIGH){
    Count ++;
    if(Count == 1 ){
  
  fill_solid (leds, NUM_LEDS, CRGB::Green);
FastLED.show();
delay(250);}

  
 }}}}

else if(Count == 2){
fill_solid (leds, NUM_LEDS, CRGB::Red);
FastLED.show();
delay(250);}

else if (Count == 3){
fill_solid (leds,NUM_LEDS, CRGB::Blue);
FastLED.show();
delay(250);}

else if (Count == 4){
fill_solid (leds, NUM_LEDS, CRGB::White);
FastLED.show();
delay(250);}

else if (Count == 5){
fill_solid (leds, NUM_LEDS, CRGB::Black);
FastLED.show();
delay(250);}
  }
}

Again, I would like to apologize for posting this incorrectly..

  • Use Ctrl T or CMD T to format your code .

After you format your code so it is readable, look for the pinned post in most every category that tells you how to get the most from the forum. You need to pay attention to state machines or what is called 'doing many things at once'

If you don't read the sensor anywhere in your loop, how could the state change...

OK, so your saying there needs to be a sensor reading outside/before the switch statement? I was assuming that the switch statement would be read in sequence with the standard loop.. If I'm understanding correctly.. the switch function will only take place after a change in the sensor is read in the loop first...

If you don't read the sensor, vibeState will never change...

Add this on your loop:
vibeState = (digitalRead(sensorPin));

I do have something similar, I have
int vibeState = (digitalRead(sensorPin));
Though I have it set before the void setup.. I'm thinking I should put it in the void loop? I assume I'll have to put it directly after the if(digitalRead(Button)==HIGH){ Count ++;
?

Yes you have in setup and it reads the sensor once there (it should be located after you define sensor pin as input).

But you need to read the sensor on your loop to detect changes.
You can place it in your loop to the most convenient place, before switch (vibeState)

Got it! Thanks for he confirmation! As well as the insight to placing it after the definitions.. I understand now, the section before the set up IS sequential. Thanks! I'll move some things around!

Found it! Thank You - Enlightening!

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