Posts Tagged ‘electronic’
Keypad & LCD Display
While I was still trying to figure out what to do these 10 keypads, I received an LCD Display I ordered on eBay. It’s friday, I have no better idea than plug them both on an Arduino and code something.
I started from the circuit of the Keypad article and moved the wires connected to pins 7 & 8 to analog pins 0 & 1. There is no good reason to that shift except that it makes it easier to have the LCD wires all connected to the same side of the Arduino.
I will not go in details with the wiring since Limor Fried, founder of Adafruit Industries published an excellent demo on how to connect the display to an Arduino. This will require an additional 6 pins on your Arduino. I used the same pins as in the Lady Ada demo, 7, 8, 9, 10, 11 and 12.
Now let’s jump into the code. This is a very simple demo and you will not find anything mind blowing. The hard part has been coded for you in the LiquidCrystal library, included with the Arduino IDE.
Thanks for reading …
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#define REDLITE 3
#define GREENLITE 5
#define BLUELITE 6
#define TITLE "KeyPad & LCD __"
#define READY "Ready ... "
#define EMPTY " "
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// you can change the overall brightness by range 0 -> 255
int brightness = 255;
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
//connect to the row pinouts of the keypad
byte rowPins[ROWS] = {2, 3, 4, 5};
//connect to the column pinouts of the keypad
byte colPins[COLS] = {6, 14, 15};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(TITLE);
lcd.setCursor(0,1);
lcd.print(READY);
brightness = 100;
Serial.begin(9600);
}
int index = 1;
char digits[2] = {' ',' '};
void loop(){
char key = keypad.getKey();
if (key != NO_KEY)
{
index = !index;
if(key == '*')
{
index = 1;
digits[0] = ' ';
digits[1] = ' ';
lcd.setCursor(0,0);
lcd.print(TITLE);
lcd.setCursor(0,1);
lcd.print(READY);
}
else if(key == '#')
{
// Animation
for(int i=0; i<15;i++)
{
lcd.setCursor(0,1);
lcd.print(EMPTY);
lcd.setCursor(i,1);
lcd.print(digits);
delay(100);
}
lcd.setCursor(0,1);
lcd.print(EMPTY);
lcd.setCursor(14,0);
lcd.print(digits);
index = 1;
digits[0] = ' ';
digits[1] = ' ';
lcd.setCursor(0,1);
lcd.print(READY);
}
else
{
digits[index] = key;
lcd.setCursor(0,1);
lcd.print(digits);
lcd.print(EMPTY);
}
Serial.println(key);
}
}
Playing With The Beaglebone
I just received my Beaglebone and started experimenting with it. The Beaglebone is a low cost, single board Linux computer with tons of IOs to interface with the physical world.
My objective is to make a Python Robot using the Beaglebone as the brain and my Makerbot to build the frame. I found an excellent series of articles on how to get started with the Beaglebone on the Dan Watts’s blog.
The most interesting feature of this board is the way used to read and write to the IOs using the file system. By using the file system, the IOs are accessible trough any language you may want to install on your Beaglebone Linux distro.
I’ll post some practical examples in the following days.
A Simple Arduino Task Scheduler (1)
My motivation to write a task scheduler framework is to help me faster develop new Arduino projects. In most projects, we repeat the same tasks over and over before getting to play with the new stuff.
You can jump into the code right now by downloading it from my github account at:
https://github.com/pchretien/scheduler.
ITask Interface
Before using the scheduler functionalities, we have to explore the ITask interface. ITask is an interface that you must implement for all tasks to be scheduled and executed by the Scheduler. This interface has only two methods, “setup” and “run”.
The ITask::setup() method of your task object will be called in the global setup() function of your Arduino application. You place in this function all the initialization code your task object needs to execute correctly.
The ITask::run(Scheduler*) method is the method called by the scheduler when your task object is scheduled to run. It receives a reference to the Scheduler object so that your task object can re-schedule itself or another task.
class ITask
{
public:
virtual void setup() = 0;
virtual void run(Scheduler* scheduler) = 0;
};
Scheduler Singleton
The scheduler has two methods to schedule a task to run. To schedule a task to run immediately you use the queue(ITask*) method. This will add a task to the queue of tasks ready to run.
Then, to schedule a task to run in the future, you should use the schedule(ITask*, int) method. This method will set a task to run in a defined number of milliseconds in the future.
class Scheduler
{
public:
Scheduler();
void setup();
void processMessages();
void queue(ITask* task);
void clearQueue();
void schedule(ITask*, int);
void clearSchedule();
private:
ITask* _taskQueue[QUEUE_MAX];
ScheduleItem _taskSchedule[SCHEDULE_MAX];
};
The setup() method should be called from the main setup() function of your Arduino application. Finally, you can clear the queue and the schedule by calling the clearQueue() and clearSchedule() functions.
Task Implementation
In this example I will create a task that will turn on/off a LED every 500 milliseconds. First, you must derive your task class from the ITask interface.
class Blinker : public ITask
{
public:
void setup();
void run(Scheduler*);
Blinker(int, int);
private:
int _pin;
int _period;
int _state;
};
Following is the implementation of this task …
The constructor initializes internal values. Here, you can put all the code you want to run at the time of creation of the Task object.
Blinker::Blinker(int pin, int period)
{
_pin = pin;
_state = 0;
_period = period;
}
The “setup” method will be called from the setup() method of your Arduino program. In this example we only need to configure the digital pin #13 as an output.
void Blinker::setup()
{
pinMode( _pin, OUTPUT);
}
The “run” method will be called by the scheduler when the task is ready to run. In this example, the first thing the Task does is to re-schedule itself to run in _period milliseconds. Once re-scheduled, the Task will toggle the LED connected on pin number _pin of the Arduino micro-controller.
void Blinker::run(Scheduler* scheduler)
{
scheduler->schedule(this, _period);
_state = (_state>0)?0:1;
digitalWrite(_pin, _state);
}
Finally you must create the task and schedule it for the first run. In this example, the Task is created in the global scope and scheduled in the setup() method.
// Create a task scheduler singleton
Scheduler __scheduler;
...
// Create custom tasks
Blinker _blinker12(12, 1000);
...
void setup()
{
...
_blinker12.setup();
...
__scheduler.setup();
__scheduler.queue(&_blinker12);
// or ...
//__scheduler.schedule(&_blinker12, 1000);
...
}
void loop()
{
__scheduler.processMessages();
}
You can download the complete example from my Github account. In this example I drive five different Tasks in parallel. Two tasks are driving servo motors, two others are driving LEDs and the last is printing data on the serial port.
This is a work-in-progress project and will keep you informed of new developments as I go. If you try it, please leave me your impressions in the comment section below or contact me via my Github account.
Build a Custom Arduino (Part 3) – Add a Compass
This post is the last part of this series of articles showing how to build a project using a custom Arduino board. To the accelerometer I added a compass. It begins to look more like a forest than an electronic circuit.
The compass uses the IC2 protocol to communicate with the Arduino micro-controller. That make it more complicated to interface with than the accelerometer. This protocol is handled by the library Wire.h that comes standard with the Arduino API.
In this example, you connect the SCL pin of the compass to the analog input #5 (pin #28) and the SDA pin to the analog input #4 (pin #27) of the micro-controller. As with the accelerometer, the compass is powered with the 3.3v output of the micro-controller on pin #20.
#include <Wire.h>
int x, y, z;
int pin13 = LOW;
int slaveAddress;
byte headingData[2];
int i, headingValue;
int HMC6352Address = 0x42;
void setup()
{
Wire.begin();
slaveAddress = HMC6352Address >> 1;
pinMode(13, OUTPUT);
Serial.begin(19200);
}
void loop()
{
// Send a "A" command to the HMC6352
Wire.beginTransmission(slaveAddress);
Wire.send("A"); // The "Get Data" command
Wire.endTransmission();
delay(100);
i = 0;
Wire.requestFrom(slaveAddress, 2); // Request the 2 byte heading (MSB comes first)
while(Wire.available() && i < 2)
{
headingData[i] = Wire.receive();
i++;
}
headingValue = headingData[0]*256 + headingData[1]; // Put the MSB and LSB together
Serial.print("Current heading: ");
Serial.print(int (headingValue / 10)); // The whole number part of the heading
Serial.print(".");
Serial.print(int (headingValue % 10)); // The fractional part of the heading
Serial.println(" degrees");
x = analogRead(0);
y = analogRead(1);
z = analogRead(2);
Serial.print("acceleretations are x, y, z: ");
Serial.print(x, DEC);
Serial.print(" ");
Serial.print(y, DEC);
Serial.print(" ");
Serial.println(z, DEC);
//pin13 = !pin13;
//digitalWrite(13,pin13);
delay(400);
}
The output will refresh twice a seccond and display the compass heading and the accelerometer three axis values.
I had a problem of interference between the compass and the small green LED. When the LED is turned on, the compass reading is off by up to 10 degrees. I can’t explain that one but, when the LED is disables in the code, the compass readings is back to normal and stable. If you have an explanation for that please let me know in the comment section below.
Arduino LED Matrix – Video
A short video to show the Arduino LED Matrix project in action.
LED Matrix and Arduino
Light Emitting Diodes (LED) are everywhere. There is a good reason for that, we love them! Most of us are using LEDs to communicate information, debug our code or simply add some style to our electronic projects. One of the first things one normally want to do is pack a bunch of LED to create a mini display. Lets see how we can build a simple 3×3 LED matrix display in less than an hour.
To build this simple circuit all you have to do is solder the cathodes (longer pin) of each column together and the anodes (shorter pin) of each row together. To avoid shorts between anodes and cathodes I added a layer of cardboard before to bend the anodes over the cathodes. Here is the schematics for this circuit:
Once the circuit is finished you connect the cathodes (k) to pins 2, 3 and 4 and the anodes (a) to pins 5, 6 and 7 of the Arduino board. You are done with the hardware. Lets make the program …
The following program will display a few different patterns on the LED matrix. The code is ugly and I don’t really care. We are not here to talk about code re-use but to play with LED!
You can find the code on my Github at:
https://github.com/pchretien/flash_leds
To match the code and the circuit displayed here, connect K3 with pin #5 and A3 with pin #2. I guess you will figure out how to connect the remaining LED wires!
Driving only 9 LEDs using 6 wires is far from efficient. To find out how to drive more LEDs using only 3 pins on your Arduino and an 75HC595 register chip , visit the Arduino Playground website at:
http://www.arduino.cc/en/Tutorial/ShiftOut
Have fun …










