mc_workshop

Piezoelectric sensor

May 24th, 2019
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Piezoelectric sensor
  2. //by M.C. maggio 2019
  3. //This example code is in the public domain.
  4. #include <Servo.h>
  5. Servo servo;
  6. const int analogInPin1 = A0;
  7. const int analogInPin2 = A1;
  8. int sensorValue1 = 0;
  9. int sensorValue2 = 0;
  10. void setup()
  11. {
  12.   Serial.begin(9600);
  13.   servo.attach (3);
  14.   pinMode(7, OUTPUT);
  15.   pinMode(8, OUTPUT);
  16.   pinMode(9, OUTPUT);
  17. }
  18. void loop()
  19. {
  20.   sensorValue1 = analogRead(analogInPin1);
  21.   sensorValue2 = analogRead(analogInPin2);
  22.   Serial.println (" ");
  23.   Serial.print("sensor1  = ");
  24.   Serial.println(sensorValue1);
  25.   Serial.print("sensor2  = ");
  26.   Serial.println(sensorValue2);
  27.   delay(50);
  28.   //Piezo 1
  29.   if (sensorValue1 >= 1) //sensibility
  30.   {
  31.     digitalWrite(7, HIGH);
  32.     digitalWrite(9, HIGH);
  33.     servo.write (-80);
  34.     delay (400);
  35.   }
  36.   if (sensorValue1 < 1);
  37.   {
  38.     digitalWrite(7, LOW);
  39.     digitalWrite(10, LOW);
  40.   }
  41.   //Piezo 2
  42.   if (sensorValue2 >= 1)
  43.   {
  44.     digitalWrite(8, HIGH);
  45.     digitalWrite(9, HIGH);
  46.     servo.write (+80);
  47.     delay (400);
  48.   }
  49.   if (sensorValue2 < 1);
  50.   {
  51.     digitalWrite(8, LOW);
  52.     digitalWrite(9, LOW);
  53.   }
  54. }
RAW Paste Data