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.

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.
I have my breadboard set up right now correctly (confirmed with the professor.)
What is the problem with my code?

14
  • Beware this is not a discussion forum (please take the tour again), so if you can't get a straight answer to your question you will have to make an effort to narrow it down. And if you loose your login credentials or whatever again, good luck, because I won't care a second time.
    – goldilocks
    Commented Apr 28, 2016 at 1:26
  • Let me guess is it the white led that is always on? Commented Apr 28, 2016 at 19:41
  • @goldilocks I didn't lose any login credentials, I just wasn't logged in when I made the post the first time. I'm not sure why you're so angry but thanks for reopening it anyways.
    – user45649
    Commented Apr 28, 2016 at 21:06
  • @MohammadAli it occasionally turns off, I'm not sure if it was because I pressed the button or because it's doing it on its own through the code (which I am aware is incorrect.)
    – user45649
    Commented Apr 28, 2016 at 21:07
  • 2
    Uhhh... Where did the original question go? By marking an answer as accepted, you indicate that the problem was solved. Deleting the text of original question is not helpful for someone who might have a similar problem.
    – BillP3rd
    Commented May 3, 2016 at 5:45

1 Answer 1

1

The problem that you are having is that your code never does anything if the button is pushed. The current code you have provided has the first if statement turn the white led on and all other LEDs on when the button is not pressed and the second if statement if the code you have provided will turn on all the LEDs in one second intervals and the white led off while the button is not pressed. The code below should work for you as it will loop through red,green,yellow then when the button is pressed it should make the white and red LEDs turn on for 5 seconds. We use a clock because sleep is a blocking function that will prevent the button press from being detected. I'm a little rusty with my c so please copy paste any compiler errors you get and ill try and debug.

//#include <iostream>
//#include <cstdio>
//#include <ctime>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
//#include <unistd.h>
#include <wiringPi.h>

time_t startTime;
void resetTime(void){
    time(&startTime);
}
int main(void)
{
    // RED LED is pinMode 1 (18)
    // YELLOW LED is pinMode 4 (23)
    // GREEN LED is pinMode 5 (24)
    // WHITE LED is pinMode 6 (25)
    // SWITCH is pinMode 0 (17)
    resetTime();
    wiringPiSetup ();
    pinMode (1, OUTPUT);
    pinMode (4, OUTPUT);
    pinMode (5, OUTPUT);
    pinMode (6, OUTPUT);
    pinMode (0, INPUT);

    for (;;) {
        if(digitalRead(0)==LOW){
            //turn on  RED
            digitalWrite (1, HIGH);
            //turn on WHITE
            digitalWrite (6, HIGH);
            //turn off YELLOW
            digitalWrite (4, LOW);
            // turn off GREEN
            digitalWrite (5, LOW);
            // delay then turn white light off
            delay (5000);
            resetTime();
            // I USE SLEEP HERE BUT DELAY MAY ALSO WORK
            //sleep(5);
        }else{
            time_t currentTime=time(NULL);
            //set all LEDS to low
            digitalWrite (1, LOW);
            digitalWrite (4, LOW);
            digitalWrite (5, LOW);
            digitalWrite (6, LOW);
            int timePastInSec=difftime(currentTime, startTime);
            if(timePastInSec<=5){
                // if less than or equal to 5 seconds have passed turn on red
                digitalWrite (1, HIGH);
            }else if(timePastInSec<=10){
                //if less than or equal to 10 seconds have passed but more than 5 turn RED led off and turn GREEN on
                digitalWrite (1, LOW);
                digitalWrite (5, HIGH);
            }else if(timePastInSec<=12){
                //if more than 10 seconds have passed and less than or equal to 12 have passed turn off the green light and turn yellow on.
                digitalWrite (5, LOW);
                digitalWrite(4, HIGH);
            }else{
                //if over 12 seconds have passed reset the timer and restart the light sequence
                resetTime();
            }
        }
    }
    return 0;
}
23
  • The only error from the compiler says "fatal error: iostream: No such file or directory #include <iostream> compilation terminated"
    – user45649
    Commented Apr 29, 2016 at 4:37
  • Where do I type the g++ filename.cpp? Into the code? As a #include?
    – user45649
    Commented Apr 29, 2016 at 4:45
  • How do you have one light on while the others are off and have it switch back and forth at the right time?
    – user45649
    Commented Apr 29, 2016 at 21:14
  • Your going to have to be a bit more specific than that? and does the code above work for you? If so please mark it as the correct answer, by clicking the green checkmark to the left of my answer Commented Apr 29, 2016 at 23:18
  • After compiling the new code, the message reads: "In function 'main': error: 'timeInSeconds' undeclared (first use in this function) timeInSeconds = difference * 1000 / CLOCKS_PER_SEC * 1000; note: each undeclared identifier is reported only once for each function it appears in. warning: unused variable 'currentTime' clock_t currentTime = clock(); warning: unused variable walk int walk=0;
    – user45649
    Commented Apr 30, 2016 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.