Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 26th, 2012  |  syntax: None  |  size: 1.58 KB  |  views: 176  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  const int switchPin = 2;    // switch input
  2.   const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  3.   const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
  4.   const int enablePin = 9;    // H-bridge enable pin
  5.  
  6.  
  7.   void setup() {
  8.    
  9.      // initialize serial:
  10.   Serial.begin(9600);
  11.    
  12.     // set the switch as an input:
  13.     pinMode(switchPin, INPUT);
  14.  
  15.     // set all the other pins you're using as outputs:
  16.     pinMode(motor1Pin, OUTPUT);
  17.     pinMode(motor2Pin, OUTPUT);
  18.     pinMode(enablePin, OUTPUT);
  19.  
  20.  
  21.     // set enablePin high so that motor can turn on:
  22.     digitalWrite(enablePin, HIGH);
  23.   }
  24.  
  25.  
  26.     void loop() {
  27.      
  28.      
  29.       while (Serial.available() >0) {
  30.        
  31.         int thespeed = Serial.parseInt();
  32.         int thedirection = Serial.parseInt();
  33.        
  34.         if (Serial.read() == '\n') {
  35.          
  36.           thespeed = 255 - constrain(thespeed, 0, 255);
  37.           thedirection = 255 - constrain(thedirection, 0, 255);
  38.          
  39.          analogWrite(enablePin,thespeed);
  40.          
  41.         }
  42.    
  43.       }
  44.      
  45.      
  46.      
  47.     // if the switch is high, motor will turn on one direction:
  48.     if (digitalRead(switchPin) == HIGH) {
  49.       digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
  50.       digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
  51.     }
  52.     // if the switch is low, motor will turn in the other direction:
  53.     else {
  54.       digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
  55.       digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
  56.     }
  57.   }