mpmcws

Inseguitore solare # 1 - test3 -

Jul 9th, 2018
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Inseguitore solare / Cerani - Tolentino luglio 2018
  2. // code open source
  3. #include <Servo.h>
  4. int ServoHorizontalPin = 10;
  5. int ServoVerticalPin = 9;
  6. int HorizontalPotPin = A1;
  7. int VerticalPotPin = A0;
  8. int ServoH_a = 120;
  9. int ServoH_b = 30;
  10. Servo HorizontalServo;
  11. Servo VerticalServo;
  12. #define Tolerance 10
  13. int HorizontalPotValue;
  14. int oldHorizontalPotValue;
  15. int VerticalPotValue;
  16. int oldVerticalPotValue;
  17. int HorizontalServoPosition;
  18. int VerticalServoPosition;
  19. void setup()
  20. {
  21.   HorizontalServo.attach(ServoHorizontalPin);
  22.   VerticalServo.attach(ServoVerticalPin);
  23.   HorizontalServo.write (90);
  24.   VerticalServo.write (90);
  25. }
  26. void loop()
  27. {
  28.   HorizontalPotValue  = averageAnalogRead(HorizontalPotPin, 10);
  29.   VerticalPotValue  = averageAnalogRead(VerticalPotPin, 10);
  30.   if ( abs(HorizontalPotValue - oldHorizontalPotValue) > Tollerance) {
  31.     oldHorizontalPotValue = HorizontalPotValue;
  32.     HorizontalServoPosition  = map(HorizontalPotValue, 0, 1023, ServoH_a , ServoH_b);
  33.     HorizontalServo.write(HorizontalServoPosition);
  34.   }
  35.   if ( abs(VerticalPotValue  - oldVerticalPotValue  ) > Tollerance) {
  36.     VerticalServoPosition  = map(VerticalPotValue, 0, 1023, ServoH_a , ServoH_b);
  37.     VerticalServo.write(VerticalServoPosition);
  38.   }
  39.   delay(50);
  40. }
  41. long averageAnalogRead(const byte AnChannel, const byte Num) {
  42.   long _val = 0;
  43.   for (int n = 0; n < Num; n++) {
  44.     _val += analogRead(AnChannel);
  45.   }
  46.   return _val / Num;
  47. }
RAW Paste Data