Sunday, February 28, 2010

Dice Part 2

I tracked down a 7 segment LED display so that I could display "real" numbers for each number. Code and Fritzing screen shot follows:

int roll;
int push;

void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
Serial.begin(9600);
}

void loop()
{
push = analogRead(0);
//delay(1000);
if (push > 100)
{
roll = random(1,7);
//generates random number for roll
digitalWrite(13,0);
digitalWrite(12,0);
digitalWrite(11,0);
digitalWrite(10,0);
digitalWrite(9,0);
digitalWrite(8,0);
digitalWrite(7,0);
//resets all segments to off
delay(1000);
Serial.println(roll);
switch (roll) {
case 1:{
digitalWrite(10,1);
digitalWrite(7,1);}
break;
case 2:{
digitalWrite(13,1);
digitalWrite(11,1);
digitalWrite(10,1);
digitalWrite(9,1);
digitalWrite(8,1);}
break;
case 3:{
digitalWrite(13,1);
digitalWrite(11,1);
digitalWrite(10,1);
digitalWrite(8,1);
digitalWrite(7,1);}
break;
case 4:{
digitalWrite(13,1);
digitalWrite(12,1);
digitalWrite(10,1);
digitalWrite(7,1);}
break;
case 5:{
digitalWrite(13,1);
digitalWrite(12,1);
digitalWrite(11,1);
digitalWrite(8,1);
digitalWrite(7,1);}
break;
case 6:{
digitalWrite(13,1);
digitalWrite(12,1);
digitalWrite(11,1);
digitalWrite(9,1);
digitalWrite(8,1);
digitalWrite(7,1);}
}
}
}

Overlapping wires makes it difficult to see whats going on.

EDIT
The sequence of numbers is not random between runs. To fix this randomSeed(analogRead(5)) is added to void setup().

Binary Dice

Editing my previous code and adding a button (which is modified from the LDR project) I made a simple die program that will randomly select a number between 1 and 6 and output the value in binary:

int onePin = 13;
int twoPin = 12;
int fourPin = 11;
int roll;
int push;

void setup() {
pinMode(onePin, OUTPUT);
pinMode(twoPin, OUTPUT);
pinMode(fourPin, OUTPUT);
Serial.begin(9600);
}

void loop()
{
push = analogRead(0);
//delay(1000);
if (push > 100)
{
roll = random(1,7);
//generates random number for roll
delay(1000);
switch (roll) {
case 1:{
digitalWrite(onePin,1);
digitalWrite(twoPin,0);
digitalWrite(fourPin,0);}
break;
case 2:{
digitalWrite(onePin,0);
digitalWrite(twoPin,1);
digitalWrite(fourPin,0);}
break;
case 3:{
digitalWrite(onePin,1);
digitalWrite(twoPin,1);
digitalWrite(fourPin,0);}
break;
case 4:{
digitalWrite(onePin,0);
digitalWrite(twoPin,0);
digitalWrite(fourPin,1);}
break;
case 5:{
digitalWrite(onePin,1);
digitalWrite(twoPin,0);
digitalWrite(fourPin,1);}
break;
case 6:{
digitalWrite(onePin,0);
digitalWrite(twoPin,1);
digitalWrite(fourPin,1);}
}
}
}

Again readability has suffered due to blogger not liking indentation.

Binary Counter

Was bored in the Project room the other day and wrote this simple program that can count from 0 to 15 in binary using 4 LEDs on pins 13,12,11 and 10:

int onePin = 13;
int twoPin = 12;
int fourPin = 11;
int eightPin = 10;
int del =1000;
int one = 0;
int two = 0;
int four = 0;
int eight = 0;

void setup() {
pinMode(onePin, OUTPUT);
pinMode(twoPin, OUTPUT);
pinMode(fourPin, OUTPUT);
pinMode(eightPin, OUTPUT);
}

void loop()
{
if (one == 1)
{
one = 0;
if (two == 1)
{
two = 0;
if (four == 1)
{
four = 0;
if (eight == 1)
{
eight = 0;
}
else
{
eight = 1;
}
}
else
{
four = 1;
}
}
else
{
two = 1;
}
}
else
{
one = 1;
}
if (one == 1) { digitalWrite(onePin, HIGH); } else { digitalWrite(onePin, LOW); }
if (two == 1) { digitalWrite(twoPin, HIGH); } else { digitalWrite(twoPin, LOW); }
if (four == 1) { digitalWrite(fourPin, HIGH); } else { digitalWrite(fourPin, LOW); }
if (eight == 1) { digitalWrite(eightPin, HIGH); } else { digitalWrite(eightPin,LOW); }
delay(del);
}



The code is written with nested if statements. A way to re-write this code so it would look a lot cleaner and easier to read is to use a case statement (it doesn't help that blogger doesn't carry over my code indentation either).

Analogue Inputs giving Serial Outputs (Tasks 24, 26)

http://www.ladyada.net/learn/sensors/cds.html
This site is a great resource for info on LDRs and the following program is taken from this site.

The code is starting to gain some more practical purposes. With this code (and circuit diagram) you can read the resistance of an LDR (Light De pendant Resistor) and output a value. We use the analogue pins along the bottom edge of the board to read a value between 0 and 1024 from the LDR. That value can then be used to determine the light levels in the room:

int reading;

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

void loop() {
reading = analogRead(0);

Serial.print("Analog reading = ");
Serial.print(reading);

if (reading < 10) {Serial.println (" - Dark");}
else if (reading < 200) {Serial.println (" - Dim");}
else if (reading < 500) {Serial.println (" - Light");}
else if (reading < 800) {Serial.println (" - Bright");}
else {Serial.println (" - Very Bright");}
delay(1000);
}


(this image was copied directly from Peter's Blog http://embeddednotices.blogspot.com/)

In the well lit room D207, the highest value I was able to cause was 724, and the lowest was 34.

Blinking with Serial Output (Tasks 21, 22)

This piece of code will flash an LED on, output a message that can be read by a serial monitor, then turn it off:

void setup(){
Serial.begin(9600);
pinMode(13, OUTPUT);
}

void loop() {
Serial.println("RED");
digitalWrite(13,1);
delay(1000);
digitalWrite(13,0);
delay(1000);
}

This can be made more complex by adding another LED and give corresponding output to match:

void setup(){
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}

void loop() {
Serial.println("RED");
digitalWrite(13,1);
delay(1000);
digitalWrite(13,0);
Serial.println("BLUE");
digitalWrite(12,1);
delay(1000);
digitalWrite(12,0);
}

Ascending then Descending Flash Rate (Task 19, 20)

This code has two separate loops that are entered depending on the value of a boolean:

int rate = 100;
boolean up = true;

void setup() {
pinMode(13, OUTPUT);
}

void loop()
{
if (up == true){
digitalWrite(13,1);
delay(rate);
digitalWrite(13,0);
delay(rate);
if (rate == 1000){
up = false;
}
else{
rate = rate + 100;
}
}
else
{
digitalWrite(13,1);
delay(rate);
digitalWrite(13,0);
delay(rate);
if (rate == 0){
up = true;
}
else{
rate = rate - 100;
}
}
}

This code starts with an if ... else loop. The first if checks if the up variable is true, if so it enters the ascending loop (if false it enters the descending loop). It then flashes with it's current rate (starts at 100ms) then increments the value by 100 until it reaches 1000. At that point the boolean value is switched to false and it enters the second loop. The LED then flashed with the current rate (1000 when entering the loop) and then decrements the value by 100 until it reaches 100 and switches the boolean again to start from the beginning.

This code can be altered so that the rate of flashing moves in 10ms increments down to 10ms instead of 100ms

int rate = 10;
boolean up = true;

void setup() {
pinMode(13, OUTPUT);
}

void loop()
{
if (up == true){
digitalWrite(13,1);
delay(rate);
digitalWrite(13,0);
delay(rate);
if (rate == 1000){
up = false;
}
else{
rate = rate + 10;
}
}
else
{
digitalWrite(13,1);
delay(rate);
digitalWrite(13,0);
delay(rate);
if (rate == 0){
up = true;
}
else{
rate = rate - 10;
}
}
}

Blinking using a for loop (Tasks 17,18)

The following code uses a for loop to blink the LED on pin 13 5 times after blinking one on pin 12:

void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}

void loop()
{
digitalWrite(12, 1);
delay(500);
digitalWrite(12,0);
delay(500);
for (int i=0; i < 5; i++){
digitalWrite(13,1);
delay(500);
digitalWrite(13,0);
delay(500);
}
}

The number of times that the second LED flashes can be changed simply by changing the for statement for example: for (int i=0; i < 23; i++) will flash 23 times rather than 5

Alternating Blinking (Tasks 11, 12, 13, 14, 16)

Basic program that will alternate between two LEDs flashing with no delay between On/Off

void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}

void loop()
{
digitalWrite(13, 1);
digitalWrite(12, 0);
delay(500);
digitalWrite(13, 0);
digitalWrite(12, 1);
delay(500);
}

You can alter the flashing pattern by changing the delay. For example:

void loop()
{
digitalWrite(13, 1);
digitalWrite(12, 0);
delay(250);
digitalWrite(13, 0);
digitalWrite(12, 1);
delay(1000);
}

This code will cause the LED on pin 13 to flash for a short period, then the LED on pin 12 to flash for a longer period of time. You could also cause the opposite by swapping the delay values.

This code is similar to the first program except there is a delay between each LED being turned on:
void loop()
{
digitalWrite(13, 1);
delay(500);
digitalWrite(12, 0);
delay(500);
digitalWrite(13, 0);
delay(500);
digitalWrite(12, 1);
delay(500);
}

This is yet another flashing pattern where one led flashes twice then the other once then repeat:
{
digitalWrite(13, 1);
delay(500);
digitalWrite(13, 0);
delay(500);
digitalWrite(13, 1);
delay(500);
digitalWrite(13, 0);
delay(500);
digitalWrite(12, 1);
delay(500);
digitalWrite(13, 0);
delay(500);
}

Thursday, February 25, 2010

Blink (First Arduino Program) (Tasks 7,8,9,10)

Here is my version of blink:
void setup() {
pinMode(13,OUTPUT);
}

void loop()
{
digitalWrite(13, 1);
delay(500);
digitalWrite(13, 0);
delay(500);
}

It works off pin 13. Turns the LED on, waits ~500ms and turns the LED off, waits ~500ms again and then restarts the loop.

An easy way to vary this program would be to increase or decrease the speed of the blink. An example of code that would cause a long delay between short blinks would look like this

digitalWrite(13, 1);
delay(250);
digitalWrite(13, 0);
delay(1000);

This would turn the LED on for ~250ms and then turn it off for ~1 second. You could also do the opposite with this code

digitalWrite(13, 1);
delay(1000);
digitalWrite(13, 0);
delay(250);

By changing both the delays to 10ms, the LED appears to constantly stay on and not blink, at 20ms it appears to “flicker” and at 40ms it begins to flash.