-
-
Save schappim/18e1e452b18de39253f3 to your computer and use it in GitHub Desktop.
Basic imp <--> Arduino code example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // electric imp device code | |
| server.log("Device Started"); | |
| arduino <- hardware.uart57; | |
| function arduinoData() { | |
| local b = arduino.read(); | |
| while(b != -1) { | |
| local state = "Unknown"; | |
| if (b == 0x10) state = "Off"; | |
| if (b == 0x11) state = "On" | |
| server.log("LED: " + state); | |
| b = arduino.read(); | |
| } | |
| } | |
| arduino.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, arduinoData); | |
| function blink(state) { | |
| server.log("Setting LED to: " + state); | |
| arduino.write(state); | |
| imp.wakeup(1.0, function() { blink(1-state); }); | |
| } blink(1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // arduino device code | |
| int led = 13; // led pin number | |
| void setup() { | |
| Serial.begin(9600); // configure serial | |
| pinMode(led, OUTPUT); // configure LED pin | |
| digitalWrite(led, 0); // turn LED off | |
| } | |
| void loop() { | |
| int b = 0; | |
| // if there's data available | |
| if (Serial.available () > 0) { | |
| // read a byte | |
| b = Serial.read(); | |
| if (b == 0x00) { | |
| digitalWrite(led, LOW); | |
| Serial.write(0x10); | |
| } else if (b == 0x01) { | |
| digitalWrite(led, HIGH); | |
| Serial.write(0x11); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment