Referência:
http://www.dfrobot.com/
Montagem:
Firmware:
#include <Wire.h>
int NewX=0, OldX=0, y=0;
int ExitReadButton = 1,
oldstate1 = 0,
oldstate2 = 0,
oldstate3 = 0,
oldstate4 = 0,
oldstate5 = 0,
oldstate6 = 0,
oldstate7 = 0,
oldstate8 = 0;
const int Addr_Button = 0x38;
void setup(){
Wire.begin();
Serial.begin(9600);
}
void loop(){
Wire.requestFrom(Addr_Button,1);
ReadButton();
Serial.println(y);
}
void ReadButton(){
byte x;
do{
Wire.requestFrom (Addr_Button,1);
if(Wire.available()) x = Wire.read();
if (x == 254 && oldstate1 == 0) oldstate1 = 1;
if (x == 255 && oldstate1 == 1){
y = 0;
oldstate1 = 0;
ExitReadButton = 0;
}
if (x == 253 && oldstate2 == 0) oldstate2 = 1;
if (x == 255 && oldstate2 == 1){
y = 1;
oldstate2 = 0;
ExitReadButton = 0;
}
if (x == 251 && oldstate3 == 0) oldstate3 = 1;
if (x == 255 && oldstate3 == 1){
y = 2;
oldstate3 = 0;
ExitReadButton = 0;
}
if (x == 247 && oldstate4 == 0) oldstate4 = 1;
if (x == 255 && oldstate4 == 1){
y = 3;
oldstate4 = 0;
ExitReadButton = 0;
}
if (x == 239 && oldstate5 == 0) oldstate5 = 1;
if (x == 255 && oldstate5 == 1){
y = 4;
oldstate5 = 0;
ExitReadButton = 0;
}
if (x == 223 && oldstate6 == 0) oldstate6 = 1;
if (x == 255 && oldstate6 == 1){
y = 5;
oldstate6 = 0;
ExitReadButton = 0;
}
if (x == 191 && oldstate7 == 0) oldstate7 = 1;
if (x == 255 && oldstate7 == 1){
y = 6;
oldstate7 = 0;
ExitReadButton = 0;
}
if (x == 127 && oldstate8 == 0) oldstate8 = 1;
if (x == 255 && oldstate8 == 1){
y = 7;
oldstate8 = 0;
ExitReadButton = 0;
}
}while(ExitReadButton);
delay(200);
ExitReadButton = 1;
}
Download: Schematic Mega Shield
Nenhum comentário:
Postar um comentário