Sunday 25 September 2022

Blink LED with processing IDE | Arduino processing serial data tutorial [Full guide]

 https://srituhobby.com/processing-ide-with-led-on-and-off/

 

https://www.youtube.com/watch?v=XFEWhu9dqBg 



Arduino IDE Sketch

/*LED on and off using the processing IDE.
  created by the SriTu Tech team.
  Read the code below and use it for any of your creation
*/
void setup() {
  Serial.begin(9600);//enable serial communication
  pinMode(2, OUTPUT);//define arduino pin
}
void loop() {
  if (Serial.available() > 0) {//check serial value
    char led = Serial.read();//read serial value
    if (led == '1') {//check serial read 
      digitalWrite(2, HIGH);//LED on
    } else if (led == '0') {//check serial read 
      digitalWrite(2, LOW);//LED off
    }
  }
}


Processing IDE Sketch


/*LED on and off using the processing IDE.
  The second time uploads this code.
  created by the SriTu Tech team.
  Read the code below and use it for any of your creation
*/
import processing.serial.*;//import serial communicate library
 
Serial myPort;//create serial object
 
int circleOneX, circleOneY;      // Position of button one
int circleX, circleY;  // Position of button two
int circleOnesize = 90;     // Diameter button one
int circleSize = 90;   // Diameter of button two
 
color currentColor, baseColor;//color variable
color circleOnecolor, circleColor;//color variable
color circleOneHighlight, circleHighlight;//color variable
 
boolean circleOneOver = false;
boolean circleOver = false;
 
PFont f;//font type
void setup() {
  size(300, 200);//screen size
  circleOnecolor = color(0);//set colors to variable
  circleOneHighlight = color(51);//set colors to variable
  circleColor = color(255);//set colors to variable
  circleHighlight = color(204);//set colors to variable
  baseColor = color(102);//set colors to variable
  currentColor = baseColor;

  circleX = width/2+circleSize/2+25;
  circleY = height/2;
  circleOneX = width/2-circleOnesize+15;
  circleOneY = height/2;
 
  printArray(PFont.list());//print fonts list your computer
  f = createFont("Ebrima Bold", 20);//Enter your font name
  textFont(f);
 
 
  String portName = "COM4";//Enter your COM port
  myPort = new Serial(this, portName, 9600);//select first port
  print(portName);
}
 
void draw() {
  update(mouseX, mouseY);//main method
  background(currentColor);//background color
 
  if (circleOneOver) {
    fill(circleOneHighlight);
  } else {
    fill(circleOnecolor);
  }
  stroke(255);
  ellipse(circleOneX, circleOneY, circleOnesize, circleOnesize);//drow circle one
 
  if (circleOver) {
    fill(circleHighlight);
  } else {
    fill(circleColor);
  }
  stroke(0);
  ellipse(circleX, circleY, circleSize, circleSize);//drow circle two
 
  textAlign(LEFT);
  fill(255);//black color
  text("LED OFF", 37, 110);//display text
  
 
  textAlign(RIGHT);
  fill(0);//white color
  text("LED ON", 255, 110);//display text
  
}
 
void update(int x, int y) {
  if ( overCircle(circleX, circleY, circleSize) ) {
    circleOver = true;
    circleOneOver = false;
  } else if (circleOneOver(circleOneX, circleOneY, circleOnesize) ) {
    circleOneOver = true;
    circleOver = false;
  } else {
    circleOver = circleOneOver = false;
  }
}
 
void mousePressed() {
  if (circleOver) {
    currentColor = circleColor;
    myPort.write('1');//send value one
    print("LED on");
  }
  if (circleOneOver) {
    currentColor = circleOnecolor;
    myPort.write('0');//send value ziro
    print("LED off");
  }
}
 
boolean circleOneOver(int x, int y, int diameter) {
  float disX = x - mouseX;
  float disY = y - mouseY;
  if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
    return true;
  } else {
    return false;
  }
}
 
boolean overCircle(int x, int y, int diameter) {
  float disX = x - mouseX;
  float disY = y - mouseY;
  if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
    return true;
  } else {
    return false;
  }
}


















No comments:

Post a Comment