Skip to main content

Processing Projects


Arduino has it's own power but when combined with PROCESSING software it gives a cherry on a cake.
The following are some of the very basic codes that i've done with Arduino+Processing both given:

1. Processing Graph - Display a graph of the pot readings

Arduino code:

void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
}

void loop() {
  // send the value of analog input 0:
  Serial.println(analogRead(A0));
  // wait a bit for the analog-to-digital converter
  // to stabilize after the last reading:
  delay(2);
}

Processing Code:
  import processing.serial.*;

 Serial myPort;        // The serial port
 int xPos = 1;         // horizontal position of the graph

 void setup () {
 // set the window size:
 size(400, 300);       

 // List all the available serial ports
 println(Serial.list());
 // I know that the first port in the serial list on my mac
 // is always my  Arduino, so I open Serial.list()[0].
 // Open whatever port is the one you're using.
 myPort = new Serial(this, Serial.list()[0], 9600);
 // don't generate a serialEvent() unless you get a newline character:
 myPort.bufferUntil('\n');
 // set inital background:
 background(0);
 }
 void draw () {
 // everything happens in the serialEvent()
 }

 void serialEvent (Serial myPort) {
 // get the ASCII string:
 String inString = myPort.readStringUntil('\n');

 if (inString != null) {
 // trim off any whitespace:
 inString = trim(inString);
 // convert to an int and map to the screen height:
 float inByte = float(inString);
 inByte = map(inByte, 0, 1023, 0, height);

 // draw the line:
 stroke(127,34,255);
 line(xPos, height, xPos, height - inByte);

 // at the edge of the screen, go back to the beginning:
 if (xPos >= width) {
 xPos = 0;
 background(0);
 }
 else {
 // increment the horizontal position:
 xPos++;
 }
 }
 }

 

2. Controlling Onboard Led via processing GUI

Arduino code:

int onboardLED = 13;


void setup() {
 // Begin Serial communication
 Serial.begin(9600);

 //Set the onboard LED to OUTPUT
 pinMode(onboardLED, OUTPUT);
}

void loop(){
 /* Read serial port, if the number is 0, then turn off LED
 if the number is 1 or greater, turn the LED on. */
 while (Serial.available() > 0) {
 int num=Serial.read()-'0';
 if(num<1){
 digitalWrite(onboardLED, LOW); //Turn Off LED
 } else{
 digitalWrite(onboardLED, HIGH); //Turn On LED
 }
 }
}
 
Processing Code:

import processing.serial.*;

Serial comPort;
boolean ledState=false; //LED is off

void setup(){
 //Open COM Port for Communication
 comPort = new Serial(this, "COM29", 9600);
 background(255,0,0); //Start with a Red background
}

void draw(){
}


void mousePressed() {
 //Toggle led ON and OFF
 ledState=!ledState;

 //If ledState is True - then send a value=1 (ON) to Arduino
 if(ledState){
 background(0,255,0); //Change the background to green

 /*When the background is green, transmit
 a value=1 to the Arduino to turn ON LED */
 comPort.write('1');
 }else{
 background(255,0,0); //Change background to red
 comPort.write('0'); //Send "0" to turn OFF LED.
 }
}
 
3. Controlling LED Blinking via a Text file **Quite Impressive to show your teacher :)**

Arduino Code:

byte byteRead; //Used to receive data from computer.
int timeDelay; //time that the LED is On or Off
int maxtimeDelay=10000; //Maximum time delay = 10 seconds
int ledPin=13; //LED connected to pin 13 on Arduino UNO.

void setup() {
//Set pin 13 (ledPin) as an output
 pinMode(ledPin, OUTPUT);
// Turn the Serial Protocol ON
 Serial.begin(9600);
}

void loop() {
 /* check if data has been sent from the computer: */
 if (Serial.available()) {
 /* read the most recent byte */
 byteRead = Serial.read();

 switch (byteRead) {
 case 69: //This is an enquiry, send an acknowledgement
 Serial.println("A");
 break;
 case 79: //This is an "O" to turn the LED on
 digitalWrite(ledPin, HIGH);
 break;
 case 88: //This is an "X" to turn the LED off
 digitalWrite(ledPin, LOW);
 break;
 case 46: //End of line
 //Make sure time delay does not exceed maximum.
 if(timeDelay > maxtimeDelay){
 timeDelay=maxtimeDelay;
 }
 //Set the time for LED to be ON or OFF
 delay(timeDelay);
 Serial.println("S");
 timeDelay=0; //Reset timeDelay;
 break;
 default:
 //listen for numbers between 0-9
 if(byteRead>47 && byteRead<58){
 //number found, use this to construct the time delay.
 timeDelay=(timeDelay*10)+(byteRead-48);
 }
 }
 }
}
 
Processing Code:

import processing.serial.*;

Serial comPort; //The com port used between the computer and Arduino
int counter=0; // Helps to keep track of values sent.
int numItems=0; //Keep track of the number of values in text file
String comPortString; //String received From Arduino
String textFileLines[]; //Array of text file lines
String lineItems[]; //Array of line items

void setup(){
 comPort = new Serial(this,"COM29", 9600); //Setup the COM port
 comPort.bufferUntil('\n'); //Generate a SerialEvent when a newline is received
 background(255,0,0); //Start with a Red background
}

/* Draw method is not used in this sketch */
void draw(){
}

//When the mouse is pressed, write an "E" to COM port.
//The Arduino should send back an "A" in return. This will
//generate a serialEvent - see below.
void mousePressed() {
 comPort.write("E");
}

void serialEvent(Serial cPort){
 comPortString = cPort.readStringUntil('\n');
 if(comPortString != null) {
 comPortString=trim(comPortString);

 /*If the String received = A, then import the text file
 change the background to Green, and start by sending the
 first line of the text file to the Arduino */
 if(comPortString.equals("A")){
 textFileLines=loadStrings("D:/LEDdata.txt");
 background(0,255,0);
 sendLineNum(counter);
 }

 /*If the the String received = S, then increment the counter
 which will allow us to send the next line in the text file.
 If we have reached the end of the file, then reset the counter
 and change the background colour back to red. */
 if(comPortString.equals("S")){
 counter++;
 if(counter > (textFileLines.length-1)){
 background(255,0,0);
 counter=0;
 } else {
 sendLineNum(counter);
 }
 }
 }
}


/*The sendLineNum method is used to send a specific line
 from the imported text file to the Arduino. The first
 line item tells the Arduino to either switch the LED on or off.
 The second line item, tells the Arduino how long to keep the
 LED on or off. The full-stop is sent to the Arduino to indicate
 the end of the line. */

void sendLineNum(int lineNumber){
 lineItems=splitTokens(textFileLines[lineNumber],",");
 comPort.write(lineItems[0]);
 comPort.write(lineItems[1]);
 comPort.write(".");
}
 
4. Fading of Led's Via mouse movement ** Again Looks Mindblowing **

Arduino Code:

void setup() {               
  // initialize the digital pins as an output.
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
// Turn the Serial Protocol ON
  Serial.begin(9600);
}

void loop() {
  byte byteRead;

   /*  check if data has been sent from the computer: */
  if (Serial.available()) {
 
    /* read the most recent byte */
    byteRead = Serial.read();
    //You have to subtract '0' from the read Byte to convert from text to a number.
    byteRead=byteRead-'0';
   
    //Turn off all LEDS
    for(int i=2; i<11; i++){
      digitalWrite(i, HIGH);
    }
   
    if(byteRead>0){
      //Turn on the relevant LEDs
      for(int i=1; i<(byteRead+1); i++){
        digitalWrite(i+1, LOW);
      }
    }
  }
}
 
Processing Code:

import processing.serial.*;

// Global variables
int new_sX, old_sX;
int nX, nY;
Serial myPort;

// Setup the Processing Canvas
void setup(){
  size( 800, 400 );
  strokeWeight( 10 );

  //Open the serial port for communication with the Arduino
  //Make sure the COM port is correct
  myPort = new Serial(this, "COM29", 9600);
  myPort.bufferUntil('\n');
}

// Draw the Window on the computer screen
void draw(){
 
  // Fill canvas grey
  background( 100 );
   
  // Set the stroke colour to white
  stroke(255);
 
  // Draw a circle at the mouse location
  ellipse( nX, nY, 10, 10 );

  //Draw Line from the top of the page to the bottom of the page
  //in line with the mouse.
  line(nX,0,nX,height); 
}


// Get the new mouse location and send it to the arduino
void mouseMoved(){
  nX = mouseX;
  nY = mouseY;
 
  //map the mouse x coordinates to the LEDs on the Arduino.
  new_sX=(int)map(nX,0,800,0,10);

  if(new_sX==old_sX){
    //do nothing
  } else {
    //only send values to the Arduino when the new X coordinates are different.
    old_sX = new_sX;
    myPort.write(""+new_sX);
  }
}
 

Comments

Popular posts from this blog

Microsoft BizTalk Server | Interview Questions | Set 1

Hi Folks, Below is list of Some important questions that you may want to go through for a Biztalk developer role opening. Sharing the set 1 now just with questions. Will be sharing more soon. What is BizTalk Server? List down components of Biztalk server Biztalk architecture how is it? Types of schemas Document schema vs Envelope schema How to create envelope schema and its properties What is Property schema , how to create and its basic properties Purpose of using Flat file schema How to create a Flat file schema What do you mean by Canonical Schema What's is a message type Can a schema be without namespace What is min max and group min max property in a schema Explain Block default property Property promotion and types Distinguished field vs Promoted field Is it possible to promote XML record of complex content What is <Any> element in a schema Max length Promoted field and distinguished field What's Auto mapping and Default mapping  Can w

Microsoft BizTalk Server| Interview Questions| Set 2

Hi folks, We are back with set 2 of Biztalk server developer Interview Questions. Let's have a look then. State and explain stages of receive and send pipeline. Difference Between XML receive pipeline and passThru pipeline. State minimum components required in a pipeline. State maximum components used in a pipeline. Which property is required using Flat file dissambler and what happens if it is not set. What are the base types of pipeline components. What Interfaces are used for developing a general custom component pipeline. What Interfaces are used for implementing a dissambler custom pipeline component. How to execute a pipeline in an Orchestration. How to set properties of an adaptor dynamically in an Orchestration. What is message box and its purpose in Biztalk server. Types of subscription in Biztalk. Is it possible to have more than one port with same name. In which state can a send port do not subscribe to a message. Why multiple receive locations can

Microsoft C# - Basics

What is C#? We'll Take this definition from Wiki: Basic Points we should cover up: We would be learning following points in this Post: Writing and Reading from Console Variables, Data Types and Conversions Operators Conditions Passing Values to methods Handling Nullables Arrays, String, Struct and Enums Let's Code: The below Program will help you understand the basics of the above points listed. Go through each region separately and get to know the syntax of C#. We Believe, it's always better to Code and Learn than Read and Learn! If you want theoretical help related the basics, please visit here: C# Basics   Hope this helps to get you to start off with C# Coding! We would be adding more to this soon! And don't forget to visit  Joodle  to compile your codes online. Thanks! 😀