Advertisement
vencinachev

Arduino-David

May 21st, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #define RELAY 4
  2. #define BUZZER 2
  3. int state = 0;
  4.  
  5. void setup() {
  6.   Serial.begin(9600);
  7.   pinMode(RELAY, OUTPUT);
  8.   pinMode(BUZZER, OUTPUT);
  9. }
  10.  
  11. void loop() {
  12.   int value = analogRead(A5);
  13.   float voltage = fmap(value, 0, 1023, 0, 5);
  14.   Serial.println(voltage);
  15.   if (state == 0){
  16.     if (voltage > 3){
  17.       digitalWrite(RELAY, HIGH);
  18.       digitalWrite(BUZZER, HIGH);
  19.       state = 1;
  20.     }
  21.   } else {
  22.     if (voltage < 2){
  23.       digitalWrite(RELAY, LOW);
  24.       digitalWrite(BUZZER, LOW);
  25.       state = 0;
  26.     }
  27.   }
  28.   delay(10);
  29. }
  30.  
  31. float fmap(float x, float in_min, float in_max, float out_min, float out_max)
  32. {
  33.  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement