hello everyone,
i have a arduino uno controlling 7 relays with 7 push buttons that all works fine except on start up i have to press each button twice to get them to work, after that they all work on the first press its just on the initial start up i have to press twice,
can anyone tell me why this is please,
novice so go easy with me (2.4 KB)
thanks
// Define pin numbers for relays and buttons
const int NUM_CHANNELS = 7;
const int relayPins[NUM_CHANNELS] = {2, 3, 4, 5, 6, 7, 8}; // Relay pins
const int buttonPins[NUM_CHANNELS] = {9, 10, 11, 12, 13, A0, A1}; // Button pins
// earth from relays to ground on arduino
// Variables to store button states
bool buttonStates[NUM_CHANNELS] = {0, 0, 0, 0, 0, 0, 0}; // Current button states
bool lastButtonStates[NUM_CHANNELS] = {0, 0, 0, 0, 0, 0, 0}; // Previous button states
bool relayStates[NUM_CHANNELS] = {0, 0, 0, 0, 0, 0, 0}; // Relay states
// Debounce variables
unsigned long lastDebounceTime[NUM_CHANNELS] = {0, 0, 0, 0, 0, 0, 0};
const unsigned long debounceDelay = 50; // Debounce time in milliseconds
void setup() {
// Initialize relay pins as outputs
for (int i = 0; i < NUM_CHANNELS; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW);
delay (1000);
digitalWrite(relayPins[i], HIGH);
}
// Initialize button pins as inputs with pull-up resistors
for (int i = 0; i < NUM_CHANNELS; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
// Optional: Initialize serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Check each button
for (int i = 0; i < NUM_CHANNELS; i++) {
// Read the button state
int reading = digitalRead(buttonPins[i]);
// If the button state changed, reset the debounce timer
if (reading != lastButtonStates[i]) {
lastDebounceTime[i] = millis();
}
// Check if enough time has passed since the last state change
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
// If the button state has changed
if (reading != buttonStates[i]) {
buttonStates[i] = reading;
// If the button is pressed (LOW due to pull-up resistor)
if (buttonStates[i] == LOW) {
// Toggle relay state
relayStates[i] = !relayStates[i];
digitalWrite(relayPins[i], relayStates[i]);
// Optional: Print relay status to Serial Monitor
Serial.print("Relay ");
Serial.print(i + 1);
Serial.print(" is now ");
Serial.println(relayStates[i] ? "ON" : "OFF");
}
}
}
// Save the button reading for next comparison
lastButtonStates[i] = reading;
}
}
since you're using the internal pull-up, won't the initial lastButtonState be HIGH?
i typically do
for (int i = 0; i < NUM_CHANNELS; i++) {
pinMode (buttonPins [i], INPUT_PULLUP);
lastButtonState [i] = digitalRead (buttonPins [i]);
}
i think your right, the relays im using are high when off low when on, ive just noticed when i press the button it says its on but its not, its back to front,
i was referring to the state of the button pin, not the relay, which may be different problem
it's often clearer to define pin states as On/Off
enum { Off = HIGH, On = LOW };
and
Serial.println (relayStates[i] == On ? "ON" : "OFF");
thanks Ive worked it out now, changed the last button state to high so now the relays are off and the button works first time, sorry it was an easy fault but just couldn't see it
You might click the Solved button on the post that gave you the solution. Later forum readers will see that the thread has an answer and gcjr will get some credit.
The forum itself serves as a community resource, many times answers are found here without a new thread repeating what's already posted.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.