bool magnetActive = false; // false = OFF, true = ON
void setup() {
pinMode(2, INPUT_PULLUP); // Start Button
pinMode(5, OUTPUT); // Relay for the Electromagnet
// SAFE START: Set to HIGH so the magnet is OFF when the Arduino turns on
digitalWrite(5, HIGH);
}
void loop() {
// Check if the button is pressed
if (digitalRead(2) == LOW) {
magnetActive = !magnetActive; // Toggle the state (flips between true and false)
if (magnetActive == true) {
digitalWrite(5, LOW); // Turn the magnet ON (Grabs the scrap)
}
else {
digitalWrite(5, HIGH); // Turn the magnet OFF (Drops the scrap into the bin)
}
delay(400); // Small delay to prevent bouncing
while(digitalRead(2) == LOW); // Wait until the user releases the button
}
}