Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save logicaroma/08dc4930cca6a0cc6119 to your computer and use it in GitHub Desktop.
Save logicaroma/08dc4930cca6a0cc6119 to your computer and use it in GitHub Desktop.
byte lineEnding = 0x0A;
//Data array that the received info from android app is recorded. Every byte corresponds to a digital pin.
byte readBuffer[15]; //For example readBuffer[5] is for digital pin 5.
byte defaultByte = 0x10; //On start all arduino digital pins must be input for electrical protection.
//Pins 0 and 1 are used for serial comminations. So read
void setup() {
for (int i=0; i < sizeof(readBuffer)-1 ; ++i) {
readBuffer[i] = defaultByte;}
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 14) {
Serial.readBytesUntil(lineEnding, (char*) readBuffer, 15);
for (int i=2; i < sizeof(readBuffer)-1 ; i++ ) {
if( bitRead(readBuffer[i], 4 )) { pinMode(i,INPUT);} else { pinMode(i,OUTPUT);}
}
for (int i=2; i < sizeof(readBuffer)-1 ; i++ ) {
if( bitRead(readBuffer[i], 0 )) { digitalWrite(i,HIGH);} else { digitalWrite(i,LOW);}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment