Skip to content

Instantly share code, notes, and snippets.

@dromer
Last active August 29, 2015 14:02
Show Gist options
  • Save dromer/a251c291b010bbeecf86 to your computer and use it in GitHub Desktop.
Save dromer/a251c291b010bbeecf86 to your computer and use it in GitHub Desktop.
#include <LiquidCrystal.h>
#include <Firmata.h>
byte pin;
int analogValue;
int previousAnalogValues[TOTAL_ANALOG_PINS];
byte portStatus[TOTAL_PORTS]; // each bit: 1=pin is digital input, 0=other/ignore
byte previousPINs[TOTAL_PORTS];
/* timer variables */
unsigned long currentMillis; // store the current value from millis()
unsigned long previousMillis; // for comparison with currentMillis
/* make sure that the FTDI buffer doesn't go over 60 bytes, otherwise you
get long, random delays. So only read analogs every 20ms or so */
int samplingInterval = 19; // how often to run the main loop (in ms)
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void sendPort(byte portNumber, byte portValue)
{
portValue = portValue & portStatus[portNumber];
if (previousPINs[portNumber] != portValue) {
Firmata.sendDigitalPort(portNumber, portValue);
previousPINs[portNumber] = portValue;
}
}
void setup() {
// initialize LCD and set up the number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(0, 0);
// Print a message to the lcd.
lcd.print("I <3 Arduino :)");
byte i, port, status;
Firmata.setFirmwareVersion(0, 1);
for (pin = 8; pin < TOTAL_PINS; pin++) {
if IS_PIN_DIGITAL(pin) pinMode(PIN_TO_DIGITAL(pin), INPUT);
}
for (port = 0; port < TOTAL_PORTS; port++) {
status = 0;
for (i = 0; i < 8; i++) {
if (IS_PIN_DIGITAL(port * 8 + i)) status |= (1 << i);
}
portStatus[port] = status;
}
Firmata.begin(57600);
}
void loop() {
lcd.setCursor(0, 1);
lcd.write("Value is: ");
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
// lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
}
}
byte i;
for (i = 8; i < TOTAL_PORTS; i++) {
sendPort(i, readPort(i, 0xff));
}
/* make sure that the FTDI buffer doesn't go over 60 bytes, otherwise you
get long, random delays. So only read analogs every 20ms or so */
currentMillis = millis();
if (currentMillis - previousMillis > samplingInterval) {
previousMillis += samplingInterval;
while (Firmata.available()) {
Firmata.processInput();
}
for (pin = 0; pin < TOTAL_ANALOG_PINS; pin++) {
analogValue = analogRead(pin);
if (analogValue != previousAnalogValues[pin]) {
Firmata.sendAnalog(pin, analogValue);
previousAnalogValues[pin] = analogValue;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment