Thursday 26 May 2011

Arduino analog Clock

Continuing the clock theme, this one uses the marvelous TVOut library to do (admittedly grainy) analog clock .

//start "sketch"
#include <TVout.h>
#include "Wire.h"
//#include <pollserial.h>
#include <fontALL.h>

TVout TV;
//pollserial pserial;

#define DS1307_I2C_ADDRESS 0x68 // This is the I2C address
// Global Variables
long previousMillis = 0; // will store last time time was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
long interval = 200;
int tvx,tvy,tvradius,x2,y2,x3,y3;

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());
hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());

}


void printTime()
{
TV.set_cursor(0,0);
TV.print( char( hour/10 + 0x30) );
TV.print( char( hour%10 + 0x30) );
TV.print(":");
TV.print( char(minute/10 + 0x30));
TV.print( char(minute%10 + 0x30));
TV.print(":");
TV.print(char (second/10+0x30));
TV.print(char (second%10+0x30));


TV.set_cursor(8,9);
TV.print(char(dayOfMonth/10+0x30));
TV.print(char(dayOfMonth%10+0x30));
TV.print("/");
TV.print(char(month/10+0x30));
TV.print(char(month%10+0x30));
TV.set_cursor(10,18);
TV.print("20");
TV.print(char(year/10+0x30));
TV.print(char(year%10+0x30));

TV.draw_circle(tvx,tvy,tvradius-5,BLACK,BLACK);

float angle = second*6 ;
angle=(angle/57.29577951) ; //Convert degrees to radians
x3=(tvx+(sin(angle)*(tvradius-6)));
y3=(tvy-(cos(angle)*(tvradius-6)));
TV.draw_line(tvx,tvy,x3,y3,WHITE);
angle = minute * 6 ;
angle=(angle/57.29577951) ; //Convert degrees to radians
x3=(tvx+(sin(angle)*(tvradius-11)));
y3=(tvy-(cos(angle)*(tvradius-11)));
TV.draw_line(tvx,tvy,x3,y3,WHITE);
angle = hour * 30 + int((minute / 12) * 6 ) ;
angle=(angle/57.29577951) ; //Convert degrees to radians
x3=(tvx+(sin(angle)*(tvradius-15)));
y3=(tvy-(cos(angle)*(tvradius-15)));
TV.draw_line(tvx,tvy,x3,y3,WHITE);
}


void setup() {
Wire.begin();
getDateDs1307() ;
//TV.begin(_NTSC,184,72);
//the circle didn't look very round with 184,72
TV.begin(_NTSC,150,71);
TV.select_font(font6x8);
TV.clear_screen();
//TV.println("TV Clock");
tvx= TV.hres()/2;
tvy=TV.vres()/2;
tvradius=TV.vres()/2 ;
//clock face
TV.draw_circle(tvx,tvy,tvradius,WHITE);
//hour ticks
for( int z=0; z < 360;z= z + 30 ){
//Begin at 0° and stop at 360°
float angle = z ;
angle=(angle/57.29577951) ; //Convert degrees to radians
x2=(tvx+(sin(angle)*tvradius));
y2=(tvy-(cos(angle)*tvradius));
x3=(tvx+(sin(angle)*(tvradius-5)));
y3=(tvy-(cos(angle)*(tvradius-5)));

TV.draw_line(x2,y2,x3,y3,WHITE);
}
//TV.set_hbi_hook(pserial.begin(57600));
}

void loop() {
// if (pserial.available()) {
// TV.print((char)pserial.read());
// }

// other processing here maybe

unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
// save the last time you printed and updated

previousMillis = currentMillis;
TV.delay(1);
printTime();
//read RTC
getDateDs1307() ;
}


}

//end sketch
Once again just a clock, blocky but readable :

Arduino LCD Clock

Using the Wire library to read a DS1307 RTC chip (I used this one from Seeed Studio and bits of the example code ) and the LCD3wire library to implement, what is almost the "hello world" of arduino projects, a digital clock. The LCD3wire example uses a 4094 shift register, my parts box only had a 74HC595 so the connections are a little different:





















74HC595LCDArduinopower
14

25

36

414

513

612

711

8

gnd
9


10

+ve
11
4 (clk)
12
2 (strobe)
13

gnd
14
8 (data)
15


16

+ve

1
gnd

2
+ve

3
2.2K to gnd

//the "sketch"
// Example use of LCD3Wire and Wire library
// to read time from RTC and display on LCD

#include <LCD3Wire.h>
#include "Wire.h"
// Arduino pins
#define LCD_LINES 2 // number of lines in your display
#define DOUT_PIN 8 // Dout pin
#define CLK_PIN 4 // Clock pin
#define STR_PIN 2 // Strobe pin

#define DS1307_I2C_ADDRESS 0x68

long previousMillis = 0; // will store last time time was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
long interval = 200;

//create object to control an LCD.
LCD3Wire lcd = LCD3Wire(LCD_LINES, DOUT_PIN, STR_PIN, CLK_PIN);
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());
hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());

}

void printTime()
{
lcd.cursorTo(1, 4); //line=1, x=4.
lcd.print( char( hour/10 + 0x30) );
lcd.print( char( hour%10 + 0x30) );
lcd.print(':');
lcd.print( char(minute/10 + 0x30));
lcd.print( char(minute%10 + 0x30));
lcd.print(':');
lcd.print(char (second/10+0x30));
lcd.print(char (second%10+0x30));

lcd.cursorTo(2, 3); //line=2, x=3.
lcd.print(char(dayOfMonth/10+0x30));
lcd.print(char(dayOfMonth%10+0x30));
lcd.print('/');
lcd.print(char(month/10+0x30));
lcd.print(char(month%10+0x30));
lcd.printIn("/20");
lcd.print(char(year/10+0x30));
lcd.print(char(year%10+0x30));
}

void setup() {
Wire.begin();
getDateDs1307() ;
lcd.init();
lcd.clear();

}

void loop() {

unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
// save the last time you printed and updated
previousMillis = currentMillis;
printTime();
//read RTC
getDateDs1307() ;
}
}
//end sketch

The result as expected (just a clock :-) ):


Wednesday 18 May 2011

Android tweeting with python tweepy and sl4a

my sweetheart bought me some cute android toys, thanks dear(my sweetheart gave me this cute android toy )

Jeff Miller's blog has an easy how-to tweet using Joshua Roesslein's tweepy module for python. You can run this on your computer (for ease of editing) to generate the access and consumer keys and then do stuff with SL4A on an android phone. The linux-mag.com tutorial link is using the previously available Basic Authentication, the tweepy module does all the heavy lifting to use Oauth (my previous caveats about implementing on lower spec platforms remain) instead. To use make a tweepy directory under sl4a/scripts and copy the *.py out of the tweepy egg or tgz.

The following reads your friends timeline into androids notification bar
#!/usr/bin/env python

import sys
import tweepy
import android
droid=android.Android()

CONSUMER_KEY = 'paste your Consumer Key here'
CONSUMER_SECRET = 'paste your Consumer Secret here'
ACCESS_KEY = 'paste your Access Key here'
ACCESS_SECRET = 'paste your Access Secret here'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

for tweet in friend_tweets:
droid.notify(tweet.author.name,tweet.text)

To send a tweet ( something like this) :
#!/usr/bin/env python

import sys
import tweepy
import android
droid=android.Android()

CONSUMER_KEY = 'paste your Consumer Key here'
CONSUMER_SECRET = 'paste your Consumer Secret here'
ACCESS_KEY = 'paste your Access Key here'
ACCESS_SECRET = 'paste your Access Secret here'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

tweet = droid.dialogGetInput('Twitter', 'Type your tweet here :').result
api.update_status(tweet)


Nintendo


Brick Nintendo before they brick you!