Saturday, October 2, 2010

shakeable 20 sided die with arduino

Shakeable 20 sided die with arduino.

Here is a video

http://www.youtube.com/watch?v=j5M7wRkePJQ


HARDWARE


Arduino

Sparkfun 7 segment serial display

http://www.sparkfun.com/commerce/product_info.php?products_id=9765

3 axis accelerometer from Pololu

http://www.pololu.com/catalog/product/766

CODE


/* Shakeable 20 sided die.

Y axis on accelerometer goes to analog pin 1 on arduino
VCC and GND on accelerometer to their respective locations

RX on display to digital I/O pin 12 on arduino
VIN and GND to their respective locations

This code requires the newsoftserial library. Make sure you have it installed

My code is copyright under the Gnu General Public Licence (GLP) version 3 or any later version
The text of the GPL can be found at http://www.gnu.org/licenses/gpl.html

*/

#include //Make sure you have this library installed,

#define SerInToArdu 2 // input pin to arduino. Not used in this example
#define SerOutFrmArdu 12 // output pin from arduino to display

NewSoftSerial mySerialPort(SerInToArdu,SerOutFrmArdu); /* creates a serial channel to send the data to and from the display. */

int y = 0; // variable to store accelerometer value

int ytrip = 0; //variable to indicate if accelerometer has been tripped

long ran; // variable to store random number in

void setup() {

pinMode(SerOutFrmArdu,OUTPUT); //sets pin to output
pinMode(SerInToArdu,INPUT); /*Not actually needed... put in to be explicit as to data direction over serial lines */

mySerialPort.begin(9600);

randomSeed(analogRead(0)); // seed for random number generator using value of unused analog pin

mySerialPort.print("v"); //a special code to reset the display.
delay(1000);
mySerialPort.print("diCE"); //send word "diCE" to display

}

void loop() {

y = analogRead(1); //read Y axis value from accelerometer

if (y > 400) ytrip++; // if accelerometer value is greater than 400 then set ytrip to 1

if (ytrip == 1){ /* check if accelerometer has been tripped

Initiate for loop. Displays 10 random numbers on the display. Last number stays on display
*/

for (int num = 0; num <= 10; num++) {

ran = random(1, 20); //generate random number from 1 to 20

mySerialPort.print("v"); //reset display.

if (ran < 10) mySerialPort.print("0"); //display 0 as first digit if ran is less than 10

mySerialPort.print(ran); //send value of ran to display

delay(200);

}
}

if (ytrip > 0) ytrip = 0; //reset ytrip to 0


}

Tuesday, September 28, 2010

Scrolling text on a sparkfun 7 segment serial display with Arduino

Here is a youtube video showing scrolling text on a sparkfun 7 segment serial display.

http://www.youtube.com/watch?v=WFEq9U17cKk

Here is the arduino code.

/*Scrolling text on a sparkfun 7 segment serial display.

My code is based in part on the code in the tutorial at

http://www.arunet.co.uk/tkboyd/ec/ec1led4x7ser.htm

If you are new to this display, I suggest checking out the tutorial first.
Once you have played around with displaying text, then try my code.

The tutorial didn't specify a license for the code. Given the spirit of the Arduino
I will assume it's public domain.

My code is copyright under the Gnu General Public Licence (GLP) version 3 or any later version
The text of the GPL can be found at http://www.gnu.org/licenses/gpl.html

Many thanks to TK Boyd for creating the tutorial above.



The RX pin on the display is hooked up to digital pin 3 on the arduino. The wiper on the potentiometer is hooked up to analog pin 0 on the arduino.

This code requires the newsoftserial library. If you don't have it installed, do so.

*/



#include <NewSoftSerial.h> //Make sure you have this library installed,

#define SerInToArdu 2 // input pin to arduino. Not used in this example
#define SerOutFrmArdu 3 // output pin from arduino




NewSoftSerial mySerialPort(SerInToArdu,SerOutFrmArdu); /* creates a serial channel to send the data to and from the display. */

char text[] = " HELLO TO YOU ALL IN YOUTUBE LAND "; /* Array with our text. Blank spaces are so characters start from the right and disappear off the left side There should be 3 blank spaces in the beginning of the array text and 4 blank spaces at the end. Blogger is being stupid and only showing one blank space.*/



void setup() {
pinMode(SerOutFrmArdu,OUTPUT); //sets pin to output
pinMode(SerInToArdu,INPUT); /* Not actually needed... put in to be explicit as to data direction over serial lines */

mySerialPort.begin(9600);

mySerialPort.print("v"); /* a special code to reset the display. See tutorial above for more info on special codes */

}

void loop() {
mySerialPort.print("v"); //rest display
for (int num = 0; num <= sizeof(text) - 5; num++) { /* simple for loop. sizeof function checks size of array. Avoids having to hard code array size. The arduino laguage reference used a -1 in their example. I ended up having to use a -5 to get it to work right. Not sure exactly how it works, but it does. See arduino language reference for more info on sizeof() */


mySerialPort.print(text[num]); /* send character in array index num, to display */
mySerialPort.print(text[num+1]);/* send next character in array after index num */
mySerialPort.print(text[num+2]);/* send third character in array after index num */
mySerialPort.print(text[num+3]); /* send fourth character in array after index num */

/*In first time through loop. above code would send the contents of array indexes 0123.
Second time in loop it would send contents of indexes 1234. And so on.
The text is shifted one space each time through the loop. 4 characters are send to the display each time

Here is what it looks like. X is a blank space
1st time through loop XXXH
2nd XXHE
3rd XHEL
4th HELL
5th ELLO
6th LLOX
7th LOXT
8th OXTO
ETC ETC
*/


delay(analogRead(0)); /* delay by pot value. This controls the speed the text scrolls at */

}
delay(500); //Short pause after text is done scrolling

Saturday, September 25, 2010

Arduino and sparkfun 7 segment display

Here is a simple demo I made of an arduino and sparkfun 7 segment display.

http://www.youtube.com/watch?v=dLDefQAYy4s

Here is the code for it



/* Arduino and sparkfun 7 segment display. http://www.sparkfun.com/commerce/product_info.php?products_id=9765

Counts up to 5000 and back down to 0. Speed of counting is adjustable by a potentiometer.

My code is based off the code from this tutorial
http://www.arunet.co.uk/tkboyd/ec/ec1led4x7ser.htm

A big thanks to the person that wrote that tutorial.

You will need to install the newsoftserial library for this code to work.

Wiring is simple.

Digital pin 3 on arduino to RX on display.
Ground pin on arduino to GND on display.
5 volt pin on arduino to VCC on display

The pot is hooked up to analog input pin 0. If you don't know how to hook up a pot. Look for videos on youtube.
*/


#include //Make sure you have this library installed,

#define SerInToArdu 2 // input pin to arduino. Not used in this example
#define SerOutFrmArdu 3 // output pin from arduino




NewSoftSerial mySerialPort(SerInToArdu,SerOutFrmArdu); //creates a serial //channel to send the data to and from the display.


void setup(){

pinMode(SerOutFrmArdu,OUTPUT); //sets pin to output
pinMode(SerInToArdu,INPUT);//Not actually needed... put in to be explicit as to //data direction over serial lines

mySerialPort.begin(9600);

/* send a "v" to the display. It's a special code to reset the display. See tutorial above for more info on special codes */
mySerialPort.print("v");

}

void loop(){

for (int num = 0; num <= 5000; num++) { //simple for loop

/*reset display. Makes numbers show up better. Try it without the reset to see what it looks like*/
mySerialPort.print("v");
mySerialPort.print(num); //send value of num to display
delay(analogRead(0)); //delay by pot value
}

//same as above except for loop goes in pposite direction
for (int num = 5000; num >= 0; num--) {
mySerialPort.print("v");
mySerialPort.print(num);
delay(analogRead(0));
}

}