Thursday, March 18, 2010

Infrared research:

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

http://www.rtfa.net/tag/arduino

http://robot-overlord.blogspot.com/2009/04/simple-ir-detection-circuit-tutorial.html

Interesting Websites for ideas...


1) http://www.instructables.com/




2) http://www.arduino.cc/playground/Projects/ArduinoUsers




3)www.youtube.com




IR Tailgate Buddy: Robotic Power Wheels Vehicle
http://www.youtube.com/watch?v=Pbob9y5NMQ0



4) http://www.coolcircuit.com/




More tasks...

34) Write a program to generate 100 random numbers between 0 and 9 to the screen.
35) Same as above but this time put your outputs in a numbered list like this:
1. 7
2. 4
3. 9 etc til you get to 100.
36) Same but also output the average (mean) of the 100 numbers with a suitable message.
37) Same but this time see if you can drive the baud rate up as far as possible before the output data gets scrambled. Also, what is the slowest speed the Arduino serail will ouitput at? Write your findings in your blog.
38) Write a program to output the value of your LDR whenever you press the button.
39) Same as 38 but this time write your output like this :
Reading number 1 is 345
Reading number 2 is 123
Reading number 3 is 876 etc...

Next topic will be on reporting back about the YABB board.

Thursday, March 11, 2010

LED project...


My plan for the LED project was to create an electronic dice. I found an example on the internet and tryed to base mine off that. To get the dice to act realisticly, the lights formation would have to be randomized. There was a page on the arduino website which was really useful = http://www.arduino.cc/en/Reference/Random Then i worked out the formation and how it would work. The LEDs would be grouped so that when the random number was called, each LED would not need to be callled individually. The groups were 1, 2, 3 & 4. Code:
int pinLeds1 = 10; //2 corner LEDs
int pinLeds2 = 9; //2 outer middle LEDs
int pinLeds3 = 8; //Other 2 corner LEDs
int pinLed4 = 7; //middle LED
int buttonPin = 6; //Button input reader
int buttonState; //On or off (HIGH/LOW)
int rGen; //random generator variable
int time = 4000; //hold for 4 seconds.

void setup () {
pinMode (pinLeds1, OUTPUT);
pinMode (pinLeds2, OUTPUT);
pinMode (pinLeds3, OUTPUT);
pinMode (pinLed4, OUTPUT);
pinMode (buttonPin, INPUT);
randomSeed(analogRead(0));
}

void loop() {
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH){ //if the button has been pressed
rGen = random(1, 7); //rGen=random number between 1 and 6.

if (rGen == 1){ //light up centre light.
digitalWrite (pinLed4, HIGH);
delay (time);
}

if (rGen == 2){ //Light up 2 corner lights.
digitalWrite (pinLeds1, HIGH); delay (time);
}
if (rGen == 3){ //Light up 2 corner and middle lights.
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLed4, HIGH);
delay (time);
}
if (rGen == 4){ //Light up all 4 corners.
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds3, HIGH);
delay (time);
}

if (rGen == 5){ //Light up all 4 corners plus middle light.
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLed4, HIGH);
delay (time);
}
if (rGen == 6){ //light up all except middle.
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds2, HIGH);
digitalWrite (pinLeds3, HIGH);
delay (time);
}
} //else if not pressed and time has ended turn all off.
digitalWrite (pinLeds1, LOW);
digitalWrite (pinLeds2, LOW);
digitalWrite (pinLeds3, LOW);
digitalWrite (pinLed4, LOW);
}//end.

At run time, something was not right and nothing lit up at all. Because I was basing mine on one i found on the internet, i went back and looked through what the example had. The bread board i was using was a completely different set up. The example had a ground row that all the resistors were connected too. Mine did not. After checking all the wires and LEDs were connected in the right pattern and formation, I went to check through my code. As this is very low level code it would have been easy to spot any errors, but there appeared to be none. With no other resources avalible to me, I had to show what I had created in class, even though it did not work.

After borrowing another bread board from Adam which had a ground row in it, i was able to re wire up my project and test it! My excitement faded when only a few of the LED lit up, but i quickly realised it was because several of my resistors were ones that I found lying around home, and were not the right level to allow much power to the LEDs. I replaced my ones from home with 7 X 220 ohm resistors that I got from Ashley, reset the Arduino, uploaded the code once again and this time SUCCESS!!! The LEDs light up perfectly and in the formation that was originally intended.

So my main issues for this project were: Lack of basic knowledge (this stuff is all still quite new to me), lack of correct equipment, and fiddly small components!!! (My eyes hurt so much). Other than this, I am very pleased with the final outcome.

Even if all the LEDs are green :P


Sunday, March 7, 2010

Task 32:

32) Same as 30 but also output the state of the button and the led to the serial screen. I might see on one line then: "button pressed ....led off".

if (buttonState == HIGH) {
Serial.println("button pressed ....led off");

same as before, only i'm adding a few more words.

Task 31:

31) Change the above program by reversing the the way the led goes on/off when the button is pushed/not pushed. Publish your program with a suitable comment.

This is the same as the last one, the only code that needed to be changed was the HIGH/LOW states in the if statement.

if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, LOW);
}
else {
// turn LED off:
digitalWrite(ledPin, HIGH);
}
}

Task 30:

The following is the code from the http://www.arduino.cc/en/Tutorial/Button. When the button is pressed, the pin 13 LED lights up. This could be reversed so that the light goes off when the button is pressed.

/*
Button

Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 7.


The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.


created 2005
by DojoDave
modified 17 Jun 2009
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Task 28




This code is from the http://www.ladyada.net/learn/sensors/cds.html website. Depending on the amount of light read in by the LDR, the LED will glow. If the LDR reads in very little or no light, the LED will increase in brightness.
At this stage this does not light up very much so I will need to edit it some more.

int photocellPin = 0;
int photocellReading;
int LEDpin = 11;
int LEDbrightness;
void setup(void) {

Serial.begin(9600);
}

void loop(void) {
photocellReading = analogRead(photocellPin);

Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading

photocellReading = 1023 - photocellReading;

LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness);

delay(100);
}

Task 26

Reads in the amount of light around it, outputs to the serial box how light/dark it is.

int photoCellPin = 0;
int photoCellReading;

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

void loop(void)
{
photoCellReading = analogRead(photoCellPin);

Serial.print("analog reading = ");
Serial.print(photoCellReading);

if (photoCellReading < 10)
{Serial.println(" - Very Dark");}
else
if (photoCellReading < 100)
{Serial.println(" - Dark");}
else
if (photoCellReading < 200)
{Serial.println(" - Dim");}
else
if (photoCellReading < 500)
{Serial.println(" - Light");}
else
if (photoCellReading < 600)
{Serial.println(" - Lighter");}
else
if (photoCellReading < 800)
{Serial.println(" - Bright");}
else
{Serial.println(" - Very Bright");}

delay(1000);
}

Thursday, March 4, 2010

Key words:

This section will be added to over time. It will be developed like a knowledge base for the arduino.

LED: A light-emitting diode is a semiconductor light source.

LDR: A photoresistor or light dependent resistor or cadmium sulfide (CdS) cell is a resistor whose resistance decreases with increasing incident light intensity. It can also be referenced as a photoconductor.

setup(): a function run once at the start of a program which can be used for initializing settings.

loop(): a function called repeatedly until the board is powered off.

analogRead(): Reads the value from the specified analog pin.

Serial.print(): Prints data to the serial port as human-readable ASCII text.

Serial.println(): Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). This command takes the same forms as Serial.print().

pinMode(): Configures the specified pin to behave either as an input or an output. See the description of digital pins for details.

digitalRead(): Reads the value from a specified digital pin, either HIGH or LOW.

Arduino PWM: Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.

Interrupt: A way for the programmer or an internal ATMega328 component to interrupt what the chip is currently doing.
Types: External (triggered by something happening on a line into the system (easy)) and Internally (triggered by something going on within the system itself (hard)).

Interrupt Service Routines: ISRs interrupt the normal processing of the Arduino.
Call: attachInterrupt("line", "ISR_name", "mode");

"line" is the interrup number(0,1) not the pin number(2,3).
"mode" can be LOW, CHANGE, RISING or FALLING.

other Interrupt Management procedures include:

detachInterrupt() : removes the ISR from the interrupt line, so processing will no longer occur on that line.

no Interrupts() : prevents intrerupt processing from occuring altogether.

interrupts() : cancels the noInterrupts() call. This is the default.

Volatile: Variables that are used within an ISR must be declared volatile. Volatile variables are retrieved from RAM each time to keep them consistantant.

TBC

Possible ideas for major project...

Light up clothing. - intro how to add EL wire to a coat or other garment.

This link has a "how to" of how to actually make clothes with light up sections by using El wire.

http://www.instructables.com/id/how-to-add-EL-wire-to-a-coat-or-other-garment/



LilyPad Arduino TBC

http://www.sparkfun.com/commerce/product_info.php?products_id=8617
http://arduino.cc/en/Guide/ArduinoLilyPad




Pong LED game.

This game is constructed of a board of LEDs. It replicates the effects of a pong game.

http://hacknmod.com/hack/arduino-powered-pong-on-an-led-matrix/


Some examples from tasks:

8) Write a variation of blink that has led mostly off. Just winking on quickly.

This version of task 8 has 2 other LEDs as well which all flick on for a second each.

int led13Pin = 13; // LED connected to digital pin 13
int led12Pin = 12;
int led11Pin = 11;


// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(led13Pin, OUTPUT);
pinMode(led12Pin, OUTPUT);
pinMode(led11Pin, OUTPUT);

}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(led13Pin, HIGH); // set the LED on
delay(5000);
digitalWrite(led13Pin,LOW);
delay(10); // wait for a second
digitalWrite(led12Pin, HIGH);
delay(5000);
digitalWrite(led12Pin, LOW);
delay(10);
digitalWrite(led11Pin, HIGH);
delay(5000);
digitalWrite(led11Pin, LOW);
delay(10);
}




9) Ditto but have the LED mostly on.

All that was needed to be changed for this one was in the loop(). The delay time just needed to be increased.

void loop()
{
digitalWrite(led13Pin, HIGH); // set the LED on
delay(5000);
digitalWrite(led13Pin,LOW);
delay(10); // wait for a second
}




10)Get the led to blink at rate of 10ms on 10ms off. Can you see it blinking?

Same again, but this time its flickering at a really fast rate (can't really see it on/off). The delay() has been reduced to delay it for 10ms.

digitalWrite(led13Pin, HIGH); // set the LED on
delay(10);
digitalWrite(led13Pin,LOW);
delay(10); // wait for a second




11) Check out the Blink2 program elsewhere in this blog. Run it and paste into your blog.
The following tasks are variations on Blink2.

(blink 2 is within this blog elsewhere...).



12) Make one of the LEDs blink when the other one is off.

Same as example given for task 8, except i've written it with 3LEDs.




13) Get one to blink mostly on and the other is mostly off. "Mostly on" means it's on all the time except for a little flash off about once a second.

....
void loop()
{
digitalWrite(led13Pin, HIGH); // set the LED on
delay(5000);
digitalWrite(led13Pin,LOW);
delay(10); // wait for a second
digitalWrite(led12Pin, HIGH);
delay(100);
digitalWrite(led12Pin, LOW);
delay(10);
}




14) Like 13 but the LEDs are reversed. That is, the one that mostly on is now mostly off.

....just a reverse of the above.
void loop()
{
digitalWrite(led13Pin, HIGH); // set the LED on
delay(100);
digitalWrite(led13Pin,LOW);
delay(10); // wait for a second
digitalWrite(led12Pin, HIGH);
delay(5000);
digitalWrite(led12Pin, LOW);
delay(10);
}




*There was no task 15.




16) Get one led to blink twice then the other one to blink once then repeat forever.

void loop()
{
digitalWrite(led13Pin, HIGH);
delay(1000);
digitalWrite(led13Pin,LOW);
delay(10);
digitalWrite(led13Pin, HIGH);
delay(1000);
digitalWrite(led13Pin,LOW);
delay(10); //end of first LED (Blinks twice).
digitalWrite(led12Pin, HIGH);
delay(1000);
digitalWrite(led12Pin, LOW);
delay(10);
//end of second LED. Because its in the loop, it will continue forever.
}




17) Write a for-loop with counter variable i. Get one led to blink 5 times, using the for-loop, then the other to blink once. Repeat forever.

int ledPin = 13; // LED connected to digital pin 13
int redLedPin = 12; // LED connected to digital pin 13
int del =500;


// The setup() method runs once, when the sketch starts


void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}


// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(redLedPin, HIGH); // set the LED on
delay(200);// wait for a second
digitalWrite(redLedPin, LOW);

for (int i = 0; i (less than) 5; i++)
{
digitalWrite(ledPin,HIGH);
delay(200);
digitalWrite(ledPin,LOW);
delay(200);
}


18) Like 17 but i can be any value. Try different ones. Later we can input this value from the keyboard.

Changing the "i" value is simple. i is only representing the loop that you are in. To adjust the amount of loops, we ask within the for loop "if i is less than (some number)" this is the middle statement in the for loop. To change the value to make it loop more or less time, simply change the (some number) int.
e.g:
for (int i = 0; i (less than) 5; i++) to for (int i = 0; i (less than) 10; i++)
if coded properly, the loop will now run through the code 10 times.




19) Write a program to get one led to increase its rate of blinking from 100ms to1sec.

For loop controls the increasing rate.

int ledPin = 13; // LED connected to digital pin 13
int redLedPin = 12; // LED connected to digital pin 13
int del =500;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}

void loop()
{
digitalWrite(redLedPin, HIGH); // set the LED on
delay(20);// wait for a second
digitalWrite(redLedPin, LOW);

for (int i = 0; i (less than) 1000; i++)
{ digitalWrite(ledPin,HIGH);
delay(i);
igitalWrite(ledPin,LOW);
delay(i);
}
}




20) Same as 19 but this time ramp up the rate from 10ms to 1 sec then back down again.

int ledPin = 13; // LED connected to digital pin 13
int redLedPin = 12; // LED connected to digital pin 13
int del =500;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}

void loop()
{
digitalWrite(redLedPin, HIGH); // set the LED on
delay(20);// wait for a second
digitalWrite(redLedPin, LOW);

for (int i = 0; i (less than) 100; i++)
{
digitalWrite(ledPin,HIGH);
delay(i);
digitalWrite(ledPin,LOW);
delay(i);
}
for (int i = 100; i (greater than) 0; i--)
{
digitalWrite(ledPin,HIGH);
delay(i);
digitalWrite(ledPin,LOW);
delay(i);
}
}
}




21) Write a program to output one led blinking at rate of 200ms that outputs the word "blink" to the screen each time it comes on.

void loop()
{

digitalWrite(redLedPin, HIGH); // set the LED on
delay(20);// wait for a second
digitalWrite(redLedPin, LOW);

for (int i = 0; i (less than) 100; i++)
{
digitalWrite(ledPin,HIGH);
Serial.print(" Blink! \n");
delay(i);
digitalWrite(ledPin,LOW);
delay(i);
}
for (int i = 100; i (greater than) 0; i--)
{
digitalWrite(ledPin,HIGH);
Serial.print(" Blink! ");
delay(i);
digitalWrite(ledPin,LOW);
delay(i);
}
}




22) Same as 21 but this time use two leds of different colours and output the colour of the led to the screen each time it goes on. So you should see eg. "red yellow red yellow ..."

Blinks the 2 LEDs but also does it at an increasing and decreasing rate.

void loop()
{

digitalWrite(redLedPin, HIGH);
delay(20);// wait for a second
digitalWrite(redLedPin, LOW);

for (int i = 0; i (less than) 100; i++)
{
digitalWrite(YellowLedPin,HIGH);
Serial.print(" Yellow! \n");
delay(i);
digitalWrite(YellowLedPin,LOW);
delay(i);
digitalWrite(RedLedPin,HIGH);
Serial.print(" Red! \n");
delay(i);
digitalWrite(RedLedPin,LOW);
delay(i);
}
for (int i = 100; i (greater than) 0; i--)
{
digitalWrite(YellowLedPin,HIGH);
Serial.print(" Blink! ");
delay(i);
digitalWrite(YellowLedPin,LOW);
delay(i);
digitalWrite(RedLedPin,HIGH);
Serial.print(" Red! \n");
delay(i);
digitalWrite(RedLedPin,LOW);
delay(i);
}
}









Monday, March 1, 2010

More tasks!!....

24) Go into the ladyada site and look at http://www.ladyada.net/learn/sensors/cds.html which deals with light dependent resistors. Put a link and comment about this page in your blog. Identify your LDR from your pack. You will not be tested on the technical details of this sensor but you'll be expected to insert it into circuits and read off the changing light values. - done.

25) Start a Arduino key words page in your blog. Put every new key word with an explanation and example when you come across it. It should have soon these keywords: setup(), loop(), analogRead(), Serial.print(), Serial.println(). This should be added to throughout the course. - done.

26) Run the ladayada photocell program that was handed out in class. Write it up in your blog about what you did and the program. - done.

27) Same as 26 above but make some changes to improve it. Put your changes into a comment so that it's obvious what the differences are.- done.

28) Write a program to turn a LED on whenever the light is blocked to your LED. You will have to play around with several values. - done.

29) Check out the wikiEducator site that has some example Arduino resources. Read the directions there about doing another one. Create one like it in your blog or create it in wikiEducator by registering and starting your own pages. If your resources page is not in your blog, create a heading and a link to it so that it can be marked.

30) Find your push button in amongst your kit. Check out use of external push buttons in http://www.arduino.cc/en/Tutorial/Button. Copy their program into your blog and make sure you can run it.

31) Change the above program by reversing the the way the led goes on/off when the button is pushed/not pushed. Publish your program with a suitable comment.

32) Same as 30 but also output the state of the button and the led to the serial screen. I might see on one line then: "button pressed ....led off".

33) Write up key words pinMode() and digitalRead() in your key words glossary. - done.