Advertisement
Guest User

w3bc4m p4n-tilt (arduino side code)

a guest
Jul 31st, 2010
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.17 KB | None | 0 0
  1.  int servoPin  =  2;    
  2.  int servoPin2 =  8;
  3.  int minPulse     =  600;  
  4.  int maxPulse     =  2400;
  5.  int turnRate     =  30;  
  6.  int refreshTime  =  60;  
  7.  int pulseWidth;
  8.  int pulseWidth2;
  9.  int moveServo;          
  10.  long lastPulse  = 0;
  11.  long lastPulse2 = 0;
  12.    
  13.    
  14.  void setup() {  
  15.    pinMode(servoPin, OUTPUT);  
  16.    pinMode(servoPin2,OUTPUT);
  17.    Serial.begin(9600);  
  18.    Serial.println("Welcome to W3bc4m P4n-Tilt ");  
  19.    Serial.println("Initializing...");  
  20.    Serial.println();  
  21.  }  
  22.    
  23.  void loop() {  
  24.  
  25.    if (Serial.available() > 0) {  
  26.      
  27.      moveServo = Serial.read();  
  28.      
  29.    
  30.      
  31.      if (moveServo == 50) {
  32.           pulseWidth = pulseWidth - turnRate;
  33.           if (millis() - lastPulse >= refreshTime) {
  34.           digitalWrite(servoPin, HIGH);
  35.           delayMicroseconds(pulseWidth);
  36.           digitalWrite(servoPin,LOW);
  37.          
  38.         }
  39.         lastPulse = millis();  
  40.       }  
  41.      if (moveServo == 51) {
  42.          pulseWidth = pulseWidth + turnRate;
  43.          if (millis() - lastPulse >= refreshTime) {
  44.          digitalWrite(servoPin, HIGH);
  45.          delayMicroseconds(pulseWidth);
  46.          digitalWrite(servoPin,LOW);
  47.            
  48.         }
  49.         lastPulse = millis();
  50.      }  
  51.      if (moveServo == 52){
  52.          pulseWidth2 = pulseWidth2 - turnRate;
  53.          if (millis() - lastPulse2 >= refreshTime) {
  54.          digitalWrite(servoPin2, HIGH);
  55.          delayMicroseconds(pulseWidth2);
  56.          digitalWrite(servoPin2,LOW);
  57.          }  
  58.          lastPulse2 = millis();
  59.       }
  60.       if (moveServo == 53){
  61.          pulseWidth2 = pulseWidth2 + turnRate;
  62.          if (millis() - lastPulse2 >= refreshTime) {
  63.          digitalWrite(servoPin2, HIGH);
  64.          delayMicroseconds(pulseWidth2);
  65.          digitalWrite(servoPin2,LOW);
  66.          
  67.          
  68.       }
  69.        lastPulse2 = millis();
  70.    }
  71.      
  72.        
  73.      
  74.      if (pulseWidth > maxPulse) {
  75.            pulseWidth = maxPulse;
  76.            if (millis() - lastPulse >= refreshTime){
  77.            digitalWrite(servoPin, HIGH);
  78.            delayMicroseconds(pulseWidth);
  79.            digitalWrite(servoPin,LOW);  
  80.            
  81.         }
  82.          lastPulse = millis();
  83.        }  
  84.      if (pulseWidth < minPulse) {
  85.            pulseWidth = minPulse;
  86.            if (millis() - lastPulse >= refreshTime){
  87.            digitalWrite(servoPin, HIGH);
  88.            delayMicroseconds(pulseWidth);
  89.            digitalWrite(servoPin,LOW);
  90.              
  91.         }
  92.         lastPulse = millis();  
  93.       }
  94.         if (pulseWidth2 > maxPulse) {
  95.            pulseWidth2 = maxPulse;
  96.            if (millis() - lastPulse2 >= refreshTime){
  97.            digitalWrite(servoPin2, HIGH);
  98.            delayMicroseconds(pulseWidth2);
  99.            digitalWrite(servoPin2,LOW);
  100.            
  101.          }
  102.           lastPulse2 = millis();
  103.        }  
  104.      if (pulseWidth2 < minPulse) {
  105.            pulseWidth2 = minPulse;
  106.            if (millis() - lastPulse2 >= refreshTime){
  107.            digitalWrite(servoPin2, HIGH);
  108.            delayMicroseconds(pulseWidth2);
  109.            digitalWrite(servoPin2,LOW);
  110.            
  111.           }
  112.           lastPulse2 = millis();
  113.          }  
  114.    
  115.    
  116.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement