Hello everybody,
Relatively new to C++ and Arduino, but have experience in Java. Helping my partner with a uni project that involves coding and as I have some experience, I'm helping her out. I'm working with an Arduino Uno R3 with two vibration sensors and a 24-LED NeoPixel in TinkerCAD.
The code below (as far as I am aware) checks every 100ms to detect if either one of two buttons, vibSensorL or vibSensorR (buttons are a substitute for vibration sensors) is pressed. If pressed, two lights on a 24-LED NeoPixel will illuminate. With each button press, the next two lights will illuminate in a clockwise fashion. However, the problem arises in that the button can be pressed and held down, causing two lights to be illuminated clockwise every 100ms. I'm attempting to make it so that no matter how long the button is pressed down, it will only light up two lights, and to illuminate the next two lights in the sequence, the button has to be depressed and repressed again. I've tried to make a boolean and change it to true after pressing the button, but the problem is it needs to be set to false again after the button is released (read the part below to see why I can't change the variable to false in an else statement).
In addition to this problem, it seems that the bottom if statement (or else if/else) does not run on its own after 10 seconds, however, this code is executed only when one of the buttons is pressed, even though it's not in the if statement that detects when a button is pressed?? This is starting to drive me insane, from my understanding, the bottom if statement should be run regardless of whether or not the button is pressed as it is in the loop() method and being checked every 100ms if the conditions are true?
#include <Adafruit_NeoPixel.h>
#define PIN 9
#define NUMPIXELS 24
Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int vibSensorLPin = 2;
int vibSensorRPin = 4;
int pressCount = -2;
uint32_t white = strip.Color(254, 254, 254);
uint32_t red = strip.Color(255, 0, 0);
uint32_t green = strip.Color(0, 255, 0);
unsigned long dangerTime = (10 * 1000); //max time that can elapse before NeoPixel turns red.
unsigned long timerCount = 0; //timer starts from 0ms.
void setup()
{
strip.begin();
strip.show();
pinMode(vibSensorLPin, INPUT);
pinMode(vibSensorRPin, INPUT);
Serial.begin(9600);
}
void loop()
{
delay(100);
timerCount = millis();
bool vibration = digitalRead(vibSensorLPin) || digitalRead(vibSensorRPin);
if(vibration)
{
//DETECTS EITHER BUTTON BEING PRESSED
if(pressCount <= 23)
{
pressCount = pressCount + 2;
}
if(pressCount >= 0 && pressCount <= 15)
{
strip.setPixelColor(pressCount, red);
strip.setPixelColor(pressCount + 1, red);
}
if(pressCount >= 16 && pressCount <= 23)
{
strip.setPixelColor(pressCount, green);
strip.setPixelColor(pressCount + 1, green);
}
strip.show();
}
else if((timerCount >= dangerTime) && pressCount <=15)
{
//MAKES ALL LEDs RED IF NOT ENOUGH VIBRATIONS ARE DETECTED AFTER 10 SECONDS.
strip.fill(red, 0, 24);
}
}
Any insight at all is appreciated since I have been struggling with this all evening.