Skip to content

Instantly share code, notes, and snippets.

@samueljackson92
Created September 18, 2014 10:38
Show Gist options
  • Save samueljackson92/af9837be499c07952767 to your computer and use it in GitHub Desktop.
Save samueljackson92/af9837be499c07952767 to your computer and use it in GitHub Desktop.
Arduino serial communication
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
const int sensorPin = A0;
const int messageBufferSize = 17;
char messageBuffer[17];
void read_serial()
{
char inputChar = -1;
int index = 0;
//clear the buffer ready for a new message
memset(&messageBuffer[0], 0, sizeof(messageBuffer));
//first check we have data available
while(Serial.available()> 0)
{
//check that we don't overflow the buffer
if (index < messageBufferSize-1)
{
inputChar = Serial.read();
messageBuffer[index] = inputChar;
++index;
messageBuffer[index] = '\0';
}
}
}
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
}
void loop()
{
int sensorValue = analogRead(sensorPin);
float voltage = (sensorValue/1024.0) * 5.0;
float temperature = (voltage - 0.5) * 100;
//write temperature to serial
Serial.println(temperature);
//read response from serial
read_serial();
lcd.clear();
lcd.setCursor(0,0);
lcd.print(messageBuffer);
delay(1000);
}
import serial
ser = serial.Serial('/dev/tty.usbmodem1411', 9600)
while True:
print ser.readline()
ser.write("hello");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment