27 May, 2010

Tasks 19-30

Tasks 19 to 30

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

int t = 100; //set the inital value of t as 100ms.
int led = 13;

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

void loop() {
digitalWrite(led, HIGH);
delay(t);
digitalWrite(led, LOW);
delay(t);
t = t + 100; //increment t by 100 each time that the code is run.
}

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

int led = 13;

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

void loop(){
for(int t = 10; t <1000; t+100){
digitalWrite(led, HIGH);
delay(t);
digitalWrite(led, LOW);
delay(t);
}

for( t= 1000; t >100; t-100){
digitalWrite(led, HIGH);
delay(t);
digitalWrite(led, LOW);
delay(t);
}
}

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.

int led = 13;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600); //set the baud rate to 9600 bits per sec
}
void setup() {
digitalWrite(led, HIGH);
Serial.println("BLINK");
delay(200);
digitalWrite(led, LOW);
delay(200);
}

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 ..."

int ledO = 13; //orange
itn ledG = 12; //green led
void setup() {
pinMode(ledO, OUTPUT);
pinMode(ledG, OUTPUT);
Serial.begin(9600); //set the baud rate to 9600 bits per sec
}
void setup() {
digitalWrite(ledO, HIGH);
Serial.println("Orange ON");
delay(200);
digitalWrite(ledO, LOW);
delay(200);
digitalWrite(ledG, HIGH);
Serial.print("Green ON);
delay(200);
digitalWrite(ledG, LOW);
delay(200;

}

23) Look at http://wwww.wikieducator.org/User:Peterb/HowManyArduinos0

In my school this website was blocked.
 But there is a kind of arduino i saw off MindKits called the Arduino MEGA which has 54 digital I/O pins.  It uses a larger version of the ATMega Microprocessor.

24) LRD photo resistors
when the light changes in a room the LDR's resistance changes.  This can be monitored using the Arduino with the analogRead() function.

25, 26 and 28)  Making the light sensor work.

To get the arduino to read the light sensor accuratly I use a 1k ohm resistor in the circut.


Here is the code that I used:

//LDRMon by Tim Sullivan

//set varyables for stuff
int ldr = 0; //inital value of the ldr
int led = 13; //use the default led on the arduino
int ldrPin = 0; //the LDR is connected to analouge pin 0

void setup() {
  pinMode(led, OUTPUT); //because LEDs usally output light...
  Serial.begin(9600); // connect to the arduino to monitor things such as 'ldr'
  //above line was just used just for debugging
}

void loop() {
ldr = analogRead(ldrPin); //read the analog pin.
// Serial.println(ldr); //tell us what the ldr is.
if(ldr > 20){
  digitalWrite(led, HIGH);
  }
 digitalWrite(led, LOW);

}

29) Random numbers on arduino

My code

int num = 0;
void setup(){
  Serial.begin(9600);
}
void loop(){
  num = random(101); //generate random numbers 0 to 100
 Serial.print(num);
}

Screenshot (my arduino was home at the [imagine random numbers in here] )


Task 30) Random dice program

/*The DICE program
by Tim Sullivan

It hasn't actually been tested with an arduino but think
it will range the correct numbers.

*/

int num = 1;
void setup(){
  Serial.begin(9600);
}
void loop(){
  num = random(1,6); //generate random numbers 1 to 6
  //i hope thats right
 Serial.print(num);
}

No comments:

Post a Comment