Objective:
In this week's assignment we had to Write an application that interfaces a user with an input and/or output device that I made



I am going to be using my Final Project board which is the one that i made in the Output devices week, with a simple servo motor as an output, and my application will be moving the servo motor on 3 different positions using Processing. Processing is a sketchbook software, allows us to code using visual context.

I went through these tutorials Youtube and Sparkfun to learn how to use it, we connect the processing interface with the Arduino IDE to be able to control the inputs and outputs with a graphic User Interface.
To start using Processing with At328p, I have to install a library that enables processing to work with microcontrollers. So I installed processing, and then installed controlP5 library by going to sketches>>import library>>add library.
.

After that it's time to start with the code, first I included the Controlp5 Library and started making my Window.



Then I added the buttons which will move the servo motor.



After that I added the serial Functions "port.write()" which will send serial signal to my board, and upon recieving the signals my board will execute different commands.





Processing Code


import controlP5.*;
 import processing.serial.*;
 
  Serial port;
ControlP5 cp5;
void setup(){

size(350, 400);

 printArray(Serial.list());
   port= new Serial(this,"com3",19200);
 }
void POS1(){
port.write('a');
}

void POS2(){
port.write('b');
}

void POS3(){
port.write('c');
}


void draw(){
background(100,0,150);

text("Servo Motor Position Control",100,30);
 cp5 = new ControlP5(this);
 cp5.addButton("POS1").setPosition(80,100).setSize(100,90);
 cp5.addButton("POS2").setPosition(80,250).setSize(100,90);
 cp5.addButton("POS3").setPosition(200,160).setSize(100,90);
}

Arduino Code

#include <servo.h>

// Include the Servo library 

// Declare the Servo pin 
int servoPin = 3; 
// Create a servo object 
Servo Servo1; 
void setup() { 
   // We need to attach the servo to the used pin number 
   Servo1.attach(servoPin); 
   Serial.begin(19200);
}
void loop(){ 
 if(Serial.available()){
     char val = Serial.read();

if(val =='a'){    
// Make servo go to 0 degrees 
Servo1.write(0); 
      
      }

if(val =='b'){    
// Make servo go to 90 degrees 
Servo1.write(90); 
      
      }      
     
if(val =='c'){    
// Make servo go to 180 degrees 
Servo1.write(180); 
      
      }
}
}

Video






Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.