-
Notifications
You must be signed in to change notification settings - Fork 0
In Class Week 12
First, let's review analogue input in Arduino, using an LDR.
From Make: Getting Started with Arduino (Third Edition)
Read Pages 66-72 (Download under "Readings" on the Wiki)
Complete Exercises 5-3 and 5-5.
Not sure of your resistor values, and without a multimeter? Here is a handy chart:
http://dlnmh9ip6v2uc.cloudfront.net/images/products/10969-03.jpg
(Note: If your LDR is not behaving as expected, keep moving through the exercises until you get to Serial Communication, to find out the read values of the sensor.)
After completing the above exercises, sub in a Force Sensitive Resistor (FSR) for the LDR.
(Note: If you’d like extra info on FSR, take a look at
https://learn.adafruit.com/force-sensitive-resistor-fsr/connecting-to-an-fsr )
Press on the FSR and read the values in the Serial Monitor. Create a condition in your code so that the LED turns on/off depending on how hard the FSR is pressed.
AND NOW FOR THE VERY MAGICAL MOMENT!!! Let’s connect the sensor to Processing!
Create a new sketch in Arduino, using this code:
////////////////////////////////////////////////////////////////////////////////////
// Code for sensing analogue sensor and writing the value to the serial port
int sensorPin = 0; //sensor hooked up to analog pin 0
int val = 0;
void setup() {
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
val = analogRead(sensorPin); //read sensor and assign to variable called val
delay(100); // Wait 100 milliseconds
Serial.write(val);
}
////////////////////////////////////////////////////////////////////////////////////
Upload the Arduino code to your board.
Create a new sketch in Processing, using this code:
////////////////////////////////////////////////////////////////////////////////////
/*
Simple sketch for reading serial values from Arduino.
The size of an ellipse changes with sensor input.
Must assign the correct port in setup in order to read from Arduino.
*/
import processing.serial.*; //imports Serial library from Processing
Serial myPort; // creates object from Serial class
int val; // creates variable for data coming from serial port
void setup() {
size(400, 400);
/*
We must tell Processing which port to read from.
Run the sketch and check your console to see the results of printArray(Serial.list());
and that is how you know the port to use.
On my Mac, it is port 2 and so I open Serial.list()[2].
*/
printArray(Serial.list()); // this line prints the port list to the console
String portName = Serial.list()[2]; //change the number in the [] for the port you need
myPort = new Serial(this, portName, 9600);
}
void draw(){
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
}
//draws an ellipse that grows and shrinks in relation to val
background(255);
fill (0);
ellipse (width/2, height/2, val, val);
println (val);
}
////////////////////////////////////////////////////////////////////////////////////
Follow the instructions for assigning a port in the Processing sketch.
Run the sketch and press your FSR. You should see an ellipse growing with increased pressure to the FSR.
Sub out the FSR for the LDR and run the sketch again.
////////////////////////////////////////////////////////////////////////////////////
Important notes on using Serial in Arduino:
- Use Serial.println() in your Arduino sketch to print values from Arduino to the Serial monitor in English. This is useful for troubleshooting.
- Use Serial.write() in your Arduino sketch to send values over the Serial port to use in Processing. It writes with binary data, not readable in English, but readable by Processing.
- Do not use Serial.println() and Serial.write() at the same time in your sketch.
- The Arduino Serial Monitor must be closed when Processing is trying to access the Serial port.
////////////////////////////////////////////////////////////////////////////////////
And now put it to the test...
Create another iteration of any of your previous Processing short studies. Integrate input from an FSR, LDR, push button, or another sensor of your choice. Note that if you use the push button, you will need to modify the code above to read from a digital pin.
////////////////////////////////////////////////////////////////////////////////////
What about the other way around: sending data from Processing to Arduino?
Or sending data between the two, simultaneously?
These tutorials offer a good summary:
https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing