Hi there, I feel a bit silly for asking as I feel like this should be a very straightforward and simple change to make in comparison to what I needed help with earlier, but here I am anyway.
I have been working on a circuit that consists of six FSR sensors, a push button and an LED. With the push of the button, I can stop the recording of data; in the serial monitor, the lines would be blank, and the serial plotter would pause. Now I would like to add an LED so that I can see when the data is being recorded more visually.
The problem is that the LED state is reversed from what I would like it to do. Meaning that the LED is off when the data is being recorded and turning on when the recording stops. Is there a way to reverse the LED state? All attempts I have made to change the states have resulted in the LED not working at all. I have attached the current code and setup below.
Thank you in advance and have a lovely day!
// References
// Innitial starting code for reading the FSR's https://forum.arduino.cc/t/separating-fsr-values-and-labelling-graph/1275362
// Innitial starting code for including a LED and button https://roboticsbackend.com/arduino-turn-led-on-and-off-with-button/
// Own post on Arduino Forum https://forum.arduino.cc/t/arduino-only-showing-3-out-of-6-outputs-on-serial-plotter/1361263
// Innitial cirquit reference for the FSR's https://forum.arduino.cc/t/connecting-8-fsrs-to-arduino-mega/618577
// Innitial cirquit source for the LED and Button https://techexplorations.com/guides/arduino/sensors/button/
#define LED_PIN 7
#define BUTTON_PIN 8
// Defining pins
const byte PinFsr [] = {A0, A1, A2, A3, A4, A5}; // Location of the six FSR's
const byte Nfsr = sizeof(PinFsr); // Number of bytes occupied
byte prevButtonState = HIGH; // Button being pressed
byte buttonState = LOW; // Button not being pressed
byte lastButtonState = LOW;
byte loopRunning = LOW;
byte ledState = LOW;
unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;
void setup() {
Serial.begin (9600); // Begin serial communication at a baud rate of 9600
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
if(loopRunning) {
for (byte n = 0; n < Nfsr; n++) { // Reading data from all FSR's simultaneously
int fsr = analogRead (PinFsr [n]);
Serial.print (fsr);Serial.print(" ");
Serial.print (","); // Separate values with a comma
}
}
Serial.println (); // Display values on Serial Plotter and Serial Monitor
delay (500); // Delay 500 Milliseconds
if (millis() - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = millis();
lastButtonState = buttonState;
if (buttonState == LOW) {
ledState = (ledState == HIGH) ? LOW: HIGH;
digitalWrite(LED_PIN, ledState);
}
}
}
buttonState = digitalRead(BUTTON_PIN); // If the current button state differs from previous, toggle other way
if((buttonState==LOW) && (prevButtonState==HIGH)) {
loopRunning = !loopRunning;
}
prevButtonState=buttonState;
pinMode(LED_PIN, OUTPUT); // LED works as an output
pinMode(BUTTON_PIN, INPUT); // Button works as an imput
}
