Skip to main content

A PROCESSING UI FOR CONTROLLING ARDUINO DIGITAL PINS

This is just a simple Processing program and its corresponding Arduino program for controlling the digital pins of Arduino. It is done by using the processing serial library 

Processing code

import processing.serial.*;
Serial n;
color c1,c2,c3;
color h1,h2;
boolean onb0=false;
boolean onb1=false;
boolean onb2=false;
boolean onb3=false;
boolean onb4=false;
boolean onb5=false;
boolean onb6=false;
boolean onb7=false;
boolean onb8=false;
boolean onb9=false;
boolean onb10=false;
boolean onb11=false;
boolean onb12=false;
boolean offb0=false;
boolean offb1=false;
boolean offb2=false;
boolean offb3=false;
boolean offb4=false;
boolean offb5=false;
boolean offb6=false;
boolean offb7=false;
boolean offb8=false;
boolean offb9=false;
boolean offb10=false;
boolean offb11=false;
boolean offb12=false;
boolean onb13=false;
boolean offb13=false;

void setup()
{
  size(800,800);
  n=new Serial(this,"/dev/ttyUSB0",9600);
  c1=color(80);
  h1=color(255,0,0);
  c2=color(255);
  h2=color(0,255,0);
  c3=color(0,0,255);
}

void draw()
{
  background(c3);
  fill(0,255,255);
  text("CONTROL DIGITAL PINS",5,15);
  fill(255,0,255);
  text("PIN 0",5,50);
  text("PIN 1",5,100);
  text("PIN 2",5,150);
  text("PIN 3",5,200);
  text("PIN 4",5,250);
  text("PIN 5",5,300);
  text("PIN 6",5,350);
  text("PIN 7",5,400);
  text("PIN 8",5,450);
  text("PIN 9",5,500);
  text("PIN 10",5,550);
  text("PIN 11",5,600);
  text("PIN 12",5,650);
  text("PIN 13",5,700);

  update(mouseX,mouseY);
 
   if (offb0) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,35,30,30);
 
  if (onb0) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,35,30,30);
  
   if (offb1) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,85,30,30);
 
  if (onb1) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,85,30,30);


if (offb2) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,135,30,30);
 
  if (onb2) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,135,30,30);


if (offb3) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,185,30,30);
 
  if (onb3) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,185,30,30);


if (offb4) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,235,30,30);
 
  if (onb4) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,235,30,30);


if (offb5) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,285,30,30);
 
  if (onb5) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,285,30,30);


if (offb6) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,335,30,30);
 
  if (onb6) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,335,30,30);


if (offb7) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,385,30,30);
 
  if (onb7) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,385,30,30);


if (offb8) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,435,30,30);
 
  if (onb8) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,435,30,30);


if (offb9) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,485,30,30);
 
  if (onb9) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,485,30,30);


if (offb10) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,535,30,30);
 
  if (onb10) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,535,30,30);


if (offb11) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,585,30,30);
 
  if (onb11) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,585,30,30);


if (offb12) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,635,30,30);
 
  if (onb12) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,635,30,30);
if (offb13) {
    fill(h1);
  } else {
    fill(c1);
  }
  stroke(255);
  rect(100,685,30,30);
 
  if (onb13) {
    fill(h2);
  } else {
    fill(c2);
  }
  stroke(0);
   rect(200,685,30,30);

}


void update(int x,int y)
{
 if(overButton(200,35,30,30))
{
 onb0=true;
 offb0=false;
}
else if(overButton(100,35,30,30))
{
  onb0=false;
  offb0=true;
}
else
{
  offb0=onb0=false;
}


if(overButton(200,85,30,30))
{
 onb1=true;
 offb1=false;
}
else if(overButton(100,85,30,30))
{
  onb1=false;
  offb1=true;
}
else
{
  offb1=onb1=false;
}


if(overButton(200,135,30,30))
{
 onb2=true;
 offb2=false;
}
else if(overButton(100,135,30,30))
{
  onb2=false;
  offb2=true;
}
else
{
  offb2=onb2=false;
}


if(overButton(200,185,30,30))
{
 onb3=true;
 offb3=false;
}
else if(overButton(100,185,30,30))
{
  onb3=false;
  offb3=true;
}
else
{
  offb3=onb3=false;
}


if(overButton(200,235,30,30))
{
 onb4=true;
 offb4=false;
}
else if(overButton(100,235,30,30))
{
  onb4=false;
  offb4=true;
}
else
{
  offb4=onb4=false;
}


if(overButton(200,285,30,30))
{
 onb5=true;
 offb5=false;
}
else if(overButton(100,285,30,30))
{
  onb5=false;
  offb5=true;
}
else
{
  offb5=onb5=false;
}


if(overButton(200,335,30,30))
{
 onb6=true;
 offb6=false;
}
else if(overButton(100,335,30,30))
{
  onb6=false;
  offb6=true;
}
else
{
  offb6=onb6=false;
}


if(overButton(200,385,30,30))
{
 onb7=true;
 offb7=false;
}
else if(overButton(100,385,30,30))
{
  onb7=false;
  offb7=true;
}
else
{
  offb7=onb7=false;
}


if(overButton(200,435,30,30))
{
 onb8=true;
 offb8=false;
}
else if(overButton(100,435,30,30))
{
  onb8=false;
  offb8=true;
}
else
{
  offb8=onb8=false;
}


if(overButton(200,485,30,30))
{
 onb9=true;
 offb9=false;
}
else if(overButton(100,485,30,30))
{
  onb9=false;
  offb9=true;
}
else
{
  offb9=onb9=false;
}


if(overButton(200,535,30,30))
{
 onb10=true;
 offb10=false;
}
else if(overButton(100,535,30,30))
{
  onb10=false;
  offb10=true;
}
else
{
  offb10=onb10=false;
}


if(overButton(200,585,30,30))
{
 onb11=true;
 offb11=false;
}
else if(overButton(100,585,30,30))
{
  onb11=false;
  offb11=true;
}
else
{
  offb11=onb11=false;
}


if(overButton(200,635,30,30))
{
 onb12=true;
 offb12=false;
}
else if(overButton(100,635,30,30))
{
  onb12=false;
  offb12=true;
}
else
{
  offb12=onb12=false;
}

if(overButton(200,685,30,30))
{
 onb13=true;
 offb13=false;
}
else if(overButton(100,685,30,30))
{
  onb13=false;
  offb13=true;
}
else
{
  offb13=onb13=false;
}

}
void mousePressed() {
  if (onb0) {
       n.write('0');
    println("0th Pin High");
  
  }
  if (offb0) {
   n.write('a');
    println("0th Pin Low");
  
  }
 
  if (onb1) {
       n.write('1');
    println("1st Pin High");
  
  }
  if (offb1) {
    n.write('b');
    println("1st Pin Low");
  
  }
 
  if (onb2) {
       n.write('2');
    println("2nd Pin High");
  
  }
  if (offb2) {

      n.write('c');
      println("2nd Pin Low");
  
  }
 
  if (onb3) {
       n.write('3');
    println("3rd Pin High");
  
  }
  if (offb3) {
      n.write('d');
      println("3rd Pin Low");
  
  }


if (onb4) {
      n.write('4');
      println("4th Pin High");
  
  }
  if (offb4) {
    n.write('e');
    println("4th Pin Low");
  
  }


if (onb5) {
       n.write('5');
    println("5th Pin High");
  
  }
  if (offb5) {
    n.write('f');
    println("5th Pin Low");
  
  }


if (onb6) {
       n.write('6');
    println("6th Pin High");
  
  }
  if (offb6) {
    n.write('g');
    println("6th Pin Low");
  
  }


if (onb7) {
       n.write('7');
    println("7th Pin High");
  
  }
  if (offb7) {
      n.write('h');
      println("7th Pin Low");
  
  }


if (onb8) {
       n.write('8');
      println("8th Pin High");
  
  }
  if (offb8) {
      n.write('i');
      println("8th Pin Low");
  
  }


if (onb9) {
       n.write('9');
    println("9th Pin High");
  
  }
  if (offb9) {
    n.write('j');
    println("9th Pin Low");
  
  }

if (onb10) {
       n.write('p');
    println("10th Pin High");
  
  }
  if (offb10) {
    n.write('k');
    println("10th Pin Low");
  
  }


if (onb11) {
       n.write('q');
    println("11th Pin High");
  
  }
  if (offb11) {
    n.write('l');
    println("11th Pin Low");
  
  }


if (onb12) {
       n.write('r');
    println("12th Pin High");
  
  }
  if (offb12) {
    n.write('m');
    println("12th Pin Low");
  
  }
   if (onb13) {
       n.write('s');
    println("13th Pin High");
  
  }
  if (offb13) {
      n.write('o');
      println("13th Pin Low");
  
  }


}

boolean overButton(int x,int y,int width,int height)
{
  if (mouseX >= x && mouseX <= x+width &&
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}



The processing UI will look like this.






Arduino Code



char val;
int lp0=0,lp1=1,lp2=2,lp3=3,lp4=4,lp5=5,lp6=6,lp7=7,lp8=8,lp9=9,lp10=10,lp11=11,lp12=12,lp13=13;
void setup()
{
  pinMode(lp0,OUTPUT);
  pinMode(lp1,OUTPUT);
  pinMode(lp2,OUTPUT);
  pinMode(lp3,OUTPUT);
  pinMode(lp4,OUTPUT);
  pinMode(lp5,OUTPUT);
  pinMode(lp6,OUTPUT);
  pinMode(lp7,OUTPUT);
  pinMode(lp8,OUTPUT);
  pinMode(lp9,OUTPUT);
  pinMode(lp10,OUTPUT);
  pinMode(lp11,OUTPUT);
  pinMode(lp12,OUTPUT);
  pinMode(lp13,OUTPUT);
  Serial.begin(9600);
  }
void loop()
{
  if(Serial.available())
  {
    val=Serial.read();
  }
  if(val=='0')
  {
    digitalWrite(lp0,HIGH);
  }
  else if(val=='a')
 {
   digitalWrite(lp0,LOW);
 }
 if(val=='1')
  {
    digitalWrite(lp1,HIGH);
  }
  else if(val=='b')
  {
    digitalWrite(lp1,LOW);
  }
  if(val=='2')
    {
      digitalWrite(lp2,HIGH);
    }
    else if(val=='c')
     {
      digitalWrite(lp2,LOW);
     }
    if(val=='3')
     {
       digitalWrite(lp3,HIGH);
     }
     else if(val=='d')
      {
        digitalWrite(lp3,LOW);
      }
    if(val=='4')
    {
      digitalWrite(lp4,HIGH);
    }
    else if(val=='e')
    {
      digitalWrite(lp4,LOW); 
    }
    if(val=='5')
    {
      digitalWrite(lp5,HIGH);
    }
    else if(val=='f')
   {
    digitalWrite(lp5,LOW);
   }
   if(val=='6')
    {
      digitalWrite(lp6,HIGH);
    }
    else if(val=='g')
   {
    digitalWrite(lp6,LOW);
   }
   if(val=='7')
    {
      digitalWrite(lp7,HIGH);
    }
    else if(val=='h')
   {
    digitalWrite(lp7,LOW);
   }
   if(val=='8')
    {
      digitalWrite(lp8,HIGH);
    }
    else if(val=='i')
   {
      digitalWrite(lp8,LOW); 
   }
   if(val=='9')
    {
      digitalWrite(lp9,HIGH);
    }
    else if(val=='j')
   {
    digitalWrite(lp9,LOW);
   }
   if(val=='p')
  {
    digitalWrite(lp10,HIGH);
  }
  else if(val=='k')
  {
    digitalWrite(lp10,LOW);
  }
  if(val=='q')
  {
    digitalWrite(lp11,HIGH);
  }
  else if(val=='l')
   {
      digitalWrite(lp11,LOW);
   }
   if(val=='r')
  {
    digitalWrite(lp12,HIGH);
  }
  else if(val=='m')
  {
   digitalWrite(lp12,LOW);
  }
  if(val=='s')
  {
    digitalWrite(lp13,HIGH);
  }
  else if(val=='o')
  {
   digitalWrite(lp13,LOW);
  }

  delay(10);
}


 

Comments