Sunday, July 31, 2011

Connecting a seven-segment LED to Arduino UNO

There are a few tutorials that sadly, I am unable to perform due to the lack of components in my starter kit. I am trying to hunt down a thermistor and a servo motor. Before those 2 are obtained, I wanted to fully exploit the current components first. So in this post I connect a seven segment LED to the Arduino UNO board.

The instructions and codes were from hacktronics.com.

The picture of the circuit:

IMG_20110731_175044

Good thing the kit had enough resistors. The resistors used were 330k Ohm ones.

LED Bounce Off Centre effect on Arduino UNO

One of the additional exercises in the manual was to add codes so that the LED blinkings appear to bounce off each other in the middle. The original project (Project 6) was to make an LED chase effect where the LEDs blink one at a time in sequence from one end to the other, and back the opposite direction.

The picture of circuit:

IMG_20110731_000433

The code in its entirety:

byte ledPin[] = {7, 8, 9, 10, 11, 12, 13};
int ledDelay;
int Direction1 = 1;
int Direction2 = -1;
int currentLED = 0;
int lastLED = 6;
unsigned long changeTime;
int x;
int potPin = 2;

void setup(){
  for(int x=0; x<7; x++){
    pinMode(ledPin[x], OUTPUT);
    pinMode(ledPin[6-x], OUTPUT); }
    changeTime = millis();
}

void loop(){
  ledDelay = analogRead(potPin);
  if ((millis() - changeTime) > ledDelay) {
    changeLED();
    changeTime = millis();
  }
}

void changeLED(){
  for (int x=0; x<7; x++){
    digitalWrite(ledPin[x], LOW);
    digitalWrite(ledPin[6-x], LOW);
  }
  digitalWrite(ledPin[currentLED], HIGH);
  digitalWrite(ledPin[lastLED], HIGH);
  currentLED += Direction1;
  lastLED +=Direction2;
  if (currentLED == 3){Direction1 = -1;}
  if (currentLED == 0) {Direction1 = 1;}
  if (lastLED == 6){Direction2 = -1;}
  if (lastLED == 3){Direction2 = 1;}
}

Basically what I did was I added another integer variable, lastLED and made it a 2nd output, since there will be 2 separate blinkings at one time. I duplicate whatever the code there is for currentLED for lastLED. Also added a second direction variable so that the new blinking can have an opposite direction to the first blinking. Made them start at different ends but end in the middle. I maintained the potentiometer-controlled ledDelay variable.

Not too bad for a total noob eh?

Saturday, July 30, 2011

Pedestrian Interactive Traffic Light using Arduino UNO

This is a pedestrian interactive traffic light made using Arduino UNO. This is project 4 from the manual that can be found at this link.

Photo of circuit:

IMG_20110730_142344

The code is as follows:

int carRed = 12;
int carYellow = 11;
int carGreen = 10;
int pedRed = 9;
int pedGreen = 8;
int button = 2;
int crossTime = 5000;
unsigned long changeTime;

int x;

void setup(){
  pinMode(carRed, OUTPUT);
  pinMode(carYellow, OUTPUT);
  pinMode(carGreen, OUTPUT);
  pinMode(pedRed, OUTPUT);
  pinMode(pedGreen, OUTPUT);
  pinMode(button, INPUT);
  digitalWrite(carGreen, HIGH);
  digitalWrite(pedRed, HIGH);

}

void loop(){
  int state = digitalRead(button);
  if (state == HIGH && (millis() - changeTime) > 5000){
    changeLights();
  }
}
void changeLights(){

  digitalWrite(carGreen, LOW);
  digitalWrite(carYellow, HIGH);
  delay(2000);
  digitalWrite(carYellow, LOW);
  digitalWrite(carRed, HIGH);
  delay(1000);
  digitalWrite(pedRed, LOW);
  digitalWrite(pedGreen, HIGH);
  delay(crossTime);
  for(int x=0; x<10; x++){
    digitalWrite(pedGreen, HIGH);
    delay(250);
    digitalWrite(pedGreen, LOW);
    delay(250);

  digitalWrite(pedRed, HIGH);
  delay(500);
  digitalWrite(carYellow, HIGH);
  digitalWrite(carRed, LOW);
  delay(1000);
  digitalWrite(carGreen, HIGH);
  digitalWrite(carYellow, LOW);
  changeTime = millis();

}

In human language, the following describes what the traffic light was doing:

1. Traffic light waits for user input. The time since the circuit was turned on was continuously logged.
2. If button is pushed, and the last light change was more than 5 seconds ago (determined by subtracting the time recorded at end of last light change and time since circuit was turned on), the yellow light turns on for 2 seconds.
3. Then the red light turns on.
4. After 1 second, the green light for pedestrian turns on for 10 seconds.
5. Pedestrian green light starts blinking 10 times after that.
6. Pedestrian red light turns on.
7. After 0.5 seconds, yellow lights for cars turn on for 1 second.
8. Yellow and red lights for cars turn off, and green light turns on.
9. Traffic light listens for user input. Time since circuit was turned on was recorded at this point.

Traffic Light using Arduino UNO

This is the first proper things I did with the Arduino UNO. The blinking LEDs one don’t really count, do they?

Anyway, this is Project 3 from the tutorial at this link.

The circuit is as follows:

IMG_20110730_131549

Proper schematic can be found at the manual linked earlier.

And the code is as follows:

int ledDelay = 10000;
int redPin = 10;
int yellowPin = 8;
int greenPin = 9;

void setup(){
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}

void loop(){
digitalWrite(redPin, HIGH);
delay(ledDelay);
digitalWrite(yellowPin, HIGH);
delay(2000);
digitalWrite(greenPin, HIGH);
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
delay(ledDelay);
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, LOW);
delay(2000);
digitalWrite(yellowPin, LOW);
}


What it was doing is described below:

1. The red light will be on for 10 secs, then remains on while the yellow light turn on for 2 seconds.

2. After that both lights will turn off and the green light turns on for 10 second.

3. After that the green light remains on while the yellow light turns on for 2 seconds.

4. Loop repeats.

Arduino!

 

IMG_20110729_154844

K so I’ve just gotten myself an Arduino UNO starter kit from myduino.com. The item that I bought can be found in this link. Downloaded the document from the same page and will be learning how to do shits with an Arduino board. I wish to eventually develop accessories using the Android ADK, so let’s hope I persevere till then.