Friday, April 30, 2010

Minor Project - LED Binary Clock Intentions

For my Minor Project, I intend to build the rest of the LED Binary Clock. This will mean using three shift registers to control 20 LEDs seperately. Also, if i can, i will try to program it so that i will be able to set the time on the clock using three push buttons.

Thursday, April 29, 2010

Tasks 21, 22 and 23

Task 21:

int del = 200;
int LEDPin = 13;
void setup()
{
  Serial.begin(9600);
  pinMode(LEDPin, OUTPUT);
}

void loop()
{
  digitalWrite(LEDPin, HIGH);
  Serial.println("Blink");
  delay(del);
  digitalWrite(LEDPin, LOW);
  delay(del);
}


Task: 22

int del = 200;
int LEDPin2 = 2;
int LEDPin3 = 3;
void setup()
{
  Serial.begin(9600);
  pinMode(LEDPin2, OUTPUT);     
  pinMode(LEDPin3, OUTPUT);
}

void loop()
{
  digitalWrite(LEDPin2, HIGH);  
  Serial.write("Red ");
  delay(del);                 
  digitalWrite(LEDPin2, LOW);
  delay(del);

  digitalWrite(LEDPin3, HIGH);
  Serial.print("Yellow");
  delay(del);
  digitalWrite(LEDPin3, LOW);
  delay(del);
  Serial.println();
}


Task 23:

I was unable to edit the Wikieducator page so i shall just show my version of the Arduino here.


    Arduino Nano:

 










For more information see http://www.arduino.cc/en/Main/ArduinoBoardNano.

Tasks 19 and 20

Task 19:

int LEDPin = 13;
int delvar; //This variable changes the speed of the LEDPin2 LED.
void setup()
{
  pinMode(LEDPin, OUTPUT);     
}

void loop()
{
  for (int count = 1; count < 11; count++)
  {
    delvar = (count * 100);
    digitalWrite(LEDPin, HIGH);  
    delay(delvar);                 
    digitalWrite(LEDPin, LOW);
    delay(delvar);
  } 
}


Task 20:

int LEDPin = 13;
int delvar; //This variable changes the speed of the LEDPin2 LED.
void setup()
{
  pinMode(LEDPin, OUTPUT);     
}

void loop()
{
  for (int count = 1; count < 101; count++)
  {
    delvar = (count * 10);
    digitalWrite(LEDPin, HIGH);  
    delay(delvar);                 
    digitalWrite(LEDPin, LOW);
    delay(delvar);
  } 
  for (int count = 100; count > 0; count--)
  {
    delvar = (count * 10);
    digitalWrite(LEDPin, HIGH);  
    delay(delvar);                 
    digitalWrite(LEDPin, LOW);
    delay(delvar);
  }
}

Saturday, April 17, 2010

LED Binary 60 Seconds 2.0 - refined code

I have done some research and have  found a much simpler yet more complex code to out put exactly the same pattern with seven LEDs.

Here is the new code:

const int nbr10SecBits = 3;  //10 seconds' number of pins.
const int first10SecPin = 2;  //First 10 seconds pin.

const int nbr1SecBits = 4;  //1 seconds' number of pins.
const int first1SecPin = 8;  //First 1 seconds pin.

// macros from DateTime.h
/* Useful Constants */
#define SECS_PER_MIN  (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY  (SECS_PER_HOUR * 24L)

/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN) 
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY)

void setup()
{
  Serial.begin(9600);
  for(int i=0; i < nbr10SecBits; i++)
    pinMode(i+first10SecPin, OUTPUT);
  for(int i=0; i < nbr1SecBits; i++)
    pinMode(i+first1SecPin, OUTPUT);   
}

void loop()
{
  unsigned long time = millis() / 1000;
  int tenSeconds = ((numberOfSeconds(time) - (numberOfSeconds(time) % 10))/ 10);
  int oneSeconds = numberOfSeconds(time) % 10 ;

  showBinary(tenSeconds,nbr10SecBits,first10SecPin);
  showBinary(oneSeconds,nbr1SecBits,first1SecPin);
 
  Serial.print(numberOfSeconds(time));
  Serial.print(tenSeconds);
  Serial.print(oneSeconds);
  Serial.println();
  delay(1000);
}

// this function sets the pins starting from firstPin to the binary value of the given number
void showBinary( int number, int nbrPins, int firstPin)
{
  for(int bit=0; bit < nbrPins; bit++)
  {
    boolean isBitSet =  bitRead(number, bit); // isBitSet will be true if given bit is set in this channel
    digitalWrite(bit + firstPin, isBitSet);
  } 
}

Saturday, April 10, 2010

For Loop: Task 17 and 18

This is the code i wrote for Task 17. It makes one LED blink 5 times and then another to blink once.

int del = 500;
int LEDPin2 = 2;
int LEDPin3 = 3;
void setup()
{
pinMode(LEDPin2, OUTPUT);
pinMode(LEDPin3, OUTPUT);
}

void loop()
{
for (int count = 1; count < 6; count++)
{
digitalWrite(LEDPin2, HIGH);
delay(del);
digitalWrite(LEDPin2, LOW);
delay(del);
}
digitalWrite(LEDPin3, HIGH);
delay(del);
digitalWrite(LEDPin3, LOW);
delay(del);
}


This is the code I wrote for task 18. This is like the code from 17 but there is a variable that is used to change the amount of times the first LED blinks.

int del = 500;
int LEDPin2 = 2;
int LEDPin3 = 3;
int countnum = 5; //This variable changes the amout of times the LEDPin2 LED blinks.
void setup()
{
pinMode(LEDPin2, OUTPUT);
pinMode(LEDPin3, OUTPUT);
}

void loop()
{
for (int count = 1; count < (countnum + 1); count++)
//For Loop uses the variable countnum to determine the duration of the loop.
{
digitalWrite(LEDPin2, HIGH);
delay(del);
digitalWrite(LEDPin2, LOW);
delay(del);
}
digitalWrite(LEDPin3, HIGH);
delay(del);
digitalWrite(LEDPin3, LOW);
delay(del);
}