Processing code:
import oscP5.*; // Load OSC P5 library
import netP5.*; // Load net P5 library
import processing.serial.*; // Load serial library
Serial arduinoPort; // Set arduinoPort as serial connection
OscP5 oscP5; // Set oscP5 as OSC connection
int E2 = 0; // redLED lets us know if the LED is on or off
int E1 = 0;
int M1;
int M2;
int [] led = new int [5]; // Array allows us to add more toggle buttons in TouchOSC
int photocellVal;
void setup() {
size(300,300); // Processing screen size
noStroke(); // We don’t want an outline or Stroke on our graphics
oscP5 = new OscP5(this,8000); // Start oscP5, listening for incoming messages at port 8000
arduinoPort = new Serial(this, Serial.list()[1], 9600); // Set arduino to 9600 baud
}
void oscEvent(OscMessage theOscMessage) { // This runs whenever there is a new OSC message
String addr = theOscMessage.addrPattern(); // Creates a string out of the OSC message
if(addr.indexOf("/1/rotary") !=-1){ // Filters out any toggle buttons
int i = int((addr.charAt(9) )) - 0x30; // returns the ASCII number so convert into a real number by subtracting 0x30
led[i] = int(theOscMessage.get(0).floatValue()); // Puts button value into led[i]
// Button values can be read by using led[0], led[1], led[2], etc.
}
if(addr.indexOf("/1/toggle") !=-1){ // Filters out any toggle buttons
int i = int((addr.charAt(9) )) - 0x30; // returns the ASCII number so convert into a real number by subtracting 0x30
led[i] = int(theOscMessage.get(0).floatValue()); // Puts button value into led[i]
//Button values can be read by using led[0], led[1], led[2], etc.
}
}
void photocell(int serial){
if (photocellVal > 50)
{
arduinoPort.write("S");
E2= 0;
E1= 0;
if(led[3] == 1){ // If led button 1 if off do....
arduinoPort.write("S"); // Sends the character “r” to Arduino
E2 = 0; // Sets redLED color to 0, can be 0-255
E1 = 0;
}
else{
if(led[4] >= 155 && led[4] <=255){ // If led button 1 if off do....
arduinoPort.write("R"); // Sends the character “r” to Arduino
E2 = 255; // Sets redLED color to 0, can be 0-255
E1 = 255;
}
if(led[4] <= 110 && led[4] >=10){ // If led button 1 is ON do...
arduinoPort.write("L"); // Send the character “R” to Arduino
E2 = 255; // Sets redLED color to 255, can be 0-255
E1 = 255;
}
if(led[1] ==1){ // If led button 1 if off do....
arduinoPort.write("F"); // Sends the character “r” to Arduino
E2 = 255; // Sets redLED color to 0, can be 0-255
E1 = 255;
}
if(led[2] ==1){ // If led button 1 is ON do...
arduinoPort.write("B"); // Send the character “R” to Arduino
E2 = 255; // Sets redLED color to 255, can be 0-255
E1 = 255;
}
}
}
}
void draw() {
background(50); // Sets the background to a dark grey, can be 0-255
if (arduinoPort.available()>0)
{
photocellVal= arduinoPort.read();
photocell(photocellVal);
}
if(led[3] == 1){ // If led button 1 if off do....
arduinoPort.write("S"); // Sends the character “r” to Arduino
E2 = 0; // Sets redLED color to 0, can be 0-255
E1 = 0;
}
else{
if(led[4] >= 155 && led[4] <=255){ // If led button 1 if off do....
arduinoPort.write("R"); // Sends the character “r” to Arduino
E2 = 255; // Sets redLED color to 0, can be 0-255
E1 = 165;
}
if(led[4] <= 110 && led[4] >=10){ // If led button 1 is ON do...
arduinoPort.write("L"); // Send the character “R” to Arduino
E2 = 165; // Sets redLED color to 255, can be 0-255
E1 = 255;
}
if(led[1] ==1){ // If led button 1 if off do....
arduinoPort.write("F"); // Sends the character “r” to Arduino
E2 = 255; // Sets redLED color to 0, can be 0-255
E1 = 255;
}
if(led[2] ==1){ // If led button 1 is ON do...
arduinoPort.write("B"); // Send the character “R” to Arduino
E2 = 255; // Sets redLED color to 255, can be 0-255
E1 = 255;
}
}
fill(E2,E1,0);// Fill rectangle with redLED amount
ellipse(150,150,150,150);
// 50 pixels from the top and a width of 50 and height of 50 pixels
}
Arduino code:
int message = 0; // This will hold one byte of the serial message
int E2Pin = 6; // SPD What pin is the red LED connected to?
int M1Pin = 4;
int E1Pin = 5;
int M2Pin = 7;
int E1 = 0;
int E2 = 0; // Direction The value/brightness of the LED, can be 0-255
int M1;
int M2;
int sensorPin = 1; //analog pin 0
int ledPin = 13;
int photocellPin = 1;// Photocell connected to analog pin 0
int photocellVal; // define photocell variable
int i;
void setup() {
Serial.begin(9600); //set serial to 9600 baud rate
pinMode(ledPin,OUTPUT);
pinMode(photocellPin, INPUT);
}
void loop(){
//int val = analogRead(sensorPin);
//Serial.println(val);
//if( val > 300)
//{
//digitalWrite(ledPin, HIGH);
//E2 = 0; // Set redLED to 255 (on)
//E1 = 0;
//}
//if( val < 299)
//{
//digitalWrite(ledPin, LOW);
//}
photocellVal = analogRead(photocellPin);// read the analog from photocell
Serial.println( photocellVal); // print to screen
if( photocellVal > 50 )//Out of the line
{
digitalWrite(ledPin, HIGH);
E2 = 0;
E1 = 0;
//delay(2000);
if (Serial.available() > 0) { // Check if there is a new message
message = Serial.read(); // Put the serial input into the message
if (message == 'S'){ // If a capitol R is received...
E2 = 0; // Set redLED to 255 (on)
E1 = 0;
}
if (message == 'R'){ // If a capitol R is received...
E2 = 255; // Set redLED to 255 (on)
E1 = 255;
M1 = LOW;
M2 = HIGH;
}
if (message == 'L'){ // If a lowercase r is received...
E2 = 255; // Set redLED to 0 (off)
E1 = 255;
M1 = HIGH;
M2 = LOW;
}
if (message == 'F'){ // If a capitol R is received...
E2 = 255; // Set redLED to 255 (on)
E1 = 255;
M1 = HIGH;
M2 = HIGH;
}
if (message == 'B'){ // If a lowercase r is received...
E2 = 255; // Set redLED to 0 (off)
E1 = 255;
M1 = LOW;
M2 = LOW;
}
}
}
if( photocellVal < 50)//IN THE LINE
{
digitalWrite(ledPin, LOW);
if (Serial.available() > 0) { // Check if there is a new message
message = Serial.read(); // Put the serial input into the message
if (message == 'S'){ // If a capitol R is received...
E2 = 0; // Set redLED to 255 (on)
E1 = 0;
}
if (message == 'R'){ // If a capitol R is received...
E2 = 255; // Set redLED to 255 (on)
E1 = 165;
M1 = HIGH;
M2 = HIGH;
}
if (message == 'L'){ // If a lowercase r is received...
E2 = 165; // Set redLED to 0 (off)
E1 = 255;
M1 = HIGH;
M2 = HIGH;
}
if (message == 'F'){ // If a capitol R is received...
E2 = 255; // Set redLED to 255 (on)
E1 = 255;
M1 = HIGH;
M2 = HIGH;
}
if (message == 'B'){ // If a lowercase r is received...
E2 = 255; // Set redLED to 0 (off)
E1 = 255;
M1 = LOW;
M2 = LOW;
}
}
}
delay(200);
analogWrite(E2Pin, E2); // Write an analog value between 0-255
analogWrite(E1Pin, E1);
digitalWrite(M1Pin, M1);
digitalWrite(M2Pin, M2);
}
Can someone help?
I don't know why the lag is from?
1