Commit 2541f559 by Michael Oduor

week 16 updates

parent 65160daf
Pipeline #94544 passed with stages
in 39 seconds
......@@ -22,32 +22,80 @@ Processing is a flexible software sketchbook and a language for learning how to
I couldn't attend the local lecture, so missed the introduction given by [Ivan](http://archive.fabacademy.org/archives/2017/fablaboulu/students/77/about.html). To get familiar with Processing, I went through the [Overview](https://processing.org/tutorials/overview/) and few [Tutorials](https://processing.org/tutorials/) from Processing's website. I also went through the examples (**File > Examples...**) available from Processing's IDE which cover different topics such as Arrays, Objects, Shapes etc.
After going through the above examples, I searched for examples on how to connect Arduino to Processing and send data over the serial port. [This one](https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all) from SparkFun outlined both talking and listening to the flow of data in both directions involving an agreement of when to send and receive ("handshake"). It was a good and easy to follow example of communication over serial. The process for serial communication, at least in Arduino, was outlined in the [Networking and Communication](http://fabacademy.org/2019/labs/oulu/students/michael-oduor/assignments/week14/) week where we connected and sent data to different boards.
In Processing, you can write both **static sketch** (programs written as a series of statements) and an **interactive sketch** (programs written as a series of frames). In static sketches, a series of functions are used to perform tasks or create images without any interaction or animation. For example,
```
size(400, 400); // Defines the dimension of the display window width and height in units of pixels
background(192, 64, 0); Sets the colour used for the background of the Processing window.
stroke(255, 128, 0, 128); // Sets the colour used to draw lines and borders around shapes.
line(150, 25, 270, 350); // Draws a line (a direct path between two points) to the screen.
```
Whereas interactive sketches are created by adding built-in functions that are called automatically as shown below.
```
void setup() { // runs once and used for any initialisation
size(400, 400); // should always be the first line inside setup()
ellipse(width/2, height/2, 50, 50); // Always the middle, no matter how the size() line changes
stroke(255);
background(192, 64, 0); // used only once when inside setup
}
void draw() { // used to handle animation
//background(192, 64, 0); // when moved here, a single line that follows the mouse is drawn clearing the displying window.
line(150, 25, mouseX, mouseY); // starting position of the line and detecting the movement of the mouse
}
void mousePressed() { // called whenever the mouse is pressed
}
```
![](../images/week16/serial02.png)
After going through the above examples, I searched for examples on how to connect Arduino to Processing and send data over the serial port. [This one](https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all) from SparkFun outlined both talking and listening to the flow of data in both directions involving an agreement of when to send and receive (**"handshake"**). It was a good and easy to follow example of communication over serial. The process for serial communication, at least in Arduino, was also outlined in the [Networking and Communication](http://fabacademy.org/2019/labs/oulu/students/michael-oduor/assignments/week14/) week where we connected and sent data to different network nodes.
When you run the sketch, "Hello world!" is displayed in the console and clicking in the Processing window, turns on and off the LED that is connected to Pin 13 in the Arduino. The files for the above example are included below.
In Processing, the process is briefly presented below:
![](../images/week16/serial.mp4)
[Blinking LED - Video](../images/week09/led.mov).
I had problems communicating via the serial port with all the ATtiny44 boards from previous assignments, so decided to connect the RGB LED board from Output Devices to an Arduino Uno. I did not get a response whenever I programmed the boards via the Arduino IDE to communicate with Processing and there were no errors displayed.
```
import processing.serial.*; //import the Serial library
![](../images/week12/board.png)
![](../images/week16/rgb.jpg)
Serial myPort; //the Serial port object
String val;
void setup() {
size(200, 200);
printArray(Serial.list()); // List all the available serial ports:
myPort = new Serial(this, Serial.list()[1], 9600); // initialize your serial port and set the baud rate
}
```
![](../images/week16/serial02.png)
<sup>Connecting Arduino to processing</sup>
When you run the sketch, "Hello world!" is displayed in the console and clicking in the Processing window, turns on and off the LED that is connected to Pin 13 in the Arduino. The files for the above example are included below.
![](../images/week16/serial.mp4)
[Blinking LED - Video](../images/week09/led.mov)
I had problems communicating via the serial port with all the ATtiny44 boards from previous assignments. I did not get a response whenever I programmed the boards via the Arduino IDE to communicate with Processing and there were no errors displayed. Therefore, I decided to connect the RGB LED board from [Output devices](http://fabacademy.org/2019/labs/oulu/students/michael-oduor/assignments/week12/) to an Arduino Uno. I programmed the board following [these instructions](https://www.himix.lt/arduino/arduino-and-rgb-control-processing/) for changing the colour and intensity of an RGB LED according to the colour moused over in the Processing window.
![](../images/week16/serial-rgb.jpg)
<sup>Connecting the RGB LED board to Arduino </sup>
![](../images/week16/rgb.png)
<sup>Programming the RGB LED board and testing</sup>
The colour and intensity of the LED change as you move the mouse across the Processing window as shown in the following video and the position (x, y) of the mouse is also printed in the console.
![](../images/week16/rgb.mp4)
[RGB - Video](../images/week09/rgb.mp4)
## Reflection
This past two weeks have been the slowest so far as I have been unwell. To make matters worse, in this week I could not get Processing to communicate with any of my boards from previous weeks (I am often slow, but in weeks like this one when it is beyond me, then it is a disaster!). Even replicating other examples from the Fab Academy, such as [this one](http://fab.academany.org/2018/labs/fablabegypt/students/khaled-youssef/exercise12.html) for controlling the LED in the [hello echo board](http://fabacademy.org/2019/labs/oulu/students/michael-oduor/assignments/week07/) board did not work. I could program the board to blink the LED, but couldn't control it via Processing. As the boards, at least two of them, worked in the Networking and Communication haven't yet figure out why the connection with Processing doesn't work.
Processing has good documentation and plenty of online examples, but could not explore it as much as I would have liked to so there is still plenty to learn. Going through a few tutorials helped me to understand the basics of, for example, the coordinate system, colours and serial communication (even though it hasn't yet worked with any of my boards). When I get it to work will also look at controlling at least two devices (servo motors, LEDs and IR sensors) and later also consider other applications for developing interfaces.
## Files
- Arduino - [Arduino RGB Control](../images/week16/Arduino_RGB_Control.ino) / [Connecting Arduino to Processing](../images/week16/serial002.ino)
......
// Arduino code is available to download - link below the video
/*
Arduino and RGB Control using Processing
https://www.himix.lt/arduino/arduino-and-rgb-control-processing/
based on: https://gist.github.com/atduskgreg/1349176
*/
// based on: https://gist.github.com/atduskgreg/1349176
/*
- Red Led to digital pin 3
- Red Led to digital pin 6
- Green Led to digital pin 5
- Blue Led: not connected
- Blue Led: to digital pin 3
*/
//#include <SoftwareSerial.h>
//#define rxPin 0
//#define txPin 1
//SoftwareSerial mySerial(rxPin, txPin);
int currentValue = 0;
int values[] = {0,0};
//boolean ledState = LOW; //to toggle our LED
// pins for the LEDs:
const int redPin = 3;
const int redPin = 6;
const int greenPin = 5;
const int bluePin = 6;
const int bluePin = 3;
void setup() {
// initialize serial:
......@@ -32,17 +45,10 @@ void loop() {
values[currentValue] = incomingValue;
currentValue++;
if(currentValue > 1){
analogWrite(greenPin, values[0]);
analogWrite(redPin, values[1]);
analogWrite(redPin, values[0]);
analogWrite(greenPin, values[1]);
analogWrite(bluePin, values[2]);
currentValue = 0;
}
}
}
/*
Processing code is available to download - link below the video
Arduino and RGB Control using Processing
https://www.himix.lt/arduino/arduino-and-rgb-control-processing/
based on: https://gist.github.com/atduskgreg/1349176
Modified by Michael 0: 15.05.19
*/
import processing.serial.*;
Serial arduinoPort;
Serial myPort;
int value1, value2;
void setup() {
size(255,255); // RGB integers are between 0-255, so our window is 255 in X and Y
frameRate(25);
frameRate(30); // Specifies the number of frames to be displayed every second
println(Serial.list());
arduinoPort = new Serial(this, Serial.list()[1], 9600); // arduino serial port
//myPort = new Serial(this, Serial.list()[1], 9600); // location of serial port
myPort = new Serial(this, "/dev/tty.usbmodem14101", 9600); // location of serial port
// Draw the background colour wash
for (int x = 0; x < width; x++) { // for all X pixels
for (int y = 0; y < width; y++) { // for all Y pixels
stroke(x,y,0); // colour of pixel = pixel position
stroke(x,y,0); // colour of pixel = pixel position ... switched between 0 and 255
point(x,y); // draw the pixel
}
}
......@@ -31,9 +33,11 @@ void draw() {
void mouseMoved() {
print(mouseX); print(","); println(mouseY);
byte out[] = new byte[2];
byte out[] = new byte[4];
out[0] = byte(mouseY);
out[1] = byte(mouseX);
arduinoPort.write(out);
out[2] = byte(mouseY);
//out[3] = byte(mouseX);
myPort.write(out);
//println(out);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment