Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int buttonPin = 2; //digital pin 2
- int lastRead = LOW;
- void setup(){
- pinMode(buttonPin, INPUT);
- digitalWrite(buttonPin, HIGH); //turn on internal pull-up resistor
- Serial.begin(9600);
- }
- void loop(){
- sendReading(); //send reading from button
- }
- void sendReading(){
- //read the digital pin, send a value only if it has changed
- int buttonValue = digitalRead(buttonPin);
- //We ONLY want to send changes so the serial buffer does not overflow with unneeded values
- if(buttonValue == HIGH && buttonValue != lastRead){
- send(1);
- lastRead = buttonValue;
- }
- else if(buttonValue != lastRead){
- lastRead = buttonValue;
- send(0);
- }
- }
- void send(int value){
- value = constrain(value, 0, 255);
- Serial.write(value);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement