Advertisement
Guest User

oneBit Send

a guest
Sep 24th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. int buttonPin = 2; //digital pin 2
  2. int lastRead = LOW;
  3.  
  4. void setup(){
  5.   pinMode(buttonPin, INPUT);  
  6.   digitalWrite(buttonPin, HIGH); //turn on internal pull-up resistor  
  7.  
  8.   Serial.begin(9600);
  9. }
  10.  
  11.  
  12.  
  13.  
  14. void loop(){
  15.   sendReading(); //send reading from button
  16. }
  17.  
  18.  
  19.  
  20. void sendReading(){
  21.   //read the digital pin, send a value only if it has changed
  22.   int buttonValue = digitalRead(buttonPin);
  23.  
  24.   //We ONLY want to send changes so the serial buffer does not overflow with unneeded values
  25.   if(buttonValue == HIGH && buttonValue != lastRead){
  26.     send(1);
  27.     lastRead = buttonValue;
  28.   }
  29.   else if(buttonValue != lastRead){
  30.     lastRead = buttonValue;
  31.     send(0);
  32.   }
  33. }
  34.  
  35.  
  36. void send(int value){
  37.   value = constrain(value, 0, 255);
  38.   Serial.write(value);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement