We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have created a program that allows you to use processing to communicate to LEDs connected to a nano. The processing code allows the user to select an LED colour and how many times the LED will flash. Now, I have to connect a push button to the circuit. This is what I'm struggling with. When I push the button, it is supposed to bring the processing screen back to its original state and turn off all the LEDs. I have put the code in Arduino for the button push, but I have no idea what kind of code I need to communicate this back to Processing. I haven't put any code into Processing yet for the push button. Any help would be greatly appreciated.
Processing code...
PFont jpFont, ebFont;
int iLED=0, iCount=0, iData=0;
import processing.serial.*;
Serial rrSerial;
void setup()
{
size(1000,500);
jpFont=loadFont("Tahoma-48.vlw");
ebFont=loadFont("Tahoma-Bold-48.vlw");
println(Serial.list()[2]);
rrSerial=new Serial(this, Serial.list()[2],9600);
rrSerial.write(0x83);
}
void draw()
{
background(100);
fill(255,0,0);
stroke(0);
strokeWeight(10);
ellipse(width/2-350,height/2+100,200,200);
fill(0,255,0);
stroke(0);
strokeWeight(10);
ellipse(width/2-125,height/2+100,200,200);
fill(0,0,255);
stroke(0);
strokeWeight(10);
ellipse(width/2+100,height/2+100,200,200);
fill(247,142,12);
stroke(0);
strokeWeight(10);
ellipse(width/2+325,height/2+100,200,200);
if(iLED==1)
{
textFont(ebFont);
textSize(75);
fill(#F7F70C);
text(""+iCount,125,375);
}
if(iLED==2)
{
textFont(ebFont);
textSize(75);
fill(#F7F70C);
text(""+iCount,350,375);
}
if(iLED==3)
{
textFont(ebFont);
textSize(75);
fill(#F7F70C);
text(""+iCount,575,375);
}
if(iLED==4)
{
textFont(ebFont);
textSize(75);
fill(#F7F70C);
text(""+iCount,800,375);
}
iData=iLED<<4|iCount&0x0F;
}
void keyPressed()
{
if(keyCode==RIGHT&&iLED<5)
{
iLED++;
iCount=0;
if(iLED==5)iLED=0;
}
if(keyCode==LEFT&&iLED>0)
{
iLED--;
iCount=0;
if(iLED==0)iLED=5;
}
if(keyCode==UP&&iCount<10)
{
iCount++;
if(iCount==10)iCount=0;
}
if(keyCode==DOWN&&iCount>-1)
{
iCount--;
if(iCount==-1)iCount=9;
}
if(key==ENTER)rrSerial.write(iData);
if(key=='q')exit();
}
Now Arduino...
boolean bToggle=0;
int iButton;
void setup()
{
Serial.begin(9600);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(9,OUTPUT);
pinMode(11,INPUT);
digitalWrite(3,LOW);
digitalWrite(5,LOW);
digitalWrite(9,LOW);
digitalWrite(11,HIGH);
}
void loop()
{
if(Serial.available()>0)
{
int iInput=Serial.read();
int iWhich_Led=iInput>>4;
int iFlash_Count=iInput&0x0f;
if(iWhich_Led==1)LED_burst(3,iFlash_Count);
if(iWhich_Led==2)LED_burst(5,iFlash_Count);
if(iWhich_Led==3)LED_burst(9,iFlash_Count);
if(iWhich_Led==4)LED_sequence(iFlash_Count);
}
iButton=digitalRead(11);
if(iButton==0)
{
while(digitalRead(11)==0);
bToggle=!bToggle;
}
if(bToggle==1)
{
digitalWrite(3,LOW);
digitalWrite(5,LOW);
digitalWrite(9,LOW);
}
}
void LED_burst(int iLED,int iCount)
{
for(int iX=0;iX<iCount;iX++)
{
digitalWrite(iLED,HIGH);
delay(100);
digitalWrite(iLED,LOW);
delay(100);
}
}
void LED_sequence(int iCountx)
{
for(int iY=0;iY<iCountx;iY++)
{
digitalWrite(3,HIGH);
delay(50);
digitalWrite(3,LOW);
delay(50);
digitalWrite(5,HIGH);
delay(50);
digitalWrite(5,LOW);
delay(50);
digitalWrite(9,HIGH);
delay(50);
digitalWrite(9,LOW);
delay(50);
}
}
Answers
Here is a very basic example for a communication from arduino to processing:
Arduino:
Processing:
That should be easy to integrate with your current code. Just send any character when your button is pressed and then on the processing side, check for that character in serialEvent().