27
1

DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Embed Size (px)

Citation preview

Page 1: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

1

Page 2: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Digital Input – Output to ProcessingControlling the Screen with a Button

1. Controlling an LED with Push ButtonA push‐button will open (or close) a circuit when pressed. Its circuit is shown below. The black wire goes to ground, the yellow to the pin (2 for this case), and the 10K resistor connects to 5V.If you connect the 10K resistor to ground and the black wire to 5V you inverse the state of the push button.

2

Page 3: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Digital Input – Output to ProcessingControlling the Screen with a Button

1. Controlling an LED with Push ButtonA push‐button will open (or close) a circuit when pressed. Its circuit is shown below. The black wire goes to ground, the yellow to the pin (2 for this case), and the 10K resistor connects to 5V.

3

Page 4: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Digital Input – Output to ProcessingControlling the Screen with a Button

1. Controlling an LED with Push ButtonA push‐button will open (or close) a circuit when pressed. Its circuit is shown below. The black wire goes to ground, the yellow to the pin (2 for this case), and the 10K resistor connects to 5V.

void setup(){Serial.begin(9600); //start the serial port in order to writepinMode(2,INPUT); // set the pin 5 as inputpinMode(13, OUTPUT); // set the pin 13 as output}void loop(){int val = digitalRead(2); //read from the pin 5if(val==LOW){ digitalWrite(13, LOW); // sets the LED off}else{digitalWrite(13, HIGH); // sets the LED on}Serial.println(val);}

4

Page 5: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Digital Input – Output to ProcessingControlling the Screen with a Button

2. Controlling Processing with Push Buttona. Upload the Arduino Code b. Stop Arduinoc. Run Processingd. Check which port is available and what is the index of the available porte. Change the Index in Processing code

// This is Processing Codeimport processing.serial.*;int val = 1;Serial port;void setup(){ size(100,100); // Use the first available port port = new Serial(this, Serial.list()[1], 9600); // if in arduino the first option in your list is the port that you are connecting to, //change the 1 to Zero, if it is the second leave it as 1 println(Serial.list()); background(255);}void draw(){ while (port.available() > 0) serialEvent(port.read()); //look for data if (val==1)background(255); if (val==0)background(0);}void serialEvent(int serial) { val=serial-48;// Change the character ascii code to numeric value println(val);}

//This is Arduino Codevoid setup(){Serial.begin(9600); //start the serial port in order to writepinMode(2,INPUT); // set the pin 5 as inputpinMode(13, OUTPUT); // set the pin 13 as output}void loop(){int val = digitalRead(2); //read from the pin 5if(val==LOW){ digitalWrite(13, LOW); // sets the LED off}else{digitalWrite(13, HIGH); // sets the LED on}Serial.println(val);}

5

Page 6: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Analog Input – Output to ProcessingControlling the Screen with a Photocell

void setup(){Serial.begin(9600);}void loop(){int in = analogRead(5);Serial.println(in);}

6

Page 7: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Analog Input – Output to ProcessingControlling the Screen with a Photocell

7

Page 8: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Analog Input – Output to ProcessingControlling the Screen with a Photocell

//This is Arduino Codevoid setup(){Serial.begin(9600);}void loop(){int in = analogRead(5);Serial.println(in);}

import processing.serial.*;int val = 1;String buff = "";int NEWLINE = 10;Serial port;void setup() { size(400,400); // Use the first available port

port = new Serial(this, Serial.list()[1], 9600); // if in arduino the first option in your list is the port that you are connecting to, //change the 1 to Zero, if it is the second leave it as 1 println(Serial.list()); background(255); delay(2000);}void draw() { while (port.available() > 0) serialEvent(port.read()); //look for data background(val);}void serialEvent(int serial) { // If the variable "serial" is not equal to the value for // a new line, add the value to the variable "buff". If the // value "serial" is equal to the value for a new line, // save the value of the buffer into the variable "val". if(serial != NEWLINE) { buff += char(serial); } else { if (buff.length()>2) { buff = buff.substring(0, buff.length()-1); //println(buff); //Parse the String into an integer if(millis()>2000) { println("OK"); val = Integer.parseInt(buff);

val=int(map(val,0,800,0,255)); } } // Clear the value of "buff" buff = ""; }}

8

Page 9: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Analog Input – Output to ProcessingPainting the Screen with a Photocell

//This is Arduino Codevoid setup(){Serial.begin(9600);}void loop(){int in = analogRead(5);Serial.println(in);}

import processing.serial.*;int val = 1;String buff = "";int NEWLINE = 10;

int count;int dir=1;Serial port;void setup(){ size(400,100); // Use the first available port port = new Serial(this, Serial.list()[1], 9600); // if in arduino the first option in your list is the port that you are connecting to, //change the 1 to Zero, if it is the second leave it as 1 println(Serial.list()); background(255);}void draw(){ while (port.available() > 0) serialEvent(port.read()); //look for data

stroke(val);//Set the stroke color based on the sensed data line(count,0,count,100);//Draw a line count=count+dir;//Go one pixel forwards or backwards if ((count>width)||(count<0)) dir=-dir;// The dial gets to the limits of the screen, //change direction}void serialEvent(int serial) { // If the variable "serial" is not equal to the value for // a new line, add the value to the variable "buff". If the // value "serial" is equal to the value for a new line, // save the value of the buffer into the variable "val". if(serial != NEWLINE) { buff += char(serial); } else { if (buff.length()>2) { buff = buff.substring(0, buff.length()-1); //println(buff); //Parse the String into an integer if(millis()>2000) { println("OK"); val = Integer.parseInt(buff); val=int(map(val,0,800,0,255)); } } // Clear the value of "buff" buff = ""; }}

9

Page 10: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

GNDV5Analog Pin 4 Analog Pin 5

Analog Input – Output to ProcessingPainting the Screen with a Two Photocell

10

Page 11: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Analog Input – Output to ProcessingControlling the Screen with Two a Photocell

11

Page 12: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Analog Input – Output to ProcessingPainting the Screen with Two a Photocell

12

Page 13: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Analog Input – Output to ProcessingPainting the Screen with Two a Photocell

//This is Arduino Codevoid setup(){ Serial.begin(9600);}void loop(){ int inA = analogRead(5); int inB = analogRead(4); inA=1000+inA;// The photocell A sends data in 1000 Range inB=2000+inB;// The photocell B sends data in 2000 Range Serial.println(inA); Serial.println(inB);}

//This is Processing Codeimport processing.serial.*;int val = 1;String buff = "";int NEWLINE = 10;Serial port;int count=0;Int dir=1;

int valA,valB;int light_Min=0;int light_Max=750;void setup(){ size(400,200); // Use the first available port port = new Serial(this, Serial.list()[1], 9600); // if in arduino the first option in your list is the port that you are connecting to, //change the 1 to Zero, if it is the second leave it as 1 println(Serial.list()); background(255);}void draw(){ while (port.available() > 0) serialEvent(port.read()); //look for data

stroke(valA);//Set the stroke color based on the sensed data line(count, 0, count, 100);//Draw a line stroke(valB); line(count, 100, count, 200);//Draw a line count=count+dir;//Go one pixel forwards or backwards if ((count>width)||(count<0)) dir=-dir;}

void serialEvent(int serial) { // If the variable "serial" is not equal to the value for // a new line, add the value to the variable "buff". If the // value "serial" is equal to the value for a new line, // save the value of the buffer into the variable "val". if(serial != NEWLINE) { buff += char(serial); } else { if (buff.length()>2) { buff = buff.substring(0, buff.length()-1); //println(buff); //Parse the String into an integer if(millis()>2000) { println("OK"); val = Integer.parseInt(buff);

if (val<2000) valA=int(map((val-1000),light_Min,light_Max,0,255)); if (val>=2000) valB=int(map((val-2000),light_Min,light_Max,0,255)); println("A="+valA); println("B="+valB); } } // Clear the value of "buff" buff = ""; }}

13

Page 14: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Input – Output to file via Processing

//This is Arduino Codevoid setup(){ Serial.begin(9600);}void loop(){ int inA = analogRead(5); int inB = analogRead(4); inA=1000+inA;// The photocell A sends data in 1000 Range inB=2000+inB;// The photocell B sends data in 2000 Range Serial.println(inA); Serial.println(inB);}

//This is Processing Codeimport processing.serial.*;

PrintWriter output;int val = 1;String buff = "";int NEWLINE = 10;Serial port;int valA,valB;int light_Min=0;int light_Max=895;

void setup(){ size(200,100);

// Create a new file in the sketch directory output = createWriter("log.txt"); // Use the first available port port = new Serial(this, Serial.list()[1], 9600); // if in arduino the first option in your list is the port that you are connecting to, //change the 1 to Zero, if it is the second leave it as 1 println(Serial.list()); background(255);}

void draw(){ while (port.available() > 0) serialEvent(port.read()); //look for data

output.println("A"+valA+","+"B"+valB); // Write the coordinate to the file fill(valA); rect(0,0,100,100); fill(valB); rect(100,0,100,100);}

void serialEvent(int serial) { // If the variable "serial" is not equal to the value for // a new line, add the value to the variable "buff". If the // value "serial" is equal to the value for a new line, // save the value of the buffer into the variable "val". if(serial != NEWLINE) { buff += char(serial); } else { if (buff.length()>2) { buff = buff.substring(0, buff.length()-1); //println(buff); //Parse the String into an integer if(millis()>2000) { println("OK"); val = Integer.parseInt(buff); if (val<2000) valA=int(map((val-1000),light_Min,light_Max,0,255)); if (val>=2000) valB=int(map((val-2000),light_Min,light_Max,0,255)); println("A="+valA); println("B="+valB); } } // Clear the value of "buff" buff = ""; }}

void keyPressed() { output.flush(); // Writes the remaining data to the file output.close(); // Finishes the file exit(); // Stops the program}

14

Page 15: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Reading Data from File-Making Graphs

String lines[];// Array of all the lines in the fileString light_A[];// Array of all the light readings of point AString light_B[];// Array of all the light readings of point Bint counter=0;// counter that changes value in each loopint graphScale;// The scale of the graph int xPrevA,xPrevB;//x Value of Previous point on the graphint avrA,avrB;int yPrevA,yPrevB;// y Value of Previouspoint on the graphvoid setup(){ size(800,600); background(255); yPrevA=height-10;// Starting from the base line yPrevB=height-310;// Starting from the base line lines= loadStrings("log.txt");//loading the data from file to lines array light_A= new String[lines.length];// set the size of the Array of the data read at point A light_B= new String[lines.length];// set the size of the Array of the data read at point B for (int i=0; i < lines.length; i++) {// for each entry in the lines Array String each_Line[]= split(lines[i], ',');// Split the two values on each line at the ',' light_A[i]=each_Line[0].substring(1);// put the first value minus the 'A' to array A light_B[i]=each_Line[1].substring(1);// put the second value minus the 'B' to array B //println(light_A[i]+","+light_B[i]); } graphScale=lines.length/width;// Scale factor scales down the graph to the width of the window line(0,height-10,width,height-10);// Base line of the data grapg of point A line(0,height-310,width,height-310);// Base line of the data grapg of point B stroke(200); for(int i=1;i<10;i++){ line(0,height-10-i*20,width,height-10-i*20);// Grid lines of the data graph of Point A line(0,height-310-i*20,width,height-310-i*20);// Grid lines of the data graph of Point B }}void draw(){ if (counter<lines.length-1){// for each entry in array A and B counter=counter+1;// Change the counter stroke(0,0,0);// Frame color of the rectangles fill(int(light_A[counter]));// Change the fill color of the rectangle representing the light intensity at point A rect(0,0,100,100); fill(int(light_B[counter]));// Change the fill color of the rectangle representing the light intensity at point B rect(100,0,100,100); // Scaleing and drawing the Graphs if(counter%graphScale==0){ avrA=avrA/graphScale;// get the avrage of the reads for Point A avrB=avrB/graphScale;// get the avrage of the reads for Point B stroke(255,0,0);// Color of the grapg for Point A line(xPrevA,yPrevA,counter/graphScale,height-10-avrA);// Draw the graph segment for Point A stroke(0,255,0);// Color of the grapg for Point B line(xPrevB,yPrevB,counter/graphScale,height-310-avrB);// Draw the graph segment for Point B xPrevA=counter/graphScale; xPrevB=counter/graphScale; yPrevA=height-10-avrA; yPrevB=height-310-avrB; avrA=0; avrB=0; } else{ avrA=avrA+int(light_A[counter]); avrB=avrB+int(light_B[counter]); } }}

15

Page 16: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Controlling Processing with Multiple input from Arduino - Making RGB colors on Screen Manipulating Potentiometers

16

Page 17: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Controlling Processing with Multiple input from Arduino - Making RGB colors on Screen Manipulating Potentiometers

Gro

und

v5

Anal

og P

in 4

Anal

og P

in 3

Anal

og P

in 5

17

Page 18: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

Controlling Processing with Multiple input from Arduino - Making RGB colors on Screen Manipulating Potentiometers

//This is Arduino Codevoid setup(){ Serial.begin(9600);}void loop(){ int inR = analogRead(5); int inG = analogRead(4); int inB = analogRead(3);

inR=1000+inR;// The Red ranges from 1000-2023 inG=3000+inG;// The Green ranges from 3000-4023 inB=5000+inB;// The Blue ranges from 5000-6023 Serial.println(inR); Serial.println(inG); Serial.println(inB);}

//This is Processing Codeimport processing.serial.*;int val = 1;String buff = "";int NEWLINE = 10;Serial port;int r,g,b;void setup(){ size(400,400); // Use the first available port port = new Serial(this, Serial.list()[1], 9600); // if in arduino the first option in your list is the port that you are connecting to, //change the 1 to Zero, if it is the second leave it as 1 println(Serial.list()); background(255);}void draw(){ while (port.available() > 0) serialEvent(port.read()); //look for data

color c=color(r,g,b); background(c);}void serialEvent(int serial) { // If the variable "serial" is not equal to the value for // a new line, add the value to the variable "buff". If the // value "serial" is equal to the value for a new line, // save the value of the buffer into the variable "val". if(serial != NEWLINE) { buff += char(serial); } else { if (buff.length()>2) { buff = buff.substring(0, buff.length()-1); //println(buff); //Parse the String into an integer if(millis()>2000) { println("OK"); val = Integer.parseInt(buff);

if (val<3000) r=int(map(val,1000,2023,0,255)); if ((val>=3000)&&(val<5000)) g=int(map(val,3000,4023,0,255)); if (val>=5000) b=int(map(val,5000,6023,0,255)); println("R="+r); println("G="+g); println("B="+b); } } // Clear the value of "buff" buff = ""; }}

18

Page 19: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

19

Page 20: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

User Input from ProcessingControlling one Actuator On Arduino

20

Page 21: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

User Input from ProcessingControlling one Actuator On Arduino

// THis is processing Code

import processing.serial.*;Serial myPort; color c= color(0,0,0);void setup(){ background(255,255,255); size(100,100);

myPort = new Serial(this, Serial.list()[1], 9600); fill(0,0,0);}void draw(){ fill(c); ellipse(50,50,50,50); }void mouseClicked(){

if (((mouseX>25)&&(mouseX<75))&&((mouseY>25)&&(mouseY<75))) if (red(c)==0){ c=color(255,0,0); myPort.write(1); } else{ c=color(0,0,0); myPort.write(0); }}

// This is Arduino Code int ledPin = 13; // LED connected to digital pin 13 int val = 0; void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin as output

Serial.begin(9600); } void loop() {

val = Serial.read(); if (val==0){digitalWrite(13,LOW);} if (val==1){digitalWrite(13,HIGH);} } 21

Page 22: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

User Input from ProcessingControlling Multiple Actuator On Arduino

22

Page 23: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

User Input from ProcessingControlling Multiple Actuator On Arduino

// THis is processing Codeimport processing.serial.*;Serial myPort; color colorA,colorB,colorC= color(0,0,0);void setup(){ background(255,255,255); size(300,100); myPort = new Serial(this, Serial.list()[1], 9600); fill(0,0,0);}void draw(){ fill(colorA); ellipse(50,50,50,50); fill(colorB); ellipse(150,50,50,50); fill(colorC); ellipse(250,50,50,50); }void mouseClicked(){ if (((mouseX>25)&&(mouseX<75))&&((mouseY>25)&&(mouseY<75))) if (red(colorA)==0){ colorA=color(255,0,0); myPort.write(11); }else{ colorA=color(0,0,0); myPort.write(10); } if (((mouseX>125)&&(mouseX<175))&&((mouseY>25)&&(mouseY<75))) if (red(colorB)==0){ colorB=color(255,0,0); myPort.write(21); }else{ colorB=color(0,0,0); myPort.write(20); } if (((mouseX>225)&&(mouseX<275))&&((mouseY>25)&&(mouseY<75))) if (red(colorC)==0){ colorC=color(255,0,0); myPort.write(31); }else{ colorC=color(0,0,0); myPort.write(30); }}

// This is Arduino Code //LED Connects to pin 13,12,11 int val = 0; void setup() { pinMode(13, OUTPUT); // sets the digital pin as output pinMode(12, OUTPUT); // sets the digital pin as output pinMode(11, OUTPUT); // sets the digital pin as output

Serial.begin(9600); } void loop() {

val = Serial.read(); if (val==10){digitalWrite(13,LOW);} if (val==11){digitalWrite(13,HIGH);} if (val==20){digitalWrite(12,LOW);} if (val==21){digitalWrite(12,HIGH);} if (val==30){digitalWrite(11,LOW);} if (val==31){digitalWrite(11,HIGH);} } 23

Page 24: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

User Input from ProcessingSending Multiple Analog Data to Arduino-

RGB Colors

24

Page 25: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

User Input from ProcessingSending Multiple Analog Data to Arduino-

RGB Colors

25

Page 26: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

User Input from ProcessingSending Multiple Analog Data to Arduino-

RGB Colors// This is Arduino Code

//maximum Amount that you can send to Arduino from Processing is 255int redPin = 11; // this function works on pins 3, 5, 6, 9, 10, and 11. int greenPin = 10;int bluePin = 9;int val = 0;int valR,valG,valB;void setup(){ pinMode(redPin, OUTPUT); // sets the digital pin as output pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); Serial.begin(9600);}

void loop(){ val = Serial.read(); if ((val>-1)&&(val<85)) {// recieved data defines the Red value valR=val*3; } else if ((val>84)&&(val<180)) {// recieved data defines the Green value valG=(val-85)*3; } else if (val>179) {// recieved data defines the Blue value valB=(val-180)*3; } analogWrite(redPin, valR); analogWrite(greenPin, valG); analogWrite(bluePin, valB);} 26

// This is Processing Codeimport processing.serial.*;Serial myPort;int k=255; color c;void setup(){ size(255,255); myPort = new Serial(this, Serial.list()[0], 9600); colorMode(HSB); for(int j=0;j<256;j++) for(int i=0;i<256;i++) { stroke(i,j,k); point(i,255-j); } fill(255); stroke(0); rect(255,0,10,255); line(255,127,265,127);}void draw(){ c= get(mouseX,mouseY); int Red= int(map(red(c),0,255,0,84)); int Green=int(map(green(c),0,255,85,179)); int Blue=int(map(blue(c),0,255,180,255)); myPort.write(Red); myPort.write(Green); myPort.write(Blue); println(Red); println(Green); println(Blue);}

Page 27: DataInputFrom Digital PinsUsing an On/OFF button to control an LEDFrom Analog Pins Reading light intensity using a photocell and turning an LED on and

User Input from ProcessingSending Multiple Analog Data to Arduino

Red=0-84 Green=85-179 Blue=180-255

Sensed Value :Red =0-255 Green=0-255 Blue=0-255

Red =0-255 Green=0-255 Blue=0-255

Red =Red/3 Green=Green/3+85 Blue=Blue/3+180

Red=Red*3 Green=(Green-85)*3 Blue=(Blue-160)*3

Change the Sensed Data to Identifiable Ranges

The actual values that are passed to Processing

Changing the Range of the Value back to 0-255

The Changed Values that result in Actuation of Space

Sent Values: Red=0-84 Green=85-179 Blue=180-255

While Sending Data fromProcessing to Arduino the Sendable data range is 0-25527