ARDUINO (sensor)
What is Sensor?
It is Input!
Digital Read&Write
Press the button
Light-on Light-off
Signal
Press release
Upload the code: File > Examples > 2.Digital > Button. Press= connect Go the easiest way.
/ constants won't change. They're used here to // set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin // variables will change: int buttonState = 0; // variable for reading the pushbutton status
void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); }
Set up which pin for what
void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } }
If you press the button It turns “on�
Analog Read
potentiometer
signal +
-
0-1024
Upload the code: http://arduino.cc/en/Tutorial/AnalogReadSerial
/* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor. Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. This example code is in the public domain. */
Set up which pin for what // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: Read the message from Analog Pin0 int sensorValue = analogRead(A0); // print out the value you read: Print out the value Serial.println(sensorValue); delay(1); // delay in between reads for stability In every ? million second }
How to see it?
Serial Monitor
Upload the code: http://arduino.cc/en/Tutorial/AnalogReadSerial
Let’s have some fun =Connect to Processing
Upload the code: http://arduino.cc/en/Tutorial/AnalogReadSerial
Same Code for “Arduino”
/* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor. Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. This example code is in the public domain. */
Set up which pin for what // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: Read the message from Analog Pin0 int sensorValue = analogRead(A0); // print out the value you read: Print out the value Serial.println(sensorValue); delay(1); // delay in between reads for stability In every ? million second }
How to see it?
Upload the code: http://arduino.cc/en/Tutorial/Graph
Code for “Processing”
import processing.serial.*; Serial myPort; // The serial port int xPos = 1; // horizontal position of the graph
Import the library
void setup () { // set the window size: size(1000, 500); // List all the available serial ports println(Serial.list()); // I know that the first port in the serial list on my mac // is always my Arduino, so I open Serial.list()[0]. // Open whatever port is the one you're using. Check myPort = new Serial(this, Serial.list()[0], 9600); myPort.bufferUntil('\n'); // don't generate a serialEvent() unless you get a newline character: background(0); // set inital background: } void draw () { // everything happens in the serialEvent() } void serialEvent (Serial myPort) {// get the ASCII string: String inString = myPort.readStringUntil('\n'); if (inString != null) {// trim off any whitespace: inString = trim(inString);// convert to an int and map to the screen height: float inByte = float(inString); inByte = map(inByte, 0, 1023, 0, height); // draw the line: stroke(127,34,255); line(xPos, height, xPos, height - inByte); // at the edge of the screen, go back to the beginning: if (xPos >= width) { xPos = 0; background(0); } else {// increment the horizontal position: xPos++; } } }
the serial
Break the numbers
Remap the Value Where we can play with
LDR Light Dependent Resistor
Same code from: http://arduino.cc/en/Tutorial/AnalogReadSerial
/* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor. Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. This example code is in the public domain. */
Set up which pin for what // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: Read the message from Analog Pin0 int sensorValue = analogRead(A0); // print out the value you read: Print out the value Serial.println(sensorValue); delay(1); // delay in between reads for stability In every ? million second }
Turn on your serial monitor
LDR
import processing.serial.*; Serial myPort; // The serial port int xPos = 1; // horizontal position of the graph
Still you can play with Processing(Same Code)
void setup () { // set the window size: size(1000, 500); // List all the available serial ports println(Serial.list()); // I know that the first port in the serial list on my mac // is always my Arduino, so I open Serial.list()[0]. // Open whatever port is the one you're using. Check myPort = new Serial(this, Serial.list()[0], 9600); myPort.bufferUntil('\n'); // don't generate a serialEvent() unless you get a newline character: background(0); // set inital background: } void draw () { // everything happens in the serialEvent() } void serialEvent (Serial myPort) {// get the ASCII string: String inString = myPort.readStringUntil('\n'); if (inString != null) {// trim off any whitespace: inString = trim(inString);// convert to an int and map to the screen height: float inByte = float(inString); inByte = map(inByte, 0, 100, 0, height); // draw the line: stroke(127,34,255); line(xPos, height, xPos, height - inByte); // at the edge of the screen, go back to the beginning: if (xPos >= width) { xPos = 0; background(0); } else {// increment the horizontal position: xPos++; } } }
the serial
Break the numbers
Need to Remap this Where we can play with
Some Examples I did
https://vimeo.com/14201235
https://vimeo.com/14201235
https://vimeo.com/13977345
https://vimeo.com/13977625
Did you get the idea?
It’s all about INPUT & OUTPUT