From Digital Pins Using an OnOFF button to
From Digital Pins Using an On/OFF button to control an LED From Analog Pins Reading light intensity using a photocell and turning an LED on and off based on the read data From Processing in realtime Controlling an LED from user manipulation in processing From File Turning and LED on and off based on the data that is being read in real-time from a file From Internet Through Processing Turning an LED on and off based on input via telepresence From SMS Thorough Processing Turning an LED on and off based on user input via text messaging Output to Digital Pins Turning an LED On and Off Output to Digital Pins with Analog Support Diming the light intensity of an LED higher or lower Output to Processing in real-time Controlling what is happening on the screen in Processing based on what a photocell is reading as light intensity Output to File Writing the read light intensity from a photocell to a file for later use Output to Internet Through Processing Sending the read light intensity from the photocell to a distant location via processing running an applet to connect to Internet 1 Input Data Output
Digital Input – Output to Processing Controlling the Screen with a Button 1. Controlling an LED with Push A push‐button will open (or close) a circuit when pressed. Its circuit is shown Button below. The black wire goes to ground, the yellow to the pin (2 for this case), and the 10 K resistor connects to 5 V. If you connect the 10 K resistor to ground and the black wire to 5 V you inverse the state of the push button. 2
Digital Input – Output to Processing Controlling the Screen with a Button 1. Controlling an LED with Push A push‐button will open (or close) a circuit when pressed. Its circuit is shown Button below. The black wire goes to ground, the yellow to the pin (2 for this case), and the 10 K resistor connects to 5 V. 3
Digital Input – Output to Processing Controlling the Screen with a Button 1. Controlling an LED with Push A push‐button will open (or close) a circuit when pressed. Its circuit is shown Button below. The black wire goes to ground, the yellow to the pin (2 for this case), and the 10 K resistor connects to 5 V. void setup(){ Serial. begin(9600); //start the serial port in order to write pin. Mode(2, INPUT); // set the pin 5 as input pin. Mode(13, OUTPUT); // set the pin 13 as output } void loop(){ int val = digital. Read(2); //read from the pin 5 if(val==LOW) { digital. Write(13, LOW); // sets the LED off } else { digital. Write(13, HIGH); // sets the LED on } Serial. println(val); } 4
Digital Input – Output to Processing Controlling the Screen with a Button 2. Controlling Processing with Push Button a. Upload the Arduino Code b. Stop Arduino c. Run Processing //This is Arduino Code which port is available and what is // This d. Check theis Processing index of. Code the available port void setup(){ import processing. serial. *; e. Change theserial Index inorder Processing code int val = 1; Serial. begin(9600); //start the port in to write pin. Mode(2, INPUT); // set the pin 5 as input pin. Mode(13, OUTPUT); // set the pin 13 as output } void loop(){ int val = digital. Read(2); //read from the pin 5 if(val==LOW) { digital. Write(13, LOW); // sets the LED off } else { digital. Write(13, HIGH); // sets the LED on } Serial. println(val); } Serial port; void setup(){ size(100, 100); // Use the first available 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) serial. Event(port. read()); //look for data if (val==1)background(255); if (val==0)background(0); } void serial. Event(int serial) { val=serial-48; // Change the character ascii code to numeric 5 value println(val);
Analog Input – Output to Processing Controlling the Screen with a Photocell void setup(){ Serial. begin(9600); } void loop(){ int in = analog. Read(5); Serial. println(in); } 6
Analog Input – Output to Processing Controlling the Screen with a Photocell 7
Analog Input – Output to Processing Controlling the Screen with a Photocell import processing. serial. *; int val = 1; String buff = ""; int NEWLINE = 10; Serial port; void setup() { size(400, 400); // Use the first available 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); //This is Arduino Code void setup(){ Serial. begin(9600); } void loop(){ int in = analog. Read(5); Serial. println(in); } } void draw() { while (port. available() > 0) serial. Event(port. read()); //look for data background(val); } void serial. Event(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. parse. Int(buff); val=int(map(val, 0, 800, 0, 255)); } } // Clear the value of "buff" buff = ""; } } 8
Analog Input – Output to Processing Painting the Screen with a Photocell 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 = 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) serial. Event(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; //This is Arduino Code void setup(){ Serial. begin(9600); } void loop(){ int in = analog. Read(5); Serial. println(in); } // The dial gets to the limits of the screen, //change direction } void serial. Event(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. parse. Int(buff); val=int(map(val, 0, 800, 0, 255)); } } 9
Analog Input – Output to Processing Painting the Screen with a Two Photocell Analog Pin 4 V 5 GND Analog Pin 5 10
Analog Input – Output to Processing Controlling the Screen with Two a Photocell 11
Analog Input – Output to Processing Painting the Screen with Two a Photocell 12
Analog Input – Output to Processing Painting the Screen with Two a Photocell //This is Processing Code import processing. serial. *; int val = 1; String buff = ""; int NEWLINE = 10; Serial port; int count=0; Int dir=1; int val. A, val. B; int light_Min=0; int light_Max=750; void setup(){ size(400, 200); // Use the first available 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) serial. Event(port. read()); //look for data stroke(val. A); //Set the stroke color based on the sensed data line(count, 0, count, 100); //Draw a line stroke(val. B); line(count, 100, count, 200); //Draw a line count=count+dir; //Go one pixel forwards or backwards if ((count>width)||(count<0)) dir=-dir; } //This is Arduino Code void setup(){ Serial. begin(9600); } void loop(){ int in. A = analog. Read(5); int in. B = analog. Read(4); in. A=1000+in. A; // The photocell A sends data in 1000 Range in. B=2000+in. B; // The photocell B sends data in 2000 Range Serial. println(in. A); Serial. println(in. B); void serial. Event(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. parse. Int(buff); if (val<2000) val. A=int(map((val-1000), light_Min, light_Max, 0, 255)); if (val>=2000) val. B=int(map((val-2000), light_Min, light_Max, 0, 255)); println("A="+val. A); println("B="+val. B); } } // Clear the value of "buff" buff = ""; } } 13
Input – Output to file via Processing //This is Processing Code import processing. serial. *; Print. Writer output; int val = 1; String buff = ""; int NEWLINE = 10; Serial port; int val. A, val. B; int light_Min=0; int light_Max=895; void setup(){ size(200, 100); // Create a new file in the sketch directory output = create. Writer("log. txt"); // Use the first available 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) serial. Event(port. read()); //look for data output. println("A"+val. A+", "+"B"+val. B); // Write the coordinate to the fill(val. A); rect(0, 0, 100); fill(val. B); rect(100, 0, 100); } //This is Arduino Code void setup(){ Serial. begin(9600); } void loop(){ int in. A = analog. Read(5); int in. B = analog. Read(4); in. A=1000+in. A; // The photocell A sends data in 1000 Range in. B=2000+in. B; // The photocell B sends data in 2000 Range Serial. println(in. A); Serial. println(in. B); void serial. Event(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. parse. Int(buff); if (val<2000) val. A=int(map((val-1000), light_Min, light_Max, 0, 255)); if (val>=2000) val. B=int(map((val-2000), light_Min, light_Max, 0, 255)); println("A="+val. A); println("B="+val. B); } } // Clear the value of "buff" buff = ""; } } void key. Pressed() { output. flush(); // Writes the remaining data to the file 14 output. close(); // Finishes the file exit(); // Stops the program
Reading Data from File. Making Graphs String lines[]; // Array of all the lines in the file String light_A[]; // Array of all the light readings of point A String light_B[]; // Array of all the light readings of point B int counter=0; // counter that changes value in each loop int graph. Scale; // The scale of the graph int x. Prev. A, x. Prev. B; //x Value of Previous point on the graph int avr. A, avr. B; int y. Prev. A, y. Prev. B; // y Value of Previouspoint on the graph void setup(){ size(800, 600); background(255); y. Prev. A=height-10; // Starting from the base line y. Prev. B=height-310; // Starting from the base lines= load. Strings("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]); } graph. Scale=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); fill(int(light_B[counter])); // Change the fill color of the rectangle representing the light intensity at point B rect(100, 0, 100); // Scaleing and drawing the Graphs if(counter%graph. Scale==0){ avr. A=avr. A/graph. Scale; // get the avrage of the reads for Point A avr. B=avr. B/graph. Scale; // get the avrage of the reads for Point B stroke(255, 0, 0); // Color of the grapg for Point A line(x. Prev. A, y. Prev. A, counter/graph. Scale, height-10 -avr. A); // Draw the graph segment for Point A stroke(0, 255, 0); // Color of the grapg for Point B line(x. Prev. B, y. Prev. B, counter/graph. Scale, height-310 -avr. B); // Draw the graph segment for Point B x. Prev. A=counter/graph. Scale; x. Prev. B=counter/graph. Scale; y. Prev. A=height-10 -avr. A; y. Prev. B=height-310 -avr. B; avr. A=0; avr. B=0; } else{ avr. A=avr. A+int(light_A[counter]); 15
Controlling Processing with Multiple input from Arduino - Making RGB colors on Screen Manipulating Potentiometers 16
Analog Pin 5 v 5 Analog Pin 4 Ground Analog Pin 3 Controlling Processing with Multiple input from Arduino - Making RGB colors on Screen Manipulating Potentiometers 17
Controlling Processing with Multiple input from Arduino - Making RGB colors on Screen Manipulating Potentiometers //This is Processing Code import 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 = 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) serial. Event(port. read()); //look for data color c=color(r, g, b); background(c); //This is Arduino Code void setup(){ Serial. begin(9600); } void loop(){ int in. R = analog. Read(5); int in. G = analog. Read(4); int in. B = analog. Read(3); } void serial. Event(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. parse. Int(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)); in. R=1000+in. R; // The Red ranges from 1000 -2023 in. G=3000+in. G; // The Green ranges from 3000 -4023 in. B=5000+in. B; // The Blue ranges from 5000 -6023 Serial. println(in. R); println("R="+r); println("G="+g); println("B="+b); } } // Clear the value of "buff" buff = ""; } } 18
From Digital Pins Using an On/OFF button to control an LED From Analog Pins Reading light intensity using a photocell and turning an LED on and off based on the read data From Processing in realtime Controlling an LED from user manipulation in processing From File Turning and LED on and off based on the data that is being read in real-time from a file From Internet Through Processing Turning an LED on and off based on input via telepresence From SMS Thorough Processing Turning an LED on and off based on user input via text messaging Output to Digital Pins Turning an LED On and Off Output to Digital Pins with Analog Support Diming the light intensity of an LED higher or lower Output to Processing in real-time Controlling what is happening on the screen in Processing based on what a photocell is reading as light intensity Output to File Writing the read light intensity from a photocell to a file for later use Output to Internet Through Processing Sending the read light intensity from the photocell to a distant location via processing running an applet to connect to Internet 19 Input Data Output
User Input from Processing Controlling one Actuator On Arduino 20
User Input from Processing Controlling one Actuator On Arduino // THis is processing Code import processing. serial. *; Serial my. Port; color c= color(0, 0, 0); void setup(){ background(255, 255); size(100, 100); my. Port = new Serial(this, Serial. list()[1], 9600); // This is Arduino Code int led. Pin = 13; // LED connected to digital pin 13 int val = 0; void setup() { pin. Mode(led. Pin, OUTPUT); // sets the digital pin as output Serial. begin(9600); } void loop() { val = Serial. read(); if (val==0){digital. Write(13, LOW); } if (val==1){digital. Write(13, HIGH); } } fill(0, 0, 0); } void draw(){ fill(c); ellipse(50, 50, 50); } void mouse. Clicked(){ if (((mouse. X>25)&&(mouse. X<75))&&((mouse. Y>25)&&(mou se. Y<75))) if (red(c)==0){ c=color(255, 0, 0); my. Port. write(1); } else{ c=color(0, 0, 0); 21 my. Port. write(0);
User Input from Processing Controlling Multiple Actuator On Arduino 22
User Input from Processing Controlling Multiple Actuator On Arduino // This is Arduino Code //LED Connects to pin 13, 12, 11 int val = 0; void setup() { pin. Mode(13, OUTPUT); // sets the digital pin as output pin. Mode(12, OUTPUT); // sets the digital pin as output pin. Mode(11, OUTPUT); // sets the digital pin as output Serial. begin(9600); } void loop() { val = Serial. read(); if (val==10){digital. Write(13, LOW); } if (val==11){digital. Write(13, HIGH); } if // THis is processing Code import processing. serial. *; Serial my. Port; color. A, color. B, color. C= color(0, 0, 0); void setup(){ background(255, 255); size(300, 100); my. Port = new Serial(this, Serial. list()[1], 9600); fill(0, 0, 0); } void draw(){ fill(color. A); ellipse(50, 50, 50); fill(color. B); ellipse(150, 50, 50); fill(color. C); ellipse(250, 50, 50); } void mouse. Clicked(){ if (((mouse. X>25)&&(mouse. X<75))&&((mouse. Y>25)&&(mouse. Y<75))) if (red(color. A)==0){ color. A=color(255, 0, 0); my. Port. write(11); }else{ color. A=color(0, 0, 0); my. Port. write(10); } if (((mouse. X>125)&&(mouse. X<175))&&((mouse. Y>25)&&(mouse. Y<75))) if (red(color. B)==0){ color. B=color(255, 0, 0); my. Port. write(21); }else{ color. B=color(0, 0, 0); my. Port. write(20); } if (((mouse. X>225)&&(mouse. X<275))&&((mouse. Y>25)&&(mouse. Y<75))) if (red(color. C)==0){ color. C=color(255, 0, 0); my. Port. write(31); }else{ color. C=color(0, 0, 0); my. Port. write(30); } } 23
User Input from Processing Sending Multiple Analog Data to Arduino-RGB Colors 24
User Input from Processing Sending Multiple Analog Data to Arduino-RGB Colors 25
User Input from Processing Sending Multiple Analog Data to Arduino-RGB Colors // This is Arduino Code //maximum Amount that you can send to Arduino from Processing is 255 int red. Pin = 11; // this function works on pins 3, 5, 6, 9, 10, and 11. int green. Pin = 10; int blue. Pin = 9; int val = 0; int val. R, val. G, val. B; void setup() { pin. Mode(red. Pin, OUTPUT); // sets the digital pin as output pin. Mode(green. Pin, OUTPUT); pin. Mode(blue. Pin, OUTPUT); Serial. begin(9600); } void loop() { val = Serial. read(); if ((val>-1)&&(val<85)) {// recieved data defines the Red value val. R=val*3; } else if ((val>84)&&(val<180)) {// recieved data defines the Green value val. G=(val-85)*3; } else if (val>179) {// recieved data defines the Blue val. B=(val-180)*3; } analog. Write(red. Pin, val. R); analog. Write(green. Pin, val. G); analog. Write(blue. Pin, val. B); } // This is Processing Code import processing. serial. *; Serial my. Port; int k=255; color c; void setup(){ size(255, 255); my. Port = new Serial(this, Serial. list()[0], 9600); color. Mode(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(mouse. X, mouse. Y); 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)); my. Port. write(Red); my. Port. write(Green); my. Port. write(Blue); println(Red); println(Green); println(Blue); } 26
User Input from Processing Sending Multiple Analog Data to : Red=0 -84 Green=85 -179 Blue=180 -255 Arduino : Red =0 -255 Green=0 -255 Blue=0 -255 Sent Values Sensed Value Change the Sensed Data to Identifiable Ranges Red =Red/3 Green=Green/3+85 Blue=Blue/3+180 The actual values that are passed to Processing Red=0 -84 Green=85 -179 Blue=180 -255 Changing the Range of the Value back to 0 -255 Red=Red*3 Green=(Green-85)*3 Blue=(Blue-160)*3 The Changed Values that result in Actuation of Space Red =0 -255 Green=0 -255 Blue=0 -255 27 While Sending Data from. Processing to Arduino the Sendable data ra
- Slides: 27