A simple sketch that engages the relay when the button is pressed. Can be used to turn on a light, radio, or other external device.
Hardware used:
- Button
- Relay
Code Used:
- digitalRead
- digitalWrite
#include <Servo.h>
#define ENCODER_A 14
#define ENCODER_B 15
#define ENCODER_PORT PINC
#define SWITCH 13
#define BUTTON 12
#define RGB_RED 11
#define RGB_GREEN 10
#define RGB_BLUE 9
#define LED 6
#define SERVO 5
#define PIEZO 3
#define RELAY 2
#define POT 2
#define HALL 3
#define THERMISTOR 4
#define PHOTOCELL 5
void setup()
{
pinMode(BUTTON, INPUT);
pinMode(RELAY, OUTPUT);
}
void loop()
{
digitalWrite(RELAY, digitalRead(BUTTON));
delay(10); // Checks the button state 100 times a second
}