Skip to content

Instantly share code, notes, and snippets.

@schappim
Forked from electricimp/ArduinoUART.device.nut
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save schappim/18e1e452b18de39253f3 to your computer and use it in GitHub Desktop.

Select an option

Save schappim/18e1e452b18de39253f3 to your computer and use it in GitHub Desktop.
Basic imp <--> Arduino code example
// 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);
// 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