Showing posts with label Arduino Fun. Show all posts
Showing posts with label Arduino Fun. Show all posts

Sunday, 14 December 2014

Arduino tells the time in words

Arduino WORD CLOCK Project

Found this project Micro Word Clock and decided to give it a go, I couldn't find any code for Arduino so I mashed up some code, the code works however I want to add a flashing asterix to show seconds.

Equipment needed:

  • Arduino Uno

  • 8x8 LED Matrix with MAX7219

  • SparkFun Real Time Clock Module (DS1307)


The 8x8 LED matrix is 32mm x 32mm  so the math was easy each cell needed to be 4mm², I started by using Microsoft EXCEL to get the letters in a grid however I couldn't get the cells size to be 4mm X 4mm ( 3.97mm X 3.97mm) so I tried Openoffice and guess what no problem.  Once all letters was added you need to center text left to right and top to bottom, make all cells background black and text white, the font I used was consolas size 8.  I have added the files with the source code on github.

Wiring:

RTC DS1307

  • Connect SCL to A5

  • Connect SDA to A4

  • Connect 5V and GRD


MAX7219 - 8x8 LED MAtrix

  • Connect DIN to Pin 8

  • Connect CS to Pin 9

  • Connect CLK to Pin10


Code:
https://github.com/catchcoder/arduino/tree/master/Max7219_8x8led_with_RTC

Images:

IMG_0636 IMG_0637

Sunday, 30 October 2011

Arduino scare trick or treaters

How to scare trick or treaters with a delayed evil laugh when the door is opened.

Items required
Arduino
Surface door contact
10K Resistor
Speakers

Connect the door contact to pin 2 and 5v with a pull down 10k resistor connected to pin2 And ground
Connect the speakers (mono) to pin 11 and ground

Extra software required:

Sox or Audacity :to convert MP3 to wav 8bit
http://sox.sourceforge.net/
http://audacity.sourceforge.net/

You will need to convert the file using  sox audiodump.wav -c 1 -r 8000 -u -b evillaugh.wav

Wav2c : used to convert the audio wav file to code as seen at bottom of the code below (https://github.com/olleolleolle/wav2c)

Code

https://github.com/catchcoder/arduino

Monday, 9 May 2011

Arduino Happy Birthday source code

Arduino source code
/*  Happy Birthday Melody with flickering Candle
*  By Chris Hawkins
*  with modified code from the  Melody example
* (cleft) 2005 D. Cuartielles for K3
*
*
* Taken from (cleft) 2005 D. Cuartielles for K3
*
* The calculation of the tones is made following the mathematical
* operation:
*
*       timeHigh = period / 2 = 1 / (2 * toneFrequency)
*
* where the different tones are described as in the table:
*
* note  frequency  period  timeHigh
* c 1         261 Hz          3830  1915
* d 2         294 Hz          3400  1700
* e 3         329 Hz          3038  1519
* f 4         349 Hz          2864  1432
* g 5         392 Hz          2550  1275
* a 6         440 Hz          2272  1136
* b 7         493 Hz          2028 1014
* C 8         523 Hz         1912  956
*
* http://www.arduino.cc/en/Tutorial/Melody
*
*
*/

int speaker = 6;
int led1pin = 9; // RED LED
int led2pin = 10; // Yellow LED
int button =4; // state swicth to play music
int x = 0;
int buttonState = 0;
int length = 28; // the number of notes
char notes[] = "ccdcfe ccdcgf ccCafed bbafgf"; // a space represents a rest
int beats[] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1, 1,1,1,1,1,1,1,1,1,1,1,1,2};
int tempo = 300;

void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speaker, HIGH);
delayMicroseconds(tone);
digitalWrite(speaker, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) {
char names[] = {
'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'   };
int tones[] = {
1915, 1700, 1519, 1432, 1275, 1136, 1014, 956   };

// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}

void playmusic ()
{
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
}
else {
if (i % 2 == 0)
{
analogWrite (led1pin,200);
analogWrite (led2pin,100);
}
else {
analogWrite (led1pin,100);
analogWrite (led2pin,200);
}
playNote(notes[i], beats[i] * tempo);

}

// pause between notes
delay(tempo / 2);
}
digitalWrite (led1pin,LOW);
digitalWrite (led2pin,LOW);
}

void setup() {
pinMode(speaker, OUTPUT); /* Speaker to play tune */
pinMode(led1pin, OUTPUT); /* Red led from tri colour LED */
pinMode(led2pin, OUTPUT); /* Yellow led from tri colour LED */
pinMode (button, INPUT);  /* Start playing melody button */
}

void loop(){
buttonState = digitalRead (button); /* check if button pressed */
if (buttonState == HIGH){
playmusic();
}
}

Sunday, 9 May 2010

Acquisition of temperature data to SD Card

I have managed to hack together from various sites a temperature data logger thats writes to a 2GB SD Card, firstly I had a blown up laptop motherboard and so I desoldered the SD card socket this was not very easy as they are surface mounted and is not designed to be removed. I then soldered 9 header pins to it and used a glue gun and melted glue all around the soldered part to strenghten it, afterwards I found a better way it was to use a MicroSD convertor to a standard SD card adaptor and soldered header pins to it.



or MicroSD adaptor



Library Used:

AF_SDLog library. from http://www.ladyada.net/make/gpsshield/download.html

I also have used the Sparkfun FAT16 library and examples <HERE> for use with there shield <HERE> and changed the '#define CS    8' on line 33 to '#define CS    10' in the examples to work with my build.

Code




/************************************/

/*            SD card temperature Logger         */

/*                             Version 1.e                          */

/*                           Chris Hawkins                      */

/************************************/


#include "AF_SDLog.h"

#include "util.h"

#include <avr/pgmspace.h>



AF_SDLog card;

File f;


#define ledBusyPin 8

#define powerPin 9

#define temperaturePin 0

#define BUFFSIZE 99

#define buttonStartStop 7


int buttonState = 0;         // current state of the button

int runState = false;     // previous state of the button

float lastPressed =0;


char buffer[BUFFSIZE];

uint8_t bufferidx = 0;

uint8_t i;


unsigned int logCount;



// blink out an error code

void error(uint8_t errno) {

while(1) {

for (i=0; i<errno; i++) {

digitalWrite(ledBusyPin, HIGH);

digitalWrite(powerPin, HIGH);

delay(200);

digitalWrite(ledBusyPin, LOW);

digitalWrite(powerPin, LOW);

delay(200);

}

for (; i<10; i++) {

delay(300);

}

}

}


void setup()

{


Serial.begin(9600);

putstring_nl("\r\nData logger");


// configure PINS

pinMode(ledBusyPin, OUTPUT);

pinMode(powerPin, OUTPUT);

pinMode(buttonStartStop, INPUT);

digitalWrite(powerPin, HIGH);


// check SD card - FAT 2GB

if (!card.init_card()) {

putstring_nl("Card init. failed!");

error(1);

}

if (!card.open_partition()) {

putstring_nl("No partition!");

error(2);

}

if (!card.open_filesys()) {

putstring_nl("Can't open filesys");

error(3);

}

if (!card.open_dir("/")) {

putstring_nl("Can't open /");

error(4);

}


// Check for next file in sequence 00 to 99

strcpy(buffer, "TMPLOG00.TXT");

for (buffer[6] = '0'; buffer[6] <= '9'; buffer[6]++) {

for (buffer[7] = '0'; buffer[7] <= '9'; buffer[7]++) {

putstring("\ntrying to open ");

Serial.println(buffer);

f = card.open_file(buffer);

if (!f)

break;

card.close_file(f);

}

if (!f)

break;

}


if(!card.create_file(buffer)) {

putstring("couldnt create ");

Serial.println(buffer);

error(5);

}

f = card.open_file(buffer);

if (!f) {

putstring("error opening ");

Serial.println(buffer);

card.close_file(f);

error(6);

}

putstring("writing to ");

Serial.println(buffer);

putstring_nl("ready!");


delay(250);


// clear buffer

strcpy(buffer,"");

}



void loop(){


// get button state

buttonState = digitalRead(buttonStartStop);


//  if button pressed and 2 seconds passed

if (buttonState ==HIGH && (millis() - lastPressed)>2000) {

lastPressed = millis();

runState = !runState;


}

// if the state has true then log temperature data

if (runState == true) {


// build string to write to card

sprintf(buffer, "%s,%d,%u,%d\n",getDate(),getTime (),logCount++,getTemperature());


//log data to SD Card

logData();


}

}



int getTemperature(){


//getting the voltage reading from the temperature sensor

float temperature = getVoltage(temperaturePin);


//converting from 10 mv per degree wit 500 mV offset

temperature =  (temperature - .5) * 100;


return temperature;

}


unsigned int getTime(){

//replace with RTC code once added

return  (millis() /1000);

}


char* getDate (){

//replace with RTC code once added

return "24/04/2010";

}

float getVoltage(int pin){

return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range

// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts

// using TMP36 temperature sensor

}


void logData( )

{


digitalWrite(ledBusyPin, HIGH);// LED on for witting to card


// get string length

bufferidx = strlen(buffer);


// write data to sd card

if(card.write_file(f, (uint8_t *) buffer, bufferidx) != bufferidx) {

putstring_nl("can't write!");

error(5);


}


digitalWrite(ledBusyPin, LOW);// LED off to show finished writting to card


}

/* End code */





Sample of output to SD card



Schematic

Friday, 5 March 2010

Arduino and TMP36 (temperature sensor) Part 2

I have now added conversion from Celsius and Fahrenheit via a push button, so you just need to press and hold the button until the display shows either a C or an F then you can release the button and the conversion is done.

Latest code v1.4 can be found here http://code.google.com/p/arduino-fun/downloads/list
// Arduino - TMP36 and 7 segment LED v1.3
// By Chris Hawkins
// February 2010
// Temperature code from http://oomlout.com/TMP36/TMP36-Guide.pdf
//
// using bitRead help from http://arduinofun.com/blog/2009/12/06/connecting-a-7-segment-led-to-the-arduino-build-it/
//
// Changes from 1.2
// Push button added to pin 12
// Added conversion scales from Celsius and Fahrenheit
// streamlined splitting the two digits

// Code Start
// pins that 7 segment is connect to
int ledSegment[] = {
  2,3,4,5,6,7,8,9};

// Analog pin that TMP36 is connect to
int tempMonitor =0;

//pin for Switching from C to F
int pinSwitch =12;

// Button state for temperature scales
boolean displayType = false;
// encode the on/off state of the LED segments for the characters
// '0' to '9' and 'DP' into the bits of the bytes
const byte numDef[11] = {
  63, 6, 91, 79, 102, 109, 124, 7, 127, 103,128 };

void setup()
{
  // Serial.begin (9600);

  // set pinmode for switch
  pinMode (pinSwitch,INPUT);

  // Sets the pinMode for all LEDs
  for (int x = 0; x<=7; x++)
  {
    pinMode (ledSegment[x],OUTPUT);
  }

  // enable on by one each LED to test them.
  for (int x =0;x<=7;x++){
    digitalWrite (ledSegment[x],HIGH);
    delay (50);

  }
  // disable one by one to finish test of each LED
  for (int x =0;x<=7;x++){

    digitalWrite (ledSegment[x],LOW);
    delay (50);
  }
}

void loop(){

  // Check if button is pressed
  if (digitalRead(pinSwitch)) {
    displayType = !displayType;
    if (displayType) {
      setSegments(113); // send byte for C to display
      delay(1500);
    }
    else{
      setSegments(57); // send byte to F to display
      delay(1500);
    }
  }

  float temperature = getVoltage(tempMonitor);  //getting the voltage reading from the temperature sensor
  temperature = (temperature - .5) * 100;          //converting from 10 mv per degree wit 500 mV offset

  if (displayType){
    temperature = ((temperature *1.8) +32);
  }

  // Print temperature to serial for use in processing
  //Serial.println (temperature);

  // Split two digit number to send to display
  int digit1 = temperature / 10;
  int digit2 = int(temperature) % 10;
  // send ten's number to display
  setSegments( numDef[digit1] );

  // Delay to display the 10's digit of the temperature
  delay (1000);

  // send unit number to display and DP
  setSegments( numDef[digit2] +  numDef[10] );

  // Delay to display the unit digit of the temperature
  delay (2000);
}

void setSegments(byte segments)
{
  // for each of the segments of the LED
  for (int s = 0; s < 8; s++)
  {
    int bitVal = bitRead( segments, s ); // grab the bit
    digitalWrite(s+2, bitVal); // set the segment
  }
}

float getVoltage(int pin){
  return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range
  // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}

Breadboard view of how I configured the Arduino:

[caption id="attachment_82" align="alignnone" width="300" caption="Designed using the Fritzing program"]Breadboard view[/caption]

Sunday, 28 February 2010

Arduino and TMP36 (temperature sensor)

Here is my first build of a temperature sensor and 7 segment LED connected to an Arduino Duemilanove, the display displays the first digit (10's) and pauses and then displays the second digit (units) pauses and then the decimal point to show end/start of temperature display.     

     

And here is the video:     





     
  

The full source code below

// Arduino - TMP36 and 7 segment LED
// By Chris Hawkins
// February 2010
// Temperature code from http://oomlout.com/TMP36/TMP36-Guide.pdf
//
// using bitRead help from connecting-a-7-segment-led-to-the-arduino-build-it/

// pins that 7 segment is connect to
int ledSegment[] = {
2,3,4,5,6,7,8,9};     

// Analog pin that TMP36 is connect to
int tempMonitor =0;     

// encode the on/off state of the LED segments for the characters
// '0' to '9' and 'DP' into the bits of the bytes
const byte numDef[11] = {
63, 6, 91, 79, 102, 109, 124, 7, 127, 103,128 };     

void setup()
{
Serial.begin (9600);     

// Sets the pinMode for all LEDs
for (int x = 0; x<=7; x++)
{
pinMode (ledSegment[x],OUTPUT);
}     

// enable on by one each LED to test them.
for (int x =0;x<=7;x++){
digitalWrite (ledSegment[x],HIGH);
delay (50);     

}
// disable one by one to finish test of each LED
for (int x =0;x30 ){
setSegments( numDef[3] );
}
// check Temperature is over 20 and below 30
else if (temperature >=20 && temperature =10 && temperature =10){
temperature=temperature -10;
}     

// Print temperature to serial for use in processing
Serial.println(temperature);     

// Delay to display the 10's digit of the temperature
delay (1000);     

// now display unit of the temperature
for (int x = 0;x<=9;x++){
if (int(temperature) == x){
setSegments( numDef[x] );
}
}
// Delay to display the unit digit of the temperature
delay (1000);     

// Now display the decimal point (DP)
setSegments( numDef[10] );     

// delay to show DP
delay (2000);     

}     

void setSegments(byte segments)
{
// for each of the segments of the LED
for (int s = 0; s < 8; s++)
{
int bitVal = bitRead( segments, s ); // grab the bit
digitalWrite(s+2, bitVal); // set the segment
}
}     

float getVoltage(int pin){
return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}     


todos's : make code work easily with temperature greater than 30
Next project add two 7 segment LEDs and add shift registers

Thursday, 11 February 2010

Arduino in a box



Arduino in a box

Originally uploaded by Christhawkins


Well I mean on a breadboard at the moment but it will be in a box running the Scalextric Lap Counter code.

For prototyping/testing I have used 5v LED's but will us 2.5v LED's in the working model. I have also used a 24mhz Crystal because I haven't got to Maplins yet, so timing is out but works fine for this test.

Next I need to add reset switch..

Sunday, 7 February 2010

Scalextric Lap Counter Schematic for Arduino



Scalextric Lap Counter Schematic for Arduino

Originally uploaded by Christhawkins


Here is my Schematic for a Scalextric Lap counter that uses and Arduino and Processing, the Arduino controls the sensors, LED's and buttons and it then relays the information via Serial to the Processing program. I have illustrated on the diagram a optocoulper but I had actual used a slotted Optical Switch (HA221).

Flickr

This is a test post from flickr, a fancy photo sharing thing.

Saturday, 30 January 2010

Atmega328 c/w Arduino bootloader on breadboard

Here is my start of making an atmega328 with arduino bootloader to work standalone either on a breadboard or in a IC socket on a veroboard, I have stuck a pinout label so it should help.

Tuesday, 8 December 2009

Arduino - code - mood lamp

byte ledRed =9;
byte ledGrn = 10;
byte ledBlu = 11;
byte ledReady = 13;
byte button[] = {2, 3, 4};
int buttonstate[]= {0,0,0};
int buttonlevel[]= {0,0,0};
int ledval[] = {0,50,100,150,200,255};
unsigned long lastpressed[] = {0,0,0};

void setup(){
 
  pinMode (ledReady,OUTPUT); // I use this to notify me when reset completed
  pinMode (ledRed,OUTPUT);
  pinMode (ledGrn, OUTPUT);
  pinMode (ledBlu, OUTPUT);
  for (int x =0;x<=3; x++)
  {
  pinMode (button[x], INPUT); // configure buttons
  }
  digitalWrite (ledRed, LOW);
  digitalWrite (ledGrn, LOW);
  digitalWrite (ledBlu, LOW);
  digitalWrite (ledReady,HIGH); // I use this to notify me when reset completed
  /* Serial.begin(9600); // for debugging only */
}

void loop(){
 

for (int x = 0;x <=2; x++){

buttonstate[x] = digitalRead (button[x]); // check if 1 of 3 buttons pressed
 
if (buttonstate[x] == HIGH  && (millis()- lastpressed[x]) >500) {
 buttonlevel[x] +=1;
if (buttonlevel[x] > 5) {buttonlevel[x] = 0;}

 /* Serial.println ( buttonlevel[x]); // for debug */
lastpressed[x] = millis(); // get time of when button pressed used for debounce

}
}
  analogWrite (ledRed, ledval[buttonlevel[0]]); // set level of Red LED
  analogWrite (ledGrn, ledval[buttonlevel[1]]); // set level of Green LED
  analogWrite (ledBlu, ledval[buttonlevel[2]]); // set level of Blue LED */
   delay (250);
 }

Sunday, 6 December 2009

Arduino mood lamp

Built another Arduino prototype of mood lamp controlled by 3 button to change individual light levels of RGB leds, next version will be automatically controlled.

Arduino - code - Happy Birthday Melody with flickering candle

/*  Happy Birthday Melody with flickering Candle
 *  By Chris Hawkins  November 2009
 *  with modified code from the  Melody example
 * (cleft) 2005 D. Cuartielles for K3
 *
 *
 * Taken from (cleft) 2005 D. Cuartielles for K3
 *
 * The calculation of the tones is made following the mathematical
 * operation:
 *
 *       timeHigh = period / 2 = 1 / (2 * toneFrequency)
 *
 * where the different tones are described as in the table:
 *
 * note  frequency  period  timeHigh
 * c 1         261 Hz          3830  1915  
 * d 2         294 Hz          3400  1700  
 * e 3         329 Hz          3038  1519  
 * f 4         349 Hz          2864  1432  
 * g 5         392 Hz          2550  1275  
 * a 6         440 Hz          2272  1136  
 * b 7         493 Hz          2028 1014 
 * C 8         523 Hz         1912  956
 *
 * http://www.arduino.cc/en/Tutorial/Melody
 *
 *
 */ 
int speaker = 6;
int led1pin = 9; // RED LED
int led2pin = 10; // Yellow LED
int button =4; // state swicth to play music
int x = 0;
int buttonState = 0;
int length = 28; // the number of notes
char notes[] = "ccdcfe ccdcgf ccCafed bbafgf"; // a space represents a rest
int beats[] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1, 1,1,1,1,1,1,1,1,1,1,1,1,2};
int tempo = 300; 
void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speaker, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speaker, LOW);
    delayMicroseconds(tone);
  }

void playNote(char note, int duration) {
  char names[] = {
    'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'   };
  int tones[] = {
    1915, 1700, 1519, 1432, 1275, 1136, 1014, 956   }; 
  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }

void playmusic ()
{
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    }
    else {
      if (i % 2 == 0)
      {
        analogWrite (led1pin,200);
       analogWrite (led2pin,100);
      }
      else {
        analogWrite (led1pin,100);
        analogWrite (led2pin,200);
      }
      playNote(notes[i], beats[i] * tempo);
     
    } 
    // pause between notes
    delay(tempo / 2);
  }
  digitalWrite (led1pin,LOW);
  digitalWrite (led2pin,LOW);

void setup() {
  pinMode(speaker, OUTPUT); /* Speaker to play tune */
  pinMode(led1pin, OUTPUT); /* Red led from tri colour LED */
  pinMode(led2pin, OUTPUT); /* Yellow led from tri colour LED */
  pinMode (button, INPUT);  /* Start playing melody button */

void loop(){
  buttonState = digitalRead (button); /* check if button pressed */
  if (buttonState == HIGH){
    playmusic();
  }
}

Saturday, 5 December 2009

Arduino Happy Birthday Melody with flickering candle

Here is my Arduino playing Happy Birthday that I coded and designed for My Boys birthday http://www.youtube.com/watch?v=PNmP1wJUhgc

Christmas Tree 2009

Been to B&Q today with family to get our Christmas Tree and now it's fully decorated it looks lovely.

Hello world!

Welcome to Wordpress.com. This is your first post. Edit or delete it and start blogging!