1

I am working on a project for my C programming class and need to use wiringPi on my raspberryPi. I need to create a traffic light (like a real one- red to green to yellow to red, etc.) and a pedestrian signal that turns on a white light and turns off the other colors when the button is pushed. This program is in c language and created on putty for raspberryPi.
I have the idea that:
While red is HIGH, yellow, green, and white are LOW.
While green is HIGH, yellow, red, and white are LOW.
While yellow is HIGH, red, green, and white are LOW.
If button is pushed, white is HIGH and red, yellow, green are LOW.
The order must go: red, green, yellow, red, green, yellow, red... Until the button is pushed and the white light turns on.
Once the white light turns off, the traffic light (red, green, yellow...) continues as usual.

I have written a code and hooked up my breadboard with my raspberryPi to mimic what it should be, or at least what I think it should be. enter image description here

enter image description here

This is my code so far:

#include <stdio.h> 
#include <stdlib.h>
#include <wiringPi.h>

int main(void)
{
    // RED is pinMode 1 (18)
    // YELLOW is pinMode 4 (23)
    // GREEN is pinMode 5 (24)
    // WHITE is pinMode 6 (25)
    // SWITCH is pineMode 0 (17)

    wiringPiSetup ();
    pinMode (1, OUTPUT);
    pinMode (4, OUTPUT);
    pinMode (5, OUTPUT);
    pinMode (6, OUTPUT);
    pinMode (0, INPUT);

    for (;;) {
        if (digitalRead(6) == LOW && digitalRead(0) == LOW) {
            digitalWrite (1, LOW);
            digitalWrite (4, LOW);
            digitalWrite (5, LOW);
            digitalWrite (6, HIGH);
            delay (2000);
        }
        if (digitalRead(0) == LOW) {
            digitalWrite (1, HIGH);
            delay (1000);
            digitalWrite (4, HIGH);
            delay (1000);
            digitalWrite (5, HIGH);
            delay (1000);
            digitalWrite (6, LOW);
            delay (1000);
        }

    }
    return 0;
}

No error message appears when I compile it (gcc -Wall -o student student.c -lwiringPi) (my project is named student.c) however it doesn't turn anything on and the button doesn't work either.
What is the problem with my code?

M.C
  • 11
  • 1
  • Can you get a single LED to light? Is the long side of the LED connected to the GPIO pin? It is hard to tell from your photos but the wiring should be GPIO to resistor to long side of LED to ground (note the resistor could be moved to after the LED and before ground. – Steve Robillard Apr 26 '16 at 23:54
  • Yeah, it looks as though the LEDs are chained in some sort of series connection - which is wrong; each LED should be wired with the series resistor on one "leg" between the GPIO pin and EITHER the 3.3V supply rail OR ground depending on whether you are driving the pin HIGH to turn the LED On or Off - however the current allowed will only be a few milli-amps for ALL of them. – SlySven Apr 27 '16 at 03:15
  • You would be better off with a NPN transistor with the base connected to the GPIO pin WITH A SERIES RESISTOR between them (can be the 10K ones you have already), the emitter connected to 0V and the LED and a 100 to 220 ohm current limiting resistor in series connected to the +5V rail (which has much more current available for these sort of thing) and to the collector. Setting the GPIO pin high will then turn the transistor on with a small base-emitter current and allow a much larger current (typically 50-500 as much) current to flow through the LED - with one of these for each LED. – SlySven Apr 27 '16 at 03:20
  • Also, particularly for the White LED the forward volt drop across it can easily be 2.9V (for a 2mA current) which when running from a 3.3V supply is going to be too big when you also have a 10K resistor in series - you ain't going to get enough current to produce much in the way of photons! – SlySven Apr 27 '16 at 03:24

0 Answers0