Created
October 4, 2013 08:40
-
-
Save abhikpal/6822845 to your computer and use it in GitHub Desktop.
Code to supplement the "Interfacing Arduino from Processing" Tutorial
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
/* | |
* Interfacing Arduino from Processing | |
* Example 1 Arduino Sketch | |
* | |
* The Arduino listens for any data on the serial buffer. | |
* If any data is received it is stored in a char-variable. | |
* If the received value is 'H' then the led is turned on and | |
* if the received value is 'L' then the LED is turned off. | |
* No action is taken for other values. | |
* | |
* Abhik Pal | |
*/ | |
// LED connected to pin 13 | |
const int ledPin = 13; | |
void setup() | |
{ | |
// set the led as output | |
pinMode(ledPin, OUTPUT); | |
// begin serial communication with a baud rate of 9600 | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
// check for any available data on the serial port | |
if (Serial.available()) | |
{ | |
// store received value as a char | |
char inputValue = Serial.read(); | |
// if the received value is 'H' turn on the LED | |
if(inputValue == 'H') | |
{ | |
digitalWrite(ledPin, HIGH); | |
} | |
// if the received value is 'L' turn off the led | |
else if(inputValue == 'L') | |
{ | |
digitalWrite(ledPin, LOW); | |
} | |
else | |
{ | |
// else do nothing | |
} | |
} | |
else | |
{ | |
// do nothing | |
} | |
// a small delay to keep things sane | |
delay(10); | |
} |
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
/* | |
* Interfacing Arduino from Processing | |
* Example 2 Arduino Sketch | |
* | |
* Analog values are first read from the pot-meter | |
* connected on Analog Pin 0 (aka pin 14) and stored in | |
* a variable. The value is then sent over the serial port | |
* to a processing sketch running on a computer, where | |
* the received value can be used to draw visuals, or do | |
* other stuff. | |
* | |
* Abhik Pal | |
*/ | |
// potmeter connected on pin 14 i.e Analog 0 (A0) | |
const int potPin = 14; | |
void setup() | |
{ | |
// declaring potPin as INPUT is not nessasary as Analog | |
// pins are pre-configured to receive inputs. | |
// begin serial communication with a baud rate of 9600 | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
// read pot meter value | |
int potReading = analogRead(potPin); | |
// Send the value via the serial port | |
Serial.println(potReading); | |
// add a delay to control amount of data sent and | |
// prevent insanely weird results | |
delay(100); | |
} |
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
/* | |
* Interfacing Arduino from Procesisng | |
* Example 1 Processing Sketch | |
* | |
* The code sends a 'H' to the ardunino via the Serial port | |
* if the mouse coordinates are inside the button and the | |
* left mouse button is pressed. | |
* If either of the two conditions are not met then a 'L' is | |
* sent. | |
* The sent values are then interpreted by the Arduino to | |
* switch on/off the led connected on pin 13 | |
* | |
* Abhik Pal | |
*/ | |
// import the processing serial library | |
import processing.serial.*; | |
// create an instance of the Serial object | |
Serial arduinoPort; | |
void setup() | |
{ | |
// setting up the basic interface attributes | |
size(400, 400); | |
background(20); | |
noStroke(); | |
fill(100); | |
// set up the serial port | |
// change "COM5" to the port your arduino is connected | |
arduinoPort = new Serial(this, "COM5", 9600); | |
} | |
void draw() { | |
// draw a rectangle to act as our button | |
rect(150, 150, 100, 100); | |
// check if the mouse pointer is over our button | |
if((mouseX > 150) && (mouseX < 250) && (mouseY > 150) && (mouseY < 250)) | |
{ | |
// change fill color to give visual feedback | |
fill(220); | |
// check if the mouse button is presses | |
// i.e. detect a click | |
if(mousePressed == true) | |
{ | |
// send 'H' to light up the LED on the arduino | |
arduinoPort.write('H'); | |
} | |
else | |
{ | |
// else do nothing | |
} | |
} | |
else | |
{ | |
// use default fill | |
fill(110); | |
// send 'L' to turn off the LED | |
arduinoPort.write('L'); | |
} | |
} |
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
/* | |
* Interfacing Arduino from Procesisng | |
* Example 2 Processing Sketch | |
* | |
* The sensor readings sent from the Arduino are | |
* converted to int data type and stored in a variable | |
* The variable is then used to draw a simple bar on the | |
* window. The height of the bar is dependent on the | |
* value received i.e. it changes when we rotate the | |
* pot-meter. | |
* | |
* Abhik Pal | |
*/ | |
// import the processing serial library | |
import processing.serial.*; | |
// create an instance of the Serial object | |
Serial arduinoPort; | |
// this variable will store the height of the | |
// bar. | |
float barHeight = 0; | |
void setup() | |
{ | |
// setting up the basic interface attributes | |
size(600, 150); | |
background(20); | |
noStroke(); | |
fill(220); | |
// set up the serial port | |
// change "COM5" to the port your arduino is connected | |
arduinoPort = new Serial(this, "COM5", 9600); | |
// serialEvent will be triggred when a | |
// '\n' (newline) character is received. | |
// As we have used Serial.println(); to send our data | |
// from the Arduino a '\n' character will be found at the end | |
// of our desired value i.e. the pot-meter reading | |
arduinoPort.bufferUntil('\n'); | |
} | |
void draw() | |
{ | |
// clear the screen so that new changes can be | |
// observed | |
background(20); | |
// drawing our bar | |
rect(25, 25, barHeight, 100); | |
} | |
// Triggreed whenever the character defined in | |
// arduinoPort.bufferUntil(); is received | |
void serialEvent(Serial arduinoPort) | |
{ | |
// store the raw input received from the arduino | |
// as string. | |
// the readStringUntil() method makes sure that | |
// only the values upto '\n' charcter are stored | |
String rawInput = arduinoPort.readStringUntil('\n'); | |
// convert the string to an int | |
// the trim() function is used to remove any spaces | |
// from the string | |
int rawVal = int(trim(rawInput)); | |
// map the received values from a range of 0 to 1023 | |
// to a smaller range of 0 to 550; our maximum and minimum | |
// bar heights. | |
barHeight = map(rawVal, 0, 1023, 0, 550); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment