Coming soon
Easiest way to get introduced into the fascinating Arduino world
Saturday, December 23, 2017
Wednesday, December 13, 2017
Monday, August 22, 2016
Sunday, July 31, 2016
Article #20 : Using interrupts with Arduino Uno
Introduction :
An
interruption, as its name indicates, interrupts momentarily the program
executed by Arduino to perform another task. When the task finishes, the
program takes on the lead from the axact place where it was interrupted.
Interruptions
could be ideally used for listening to user’s actions, for example pressing a
button, a keypad, or the changing state of a sensor without the need of
constantly reading a pin’s state.
How does it work ?
Arduino
Uno (ATMega328) can manage 2 external interruptions on its pins INT0 and INT1
mapped to pins D2 and D3 on the microcontroller board.
Interruptions
could be triggered under 4 modes :
- LOW : The pin is in a low state
- RISING : The pin passes from low to high state
- FALLING : The pin passes from high to low state
- CHANGE : The pin has changed state
Creating an interrupt :
To
“listen” for an interruption on Arduino, you only have to attach an interrupt
on either pin D2 or D3 using the method attachInterrupt. This method takes as
an argument the interruption’s pin number 0 or 1, the name of the function
called by the interruption, and finally the interruption mode as the following
example shows :
attachInterrupt(0, myInterrupt(), RISING);
Even though Arduino’s pin is D2, we indicate 0 for the interruption’s pin (0 for INT0 / D2, 1 for INT1 / D3).
The
function attached to the interrupt have to take a small amout of time to
execute and it is also forbidden to use time based instructions in the interior
of the function as it blocks the microcontroller.
Application :
In
the following application, we attach an interruption to a push button. The
program writes the interruption number to the serial monitor every time the
button is pushed.
Code :
int interruption = 0; //interrupt 0 is on digital pin 2
volatile int numInterrupt = 0; //variables used inside interrupt functions declared as volatile
void setup() {
Serial.begin(9600);
attachInterrupt(interruption, functionInterruption, FALLING);
}
Void loop() {
//simulating a task being executed repeatedly
While(true) {
Serial.print(“.”);
delay(250);
}
}
Void functionInterruption() {
Serial.print(“Interrupt numero ”);
Serial.println(numInterrupt);
numInterrupt++;
}
volatile int numInterrupt = 0; //variables used inside interrupt functions declared as volatile
void setup() {
Serial.begin(9600);
attachInterrupt(interruption, functionInterruption, FALLING);
}
Void loop() {
//simulating a task being executed repeatedly
While(true) {
Serial.print(“.”);
delay(250);
}
}
Void functionInterruption() {
Serial.print(“Interrupt numero ”);
Serial.println(numInterrupt);
numInterrupt++;
}
Monday, May 30, 2016
Article #19 : Arduino Timers
Introduction :
When we start learning about
microcontrollers, and especially Arduino in our case, the first program that
you have written was most probably the famous blinking LED program. In that
program you certainly used to call the delay() function to set the time
interval between blinks. The only thing is that delay() sets Arduino on pause
mode, so how we can blink the LED while still using Arduino for doing other
tasks at the same time like sending serial data for example ? That’s why we are
going to learn about the Timer library to facilitate this task.
The library :
The library Timer has a simple functioning
principle. After adding the library to our project, we can create a variable of
type Timer, then we update it in the loop function and finally give some
instructions to our timer to perform the tasks like blinking an LED for 100ms
for example.
Basic Commands :
As said previously, we have to import the
Timer library to our program.
#include “Timer”
Then, we have to create our variable of type
timer :
Timer timer;
Also, we have to update our newly created
timer in the loop function as follows :
void loop(){
Timer.update();
}
Now we don’t need to use the delay() function
anymore. We have our timer to count time in the program.
The most used functions in the Timer library
are the following :
int every(long period, callback, int
repeatCount)
Run the 'callback' every 'period' milliseconds for a total of 'repeatCount' times.
int after(long duration, callback)
Run the 'callback' once after 'period' milliseconds.
int oscillate(int pin, long period, int
startingValue, int repeatCount)
Toggle the state of the digital output 'pin' every 'period' milliseconds 'repeatCount' times.
The pin's starting value is specified in 'startingValue', which should be HIGH or LOW.
int pulse(int pin, long period, int
startingValue)
Toggle the state of the digital output 'pin' just once after 'period' milliseconds.
The pin's starting value is specified in 'startingValue', which should be HIGH or LOW.
int update()
Must be called from 'loop'. This will service all the events associated with the timer.
Examples :
The following example is a program that passes
a pin from a state to another for a set period of time only once. To do it we
need to implement the ‘pulse’ method.
#include "Timer.h"
Timer t;
int pin = 13;
void setup()
{
pinMode(pin, OUTPUT);
t.pulse(pin, 10 * 60 * 1000, HIGH); // 10 minutes
}
void loop()
{
t.update();
}
The update method in the loop function takes
only few milliseconds to run, and the program can continue executing other
instructions while the timer counts the time in the setup function.
The second example is program that toggles the pin 13 from a state to
another every 100 ms and reads the analog pin 0 from the serial monitor every 2
seconds.
#include "Timer.h"
Timer t;
int pin = 13;
void setup()
{
Serial.begin(9600);
pinMode(pin, OUTPUT);
t.oscillate(pin,
100, LOW); // la fonction qui fait
clignoter la led toutes les 100 ms
t.every(1000,
envoi); // et celle-ci qui appelle la fonction 'envoi' toutes les secondes
}
void loop()
{
t.update();
}
void envoi() // fonction qui envoie par liaison série l'entrée du pin 0
{
Serial.println(analogRead(0));
}
References :
Sunday, February 28, 2016
Article #18 : Using Arduino To Program an SD Card
Introduction :
Arduino offers the possibility to read from and
write to SD cards using the Sd library. However, because the Arduino board
doesn’t have a built-in SD card input you have to connect a shield that have an
SD card reader into your board. After that, you will be able to use it for a
multitude of applications such as reading sensor data values and storing them
into the SD card.
In this tutorial, we will see how to connect the
SD card shield to the microcontroller, write some basic commands of the SD
library and finally write a program creating a file, writing data into it and
reading it after.
Project parts :
- Arduino Uno.
- SD card shield.
SD card shield pinout :
First,
you need to connect the shield to the Arduino board. On every pin of the
shield, there is a label. S, you have to connect avery pin of the shield (+5v,
GND, SCK, MOSI, MISO) to its corresponding pin on the Arduino card, according
to the documentation of Arduino Uno :
- MOSI è Pin 11
- MISO è Pin 12
- SCK è Pin 13
- SS è Pin 10
Then, connect the Cs pin on any of the remaining
digital I/O pins of your Arduino, in this tutorial, I have connected it to pin
5.
You also have to format the SD card to FAT16 or
FAT32 from your computer.
Basic Codes :
As always, you have to include the library
#include <SD.h>
To create a file you have to create a variable
of type File.
File file;
Then, in the setup function, you have to write a
code to start the SD card. The following sequence of code enables to determine
if there is a connection error via the serial bus.
If(!SD.begin(5)) {
Serial.println(“Starting
SD card error!”);
}
Serial.println(“SD card ready.”);
Using the File variable that we have created, we
can open the file in read or write mode.
file = SD.open(“myFile.txt”,
FILE_WRITE);//file on writing mode
file = SD.open(“myFile.txt”,
FILE_READ);//file on read mode
We usually have to check if the file has been
opened, for that we use the following condition :
if (file) {
Serial.print(“File
opened successfully”);
}else {
Serial.println(“error
while opening the file”);
}
We can then write to or read from the created file
depending on the defined mode.
file.println(“Welcome to
simple-arduino tutorial !”);
file.close();//always remember to
close the file after
In read mode, we create a loop that reads the
file until its end.
While(file.available()) {
char
c = file.read();
Serial.write(c);//send
the character to serial monitor
}
Total Code:
The following code uses all functions that we
have seen above. It writes to a file, then sends the data over serial bus to
the screen.
#include <SD.write>
File file;
void setup() {
Serial.begin(9600);
Serial.print(“Initializing
SD card”);
pinMode(10,
OUTPUT);
if (!SD.begin(5)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization
done.");
file = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile =
SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in
it:
while
(myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file
didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
// all programming
happens in setup mode
}
Sunday, February 21, 2016
ARTICLE #17 : Using Arduino with Processing
Introduction :
For writing programs and loading them to the
Arduino board, we used the Arduino IDE program. Now we will see another
programming language and IDE to communicate with Arduino using serial port
which is called Processing.
Processing is simply an open source computer
programming language and IDE built for the electronic arts using a visual
context. The processing language is based on the Java language. So the programs
that we are going to write are similar to Java code.
In this tutorial we will see :
- How to send data from Android and receive it in Processing.
- How to send data from processing and receive it in Android.
To perform this tutorial you will have to have:
- Arduino IDE download it from here.
- Processing IDE download it from here.
Hello world :
We will write a “hello world” to the Arduino
board using Arduino IDE and then manage to receive it in the Processing IDE.
Code Arduino :
Void setup() {
Serial.begin(9600);
//initializing serial communications
}
Void loop() {
Serial.println(“hello
world”); //wait 500 ms and write it again
}
Code Processing :
To receive data in Processing we have to create
an object from serial class that will get the communication port that Arduino
is hooked to.
import processing.serial.*;
Serial myPort;
String val; //Data received from
the serial port
void setup() {
String
portName = Serial.list()[0];//depending on the port number of the device
myPort
= new Serial(this, portName, 9600);
}
void draw() {
if(myPort.available()
> 0) { //If data is available
val
= myPort.readStringUntil(‘\n’);//read the string and put it in val
If(val
!= null)
Println(val);
}
}
Now that we learned how to receive data sent
from Arduino to Processing. We will now learn how to do it the other way, from
processing to Arduino.
What does the following code ? whenever we press
the mouse, processing sends ‘1’ to Arduino. Whenever the Arduino IDE catches
‘1’ it turns the LED pin 13 on.
Code Processing :
import processing.serial.*;
Serial myPort; //Creating a Serial
object
void setup() {
size(200,
200);//The space for the mouse to click
String
portName = Serial.list()[0];
myPort
= new Serial(this, portName, 9600);
}
void draw() {
if(mousePresses
== true) {
myPort.write(‘1’);//send 1 over the
serial port
println(“1”);//printing it to
make sure we are sending 1
}
else {
myPort.write(‘0’);
//send 0 instead
}
}
Code Arduino :
char cal;
int ledPin = 13;
void setup() {
pinMode(ledPin,
OUTPUT);
Serial.begin(9600);
}
void loop() {
if(Serial.available())
{
val
= Serial.read();
}
if (val == ‘1’) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(10);
}
When we load the code above to Arduino and run
Processing sketch, you should be able to turn the LED on by simply clicking
into the created processing canvas.
Subscribe to:
Posts (Atom)