Arduino Processing
Arduino Processing
Problem Statement-
To study the theoretical and practical implementation of Processing and Arduino
programming by designing ways, 1) to interpret , analyize and visualize sensors data
dynamically, 2) to manually upload the microcontroller programs to the arduino board
without using the API, 3) to formulate a way to include parallelization in Arduino
programming for better speed and performance, 4) to generate code for a line following
robot using IR sensors.
Introduction-
Processing relates software concepts to principles of visual form, motion, and
interaction. It integrates a programming language, development environment, and
teaching methodology into a unified system. The processing language is a text
programming language specifically design to generate and modify images.
Sketching is necessary for the development of ideas. It allows us to analyse how the data
varies and interpret the changes needed to incorporate for obtaining the desired result.
Arduino is a single-board microcontroller, intended to make building interactive objects
or environments more accessible. Arduino is a tool for making computers that can sense
and control more of the physical world than the desktop computer. It's an open-source
physical computing platform based on a simple microcontroller board, and a
development environment for writing software for the board.
Arduino can be used to develop interactive objects, taking inputs from a variety of
switches or sensors, and controlling a variety of lights, motors, and other physical
outputs. Arduino projects can be stand-alone, or they can communicate with software
running on the computer (e.g. Flash, Processing, MaxMSP). The Arduino programming
language is an implementation of Wiring, a similar physical computing platform, which
is based on the Processing multimedia programming environment.
Most basic analog sensors utilize resistance. Changes in a physical phenomenon modify
the resistance of the sensor, therefore varying the voltage output through the sensor.
An analog-to-digital converter can continuously measure this changing voltage and
convert it to a number that can be used by software. Sensors that produce digital signals
send data as binary values to an attached device or computer. These sensors use a
voltage (typically between 3.5 and 5 volts) as ON (binary digit 1 or TRUE) and no
voltage as OFF (binary digit 0 or FALSE). More complex sensors include their own
microcontrollers to convert the data to digital signals and to use established
communication protocols for transmitting these signals to another computer.
Analog voltage signals from sensors can’t be directly interpreted by a computer, so they
must be converted it to a digital value. Some microcontrollers provide analog-to-digital
converters (ADC or A/D) that measure variations in voltage at an input pin and convert
it to a digital value.
Data is sent and received between microcontrollers and computers according to
established data protocols such as RS-232 serial, USB, MIDI, TPC/IP, Bluetooth, and
other proprietary formats like I2C or SPI. Most electronics prototyping kits and
microcontrollers include an RS-232 serial port, and this is therefore a convenient way to
communicate.
The following examples connect sensors and actuators to a Arduino board and
communicate the data between the I/O board and a Processing application. When the
Arduino board is plugged in to a computer’s USB port, it appears on the computer as a
serial port, making it possible to send/receive data on it.
Example 1: Switch
This example sends the status of a switch (ON or OFF) connected to the Arduino board
to a Processing application running on a computer. Software runs on the board to read
the status of a switch connected on digital pin 4. The value 1 is sent to the serial port
continuously while the switch is pressed and 0 is sent continuously when the switch is
not pressed.
Example 2: Light sensor
This example brings data from a light sensor (photoresistor) connected to the Arduino
board’s analog input pin 0 into a Processing application running on a computer.
Software runs on the board to send the value received from the light sensor to the serial
port. Because the light sensor is plugged into an analog input pin, the analog voltage
coming into the board is converted into a digital number before it is sent over the serial
port.
Example 3: Turning a light on and off
This example sends data from a Processing program running on a computer to a
Arduino board to turn a light ON or OFF. The program continually writes an H to the
serial port if the cursor is inside the rectangle and writes a L if it’s not. Software running
on the board receives the data and checks for the value.
Example 4: Controlling a servomotor
This example controls the position of a servomotor through an interface within a
Processing program. When the mouse is dragged through the interface, it writes the
position data to the serial port. Software running on a Arduino board receives data from
the serial port and sets the position of a servomotor connected to the digital I/O pin
number 4.
Arduino Programming-
First have an introduction on how the Arduino board is used for microcontroller
programming.
An Arduino board is a microcontroller situated on a PCB with other components to
make it easier to program, attach/detach components, and turn on and off. This board
typically have components to regulate power to protect the microcontroller and a USB
or RS-232 serial port to make it easy to attach cables. The small pins on the micro-
controller are wired to larger pins called headers that make it easy to insert and
remove sensors and motors. Small wires embedded within the PCB connect pins to a
corresponding header. Small reset switches make it easy to restart the power without
having to physically detach the power supply or battery.
Arduino boards use the Wiring language to program their microcontrollers and use a
development environment built from the Processing environment. In comparison to the
Processing language, the Wiring language provides a similar level of control and ease of
use within its domain. Wiring has some functions specific to programming micro-
controllers and omits the graphics programming functions within Processing. Like
Processing programs, Wiring programs are translated into another language before
they are run. When a program written with the Wiring language is compiled, it’s first
translated into the C/C++ language and then compiled using a C/C++ compiler.
Arduino is a powerful system (the board has more memory, I/O pins, serial ports, and
other internal capabilities) and is therefore expensive. The microprocessor on the
Arduino board can be removed and replaced if it is damaged.
Arduino Software IDE
Uploading
Before uploading our sketch, we need to select the correct items from the Tools >
Board and Tools > Serial Portmenus. On Windows, it's probably COM1 or COM2 (for a
serial board) or COM4, COM5, COM7, or higher (for a USB board) - to find out, we look
for USB serial device in the ports section of the Windows Device Manager.
Once we've selected the correct serial port and board, press the upload button in the
toolbar or select the Upload item from the File menu. Current Arduino boards will reset
automatically and begin the upload. With older boards (pre-Diecimila) that lack auto-
reset, we'll need to press the reset button on the board just before starting the upload.
On most boards, we'll see the RX and TX LEDs blink as the sketch is uploaded. The
Arduino environment will display a message when the upload is complete, or show an
error.
When we upload a sketch, we're using the Arduino bootloader, a small program that has
been loaded on to the microcontroller on our board. It allows us to upload code without
using any additional hardware. The bootloader is active for a few seconds when the
board resets, then it starts whichever sketch was most recently uploaded to the
microcontroller. The bootloader will blink the on-board (pin 13) LED when it starts (i.e.
when the board resets).
Sample Arduino program being uploaded using Arduino API-
Manually Uploading the Arduino Program without the use of API-
Multi-file sketches-
A sketch can contain multiple files. To manage them, we need to click on the right-facing
arrow just above the scroll bar near the top of the environment. Tabs have one of four
extensions: no extension, .c, .cpp, or .h (if we provide any other extension, the period
will be converted to an underscore). When the sketch is compiled, all tabs with no
extension are concatenated together to form the "main sketch file". Tabs with .c or .cpp
extensions are compiled separately. To use tabs with a .h extension, we need to #include
it (using "double quotes" not <angle brackets>).
Build process-
Sketches are compiled by avr-gcc.
The include path includes the sketch's directory, the target directory
(<ARDUINO>/hardware/core/<CORE>/) and the avr include directory
(<ARDUINO>/hardware/tools/avr/avr/include/), as well as any library directories (in
<ARDUINO>/hardware/libraries/) which contain a header file which is included by the
main sketch file.
When we verify a sketch, it is built in a temporary directory in the system temp
directory. When we upload it, it is built in the applet/ subdirectory of the sketch's
directory .
The .c and .cpp files of the target are compiled and output with .o extensions to this
directory, as is the main sketch file and any other .c or .cpp files in the sketch and any .c
or .cpp files in any libraries which are #included in the sketch.
These .o files are then linked together into a static library and the main sketch file is
linked against this library. Only the parts of the library needed for our sketch are
included in the final .hex file, reducing the size of most sketches.
The .hex file is the final output of the compilation which is then uploaded to the board.
During a "Verify" the .hex file is written to \Documents and Settings\<USER>\Local
Settings\Temp (on Windows). During upload, it's written to the applet sub-directory of
the sketch directory (which we can open with the "Show Sketch Folder" item in the
Sketch menu).
Upload process-
Sketches are uploaded by avrdude.
The upload process is also controlled by variables in the boards and main preferences
files. The board files include-
<BOARD>.upload.protocol the protocol that avrdude should use to talk to the board
(typically "stk500").
<BOARD>.upload.speed: the speed (baud rate), avrdude should use when uploading
sketches (typically "19200").
<BOARD>.upload.maximum_size, the maximum size for a sketch on the board
(dependent on the chip and the size of the bootloader).
Outcome-
Yes we can upload multiple programs at the same time to one Arduino board by
including them as multiple sketches but uploading a sketch to more than one arduino at
the same time is not possible because arduino is writing to its flash memory. The write
time for this is not the same on all arduinos, nor the same through the arduino's life
time. As the arduino has to signal back to the PC to get the next bytes to load we would
have to multiplex all the arduino's serial responses to see when they were all ready
which is not impossible but definitely not worth doing.
Line Following Robot
To make a robot follow a line using IR sensors first we need to understand that a dark
object reflects less light than a bright object, or a dark object absorbs more light than a
bright object, this is the only fundamental logic behind a line sensing robot. So, in a line
sensing robot, we make a sensor that detects the difference between bright object and a
dark object i.e. it distinguishes a black line from a white surface.
In our experiment we are using three IR sensors. So there are three possible conditions-
The center IR sensor seeing the line
The left IR sensor seeing the line
The right IR sensor seeing the line
Now we need to use these conditions to design the way a robot always steer towards
the line. The information obtained from sensors is used to decide whether to continue
straight or to change the wheel speeds to turn or to stop.
First I am going to generate line following code using SpeedobotiX GUI, which is a
product of Omnipresent Robot Technologies Pvt. Ltd. It is a processing based graphical
user interface which is developed for school kids to understand robotics in an easy way.
The main aim of the company in designing this product is to provide school kids with
such a platform where they can run a robot by using normal logic without even getting
into programming.
Speedobotix GUI-
Now we first generate the code for a simple program using SpeedobotiX GUI to test the
functionality of all elements, In this program the bot will move forward with a delay of
5ms if there are no obstacles and turns backward when encounters any obstacles in
front of it with a delay of 10ms.
The code generated by uploading the above flowchart is-
First lets understand how to describe the motion of bot using the three sensors-
Let the ON state in green colour describe detection of white surface.
Let the OFF state in red colour describe the detection of black surface.
For three sensors we have eight different combinations (23 =8 ) as every sensor has two
possibility so in general for n- sensors we will have 2n possible combinations.
The code generated for the line follower by uploading the above flowchart is –
The only problem with the above generated code is that it does not involve Calibration.
So if left or right sensor detects a change in sensor value they will steer towards their
respective ends, regardless whether the surface is black or not. So to distinguish
between lighter and darker surface and also ensure at what minimum reading change
the sensors should behave differently we need to write the Calibrate code. For this we
define a term threshold. Threshold is the difference in values required between the
center sensor and the left or right sensors before the robot decides to make a turn. We
can assume the difference to be any value, lets say 5. This means that the left and right
sensors would need to detect a value greater than the value read from the center sensor
plus the threshold value before action is taken. So, if the center sensor is reading a value
of 600 and the left sensor is reading 603, then the robot will keep going straight.
However, a left sensor value of 612 (which is higher than the center value plus
threshold) means that the left sensor is detecting the black line, indicating that the robot
is too far over to the left. So the motors would adjust to make the robot turn to the right
to compensate. The threshold value will vary depending on the contrast between the
floor (or whatever surface used) and the black line. This may need to be adjusted to
ensure the robot only turns when it has detected enough of a difference between floor
and line to ascertain it had moved too far left or right. So I wrote the following code for a
line follower robot using three IR sensors which includes the calibration code-
#define lights 9
int IR1, IR2, IR3;
int leftOffset = 0, rightOffset = 0, centre = 0;
int speed1 = 3, speed2 = 11, direction1 = 12, direction2 = 13;
int startSpeed = 150, rotate = 30;
int threshhold = 5;
int left = startSpeed, right = startSpeed;
void calibrate() {
for (int x=0; x<10; x++) {
digitalWrite(lights, HIGH);
delay(100);
IR1 = analogRead(0);
IR2 = analogRead(1);
IR3 = analogRead(2);
leftOffset = leftOffset + IR1;
centre = centre + IR2;
rightOffset = rightOffset + IR3;
delay(100);
digitalWrite(lights, LOW);
delay(100);
}
leftOffset = leftOffset / 10;
rightOffset = rightOffset / 10;
centre = centre /10;
leftOffset = centre - leftOffset;
rightOffset = centre - rightOffset;
}
void setup()
{
pinMode(lights, OUTPUT);
pinMode(speed1, OUTPUT);
pinMode(speed2, OUTPUT);
pinMode(direction1, OUTPUT);
pinMode(direction2, OUTPUT);
calibrate();
delay(3000);
digitalWrite(lights, HIGH);
delay(100);
digitalWrite(direction1, HIGH);
digitalWrite(direction2, HIGH);
analogWrite(speed1,left);
analogWrite(speed2,right);
}
void loop() {
left = startSpeed;
right = startSpeed;
IR1 = analogRead(0) + leftOffset;
IR2 = analogRead(1);
IR3 = analogRead(2) + rightOffset;