Sunday, May 30, 2010

Tasks 54 - 60

54) Data logging. Write a small program that reads a real world signal like light or temperature and takes 10 samples over ten seconds. Output your values to the serial screen.

int photoCellPin = 0;
int photoCellReading;

int i;


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

photoCellReading = analogRead(photoCellPin);


for(i=0;i less than 10;i++)
{
Serial.print("analog reading = ");
Serial.print(photoCellReading);


if (photoCellReading less than 10)
{Serial.println(" - Dark");}
else
if (photoCellReading less than 200)
{Serial.println(" - Dim");}
else
if (photoCellReading less than 500)
{Serial.println(" - Light");}
else
if (photoCellReading less than 800)
{Serial.println(" - Bright");}
else
{Serial.println(" - Very Bright");}

Serial.println(i);
delay(1000);

}
}

void loop(void)
{
}



55) Same but this time store your values in an array called mySamples[] and output the array on completion of the tenth sample.

int photoCellPin = 0;
int photoCellReading;

int i;
int x;

int mySamples[10];

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

for(i=0; i less than 10; i++)
{

photoCellReading = analogRead(photoCellPin);

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

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

delay(1000);

mySamples[i] = photoCellReading;
}

for(x=0;x less than 10;x++)
{
Serial.println(mySamples[x]);
}

}

void loop(void)
{}



56) Same as 55 but this time store your values into eeprom then output from there after 5 second delay.


57) Same as 56 but this time write a separate function to find the smallest value and out put that at the end of the sampling with a suitable message.


58) Same as 57 but this time write two functions average() and biggest(). The average function outputs the mean of the values as a floating point number and the other function, biggest(), will output the greatest value that you have sampled.


--Next tasks are to do with longer term data logging over a day or longer.


59) Create an experiment that will log a real-world variable like light or temperature over 24 hours or more. Store your logged values in eeprom and display your values in a useful way. Write up your experiment with photos and movies in your blog.


60) Make a presentation to the class with at least three slides on one of the function blocks of the mega328. This is a diagram in the blog with the heading "the main parts of the mega 328".

//not enough time left in the semester - asked to flag as it will be included in the test.

Sunday, May 23, 2010

Minor Project



My minor project is the basic layout that my major will have. It consists of two push buttons, each with their own corresponding set of LED's that "flash" when the button is pressed. This will eventually be migrated to the Lilypad Arduino so that it can sewn onto the back of a jacket to represent indicators.

At this stage I have only just received the Lilypad Arduino so will not be using it for the minor.

The major projects code will have a much stronger structure. The code for this is very basic but it represents how the major project will function.

Code:

const int leftButton = 7;
const int rightButton = 8;

const int leftLED = 11;
const int rightLED = 12;

boolean leftBool;
boolean rightBool;

int x;

int buttonStateL = 0; // variable for reading the pushbutton status
int buttonStateR = 0;


void setup() {
// initialize the LED pin as an output:
pinMode(leftLED, OUTPUT);
pinMode(rightLED, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(rightButton, INPUT);
pinMode(leftButton, INPUT);

leftBool = false;
rightBool = false;
}

void loop(){
// read the state of the pushbutton value:

//right
buttonStateR = digitalRead(rightButton);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonStateR == HIGH) {
rightBool = true;
}

if (rightBool == true)
{
// turn LED on:
for (x=0;x less than 10;x++)
{
digitalWrite(rightLED, HIGH);
delay(500);
digitalWrite(rightLED, LOW);
delay(500);
}
rightBool = false;
}



//left buttonStateL
buttonStateL = digitalRead(leftButton);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonStateL == HIGH)
{
leftBool = true;
}

if (leftBool == true)
{
// turn LED on:
for (x=0;x less than 10;x++)
{
digitalWrite(leftLED, HIGH);
delay(500);
digitalWrite(leftLED, LOW);
delay(500);
}
leftBool = false;

}
}



Fritzing Diagram:
Note: from first attempt.



Photos:


Video:

Thursday, May 6, 2010

Tasks 51 - 53

51) Check out the eeprom read program at http://arduino.cc/en/Reference/EEPROMRead
Get it running and put a screen dump into your blog. They may be all $FFs or all $00's at first.

Note: EEPROM.write(address, value);
#include EEPROM.h

int a = 0;
int value;

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

void loop()
{
value = EEPROM.read(a);

Serial.print(a);
Serial.print("\t");
Serial.print(value);
Serial.println();

a = a + 1;

if (a == 512)
a = 0;

delay(500);
}


My first attempt printed this out to the serial monitor:


However after I ran the eeprom write program and went back to try the read program again it printed out this instead:
This was because it had reset the values through the for loop.

52) Now run the similar eeprom write program to send $23 to address 01 and $2A to address 02. Add a eeprom dump, all 512 bytes, function to your program based on the eeprom read program above so you can see that your bytes have been changed.

//Notes:
EEPROM.write(address, value);
#include EEPROM.h

void setup()
{
//for (int i = 0; i less than 512; i++)
// EEPROM.write(i, i);

instead of the above for loop:
EEPROM.write(01, 23);
EEPROM.write(02, 24);

This will write the values 23 and 24 to the address of 01 and 02.

}

void loop()
{
}



53) Fill the total eeprom with $AB's all 1024 bytes. Read eeprom space back onto your screen and do a screen capture picture for your blog.

Tasks 45 - 50

45)Explore the way your infrared transmitter sends pulses to the receiver. Set up a little demonstration to be marked in class. You may share this project with another person but both members have to know what's going on. Transmission from one Arduino to another is especially excellent.

Had done this - just completely forgot to upload the photo and describe it.

For this task I decided to use the blink program. This would light up the sending IR led so to make a connection, the receiver would have to be in line with it. Because we cannot see infrared light, I added an LED which was set up to light up when there was a connection between the 2 IR's.
The transmitting IR is placed at pin 13 and ground, with the concave side facing the breadboard.
There are also 2 wires connected at ground and 3v pins on the power side of the arduino board. The 3v is set inline with the IR receiver and ground with the LED, the IR and LED are placed so that they link up.
When run with the blink sketch, the LED on the breadboard will light up. If something interrupts the signal, the LED goes off.

//insert photo here..


46) Get a plan to send a signal from one Arduino to another. Set one up to receive and one to transmit. Connect the input pin of the receiver to an output pin of the transmitter. Get the receiver to note on the serial screen what it's reading. Get the transmitter to send a slow square wave that can be received and displayed in real human time.


Plan: One arduino has the blink sketch running which also has TxPin = 2 (transmitter), as an output to the other arduino. When the ledPin is HIGH the TxPin is HIGH also. This is then connected to the other arduinos RxPin = 2 (reciever) and the grounds of both arduinos are connected. The recieving arduino is either expecting something or nothing (0 or 1). If the RxPin is equal to 1, it means the led on the other arduino is HIGH so the recieving arduinos led is set to HIGH and outputs a H to the serial monitor.


Transmitter:

int ledPin = 13;
int TxPin = 2;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(TxPin, OUTPUT);
}

void loop()
{
digitalWrite(TxPin, HIGH);
digitalWrite(ledPin, HIGH);
delay(1000);

digitalWrite(TxPin, LOW);
digitalWrite(ledPin, LOW);
delay(1000);
}


Reciever:

int RxPin = 2;
int ledPin = 13;

void setup()
{
pinMode(RxPin, INPUT);
Serial.begin(9600);
}

void loop()
{
if (digitalRead(RxPin)==1)
{
Serial.println("H");
digitalWrite(ledPin,HIGH);
}
else
{
digitalWrite(ledPin,LOW);
Serial.println("L");
}
}


47) Same as 46 but this time reverse roles of sender and receiver.

..cut, paste, done :)

48) Same as 46 but get the receiving Arduino to send the level, 1 or 0, that it's getting back to the other Arduino. So it's just receiving and sending a copy back for checking. Get both Arduinos to display what they are getting. A one second on/off will work ok.

This time I tryed getting both the arduinos to send and receive at the same time. This was my code:

int RxPin = 2;
int TxPin = 3;
int ledPin = 13;

void setup()
{
pinMode(RxPin, INPUT);
pinMode(TxPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
//sending
digitalWrite(TxPin, HIGH);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(TxPin, LOW);
digitalWrite(ledPin, LOW);
delay(1000);
//receiving
if (digitalRead(RxPin)==1)
{
Serial.println("H");
digitalWrite(ledPin,HIGH);
}
else
{
digitalWrite(ledPin,LOW);
Serial.println("L");
}
}

This was on both arduinos (Rx and Tx pin numbers swapped), but it didnt seem to work properly, one would only output low and the other only high so im not sure if this was working correctly.



49) Get one Arduino to send a given series of pulses, say about a 100, about 1ms long, to the other. Get the receiver to count the number of pulses it gets and display this number on the serial screen.

//in the setup so it doesnt loop once its done.

while (count less than 100)
{
digitalWrite(TxPin, HIGH);
digitalWrite(ledPin, HIGH); // set the LED on
delay(500); // wait for a second
digitalWrite(TxPin, LOW);
digitalWrite(ledPin, LOW); // set the LED off
delay(500);
count++;
}


50) How fast can you send a thousand 1/0 pulses and still count them accurately.
I reduced the delay times on both the sender and receiver but it still printed out, not all at once though.