Advertisement
3rdWard_Arduino

class4_Arduino_to_Processing_send_potVals

Apr 1st, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. /**
  2.  * Send a byte through Serial to be used by Processing
  3.  * I am pretty sure the original code was written by Tom Igoe
  4.  */
  5.  
  6. int analogPin = 0;
  7. int analogValue = 0;
  8. int forwardLedPin = 3;
  9. int backwardLedPin = 11;
  10. int pwmValue = 0;
  11.  
  12. void setup()
  13. {
  14.   // start serial port at 9600 bps:
  15.   Serial.begin(9600);
  16. }
  17.  
  18. void loop()
  19. {
  20.   // read analog input, divide by 4 to make the range 0-255:
  21.   analogValue = analogRead(analogPin);
  22.   analogValue = analogValue / 4;
  23.   Serial.print(analogValue, BYTE);
  24.   // figure out which LED should be on
  25.   // if the values are between 0-127, then the backwardLed should be lit
  26.   if(analogValue <= 127){
  27.     pwmValue = map(analogValue, 127, 0, 0, 255);
  28.     analogWrite(backwardLedPin, pwmValue);
  29.     analogWrite(forwardLedPin, 0);// this will make sure the other LED is off
  30.   }
  31.   // if the values are between 128-255, then the forwardLed should be lit
  32.   // pwm an led to show that the pot is working
  33.   if(analogValue >= 128){
  34.     pwmValue = map(analogValue, 128, 255, 0, 255);
  35.     analogWrite(forwardLedPin, pwmValue);
  36.     analogWrite(backwardLedPin, 0);// this will make sure the other LED is off
  37.   }
  38.   // pause for 10 milliseconds:
  39.   delay(10);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement