Thursday, March 4, 2010

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);
}
}









No comments:

Post a Comment