Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define RELAY 4
- #define BUZZER 2
- int state = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(RELAY, OUTPUT);
- pinMode(BUZZER, OUTPUT);
- }
- void loop() {
- int value = analogRead(A5);
- float voltage = fmap(value, 0, 1023, 0, 5);
- Serial.println(voltage);
- if (state == 0){
- if (voltage > 3){
- digitalWrite(RELAY, HIGH);
- digitalWrite(BUZZER, HIGH);
- state = 1;
- }
- } else {
- if (voltage < 2){
- digitalWrite(RELAY, LOW);
- digitalWrite(BUZZER, LOW);
- state = 0;
- }
- }
- delay(10);
- }
- float fmap(float x, float in_min, float in_max, float out_min, float out_max)
- {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement