hello, so Im trying to use my relay to be toggled on and off with a switch, when the the circuit is powered for the first time, I want the a lightbulb to turn on, and with the push of a button, I want the lightbulb to turn off and activate the LEDs and the buzzer thats also connected to the relay. (not sure if this is possible but I need to make it work somehow. My math grade(somehow) depends on this. Thanks in advance! Im going to paste my current code. Right now, the button doesnt do anything other than dimming the light on the relay when activated but it used to toggle it but I messed it up somehow.
Thanks in advance.
#define LED_PIN_1 7
#define LED_PIN_2 8
#define LED_PIN_3 9
#define LED_PIN_4 10
#define LED_PIN_5 11
#define LED_PIN_6 12
#define BUZZER_PIN 6
#define BUTTON_PIN 2
#define RELAY1_PIN 3
#define RELAY2_PIN 4
const int LED_COUNT = 12;
const int LED_ON_TIME = 750;
const int LED_OFF_TIME = 750;
const int BUZZER_FREQUENCY = 523;
const int BUZZER_DURATION = 500;
const int RELAY1_ON_TIME = 1000;
const int RELAY1_OFF_TIME = 0;
const int RELAY2_ON_TIME = 0;
const int RELAY2_OFF_TIME = 1000;
const int BUTTON_DEBOUNCE_TIME = 50;
const int MIDI_NOTE_C4 = 60;
const int MIDI_NOTE_G3 = 55;
const int MIDI_NOTE_A3 = 57;
const int MIDI_NOTE_B3 = 59;
const int MIDI_NOTE_COTTON_EYE_JOE[] = {
MIDI_NOTE_C4, MIDI_NOTE_G3, MIDI_NOTE_G3, MIDI_NOTE_A3, MIDI_NOTE_G3,
0, MIDI_NOTE_B3, MIDI_NOTE_C4,
MIDI_NOTE_C4, MIDI_NOTE_G3, MIDI_NOTE_G3, MIDI_NOTE_A3, MIDI_NOTE_G3,
0, MIDI_NOTE_B3, MIDI_NOTE_C4
};
const int MIDI_DURATION_COTTON_EYE_JOE[] = {
4, 4, 4, 4, 4,
4, 4, 4,
4, 4, 4, 4, 4,
4, 4, 4
};
const int NUM_NOTES_COTTON_EYE_JOE = sizeof(MIDI_NOTE_COTTON_EYE_JOE) / sizeof(int);
void setup() {
pinMode(LED_PIN_1, OUTPUT);
pinMode(LED_PIN_2, OUTPUT);
pinMode(LED_PIN_3, OUTPUT);
pinMode(LED_PIN_4, OUTPUT);
pinMode(LED_PIN_5, OUTPUT);
pinMode(LED_PIN_6, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RELAY1_PIN, OUTPUT);
pinMode(RELAY2_PIN, OUTPUT);
for (int i = 0; i < LED_COUNT; i++) {
digitalWrite(LED_PIN_1 + i / 2, LOW);
}
digitalWrite(RELAY1_PIN, LOW);
digitalWrite(RELAY2_PIN, LOW);
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
while (digitalRead(BUTTON_PIN) == LOW) {}
digitalWrite(RELAY1_PIN, !digitalRead(RELAY1_PIN));
delay(50);
}
}