SHARE
TWEET

Untitled

a guest Jun 19th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2.  
  3. Servo firstServo;
  4.  
  5. int pot1Pin = A0;
  6. int pos = 0;
  7. int angle = 0;
  8. int pot1value = 0; // This is the 10bit positive
  9.  
  10. int led[] = {10,11,2,13};
  11.  
  12. void setup(){
  13.  
  14.     firstServo.attach(9);
  15.     pinMode(pot1Pin, INPUT);
  16.     for(int i=0; i < 4; i++){
  17.     pinMode(led[i], OUTPUT);
  18.  
  19.     }
  20. Serial.begin(9600);    
  21. }
  22.  
  23.  
  24. void loop(){
  25.  
  26.    pot1value = analogRead(A0);
  27.    pos = potToServo(pot1value);
  28.    angle = potToDegrees(pot1value);
  29.  
  30.    firstServo.write(pos);
  31.  
  32.    Serial.println(angle);
  33.        Serial.println(pot1value);
  34.  
  35.    if(angle >= 0 && angle < 15){
  36.      digitalWrite(led[0], HIGH);
  37.      digitalWrite(led[1], LOW);
  38.      digitalWrite(led[2], LOW);
  39.      digitalWrite(led[3], LOW);
  40.      }
  41.    if(angle >= 15 && angle < 30){
  42.      digitalWrite(led[0], LOW);
  43.      digitalWrite(led[1], HIGH);
  44.      digitalWrite(led[2], LOW);
  45.      digitalWrite(led[3], LOW);
  46.      }
  47.    if(angle >= 30 && angle < 45){
  48.      digitalWrite(led[0], LOW);
  49.      digitalWrite(led[1], LOW);
  50.      digitalWrite(led[2], HIGH);
  51.      digitalWrite(led[3], LOW);
  52.      }
  53.    if(angle >= 45 && angle < 60){
  54.      digitalWrite(led[0], LOW);
  55.      digitalWrite(led[1], LOW);
  56.      digitalWrite(led[2], LOW);
  57.      digitalWrite(led[3], HIGH);
  58.      }
  59.    
  60. }
  61.  
  62. int potToServo(int potvalue){
  63.  
  64.     potvalue = round(potvalue * 0.24926686);
  65.  
  66.  
  67.   return potvalue;
  68.   }
  69.  int potToDegrees(int potvalue){
  70.   int output = 0;
  71.  
  72.     output = round(potvalue * 0.058651026);
  73.  
  74.  
  75.  return output;  
  76.  }
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top