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?

Aurora0001
  • 6,308
  • 3
  • 23
  • 38
  • 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 Apr 28 '16 at 01:26
  • Let me guess is it the white led that is always on? – Mohammad Ali Apr 28 '16 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. –  Apr 28 '16 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.) –  Apr 28 '16 at 21:07
  • You can't post anything without being logged in (the other post has a very similar username attached too), but the SE cookies have a long duration, so you may have remained logged in from days before. Or something. SE's whole login is a bit confusing IMO. I'm not angry, I'm just trying to make sure you slow down and pay attention ;) – goldilocks Apr 28 '16 at 21:10
  • So running what you have which LEDs turn on, and what helps pens when you press the button? Because what I don't understand is why you do not have an if statement for 'digitalRead(0)==HIGH' so like when the button is pressed something will happen. And your first if statement is completely useless, if you remove it, it should just flash the LEDS 1 by 1 every second. See if that works then get back to me and we can take it from there. – Mohammad Ali Apr 28 '16 at 21:12
  • @MohammadAli I changed the digitalRead(0) == HIGH and removed the first if statement. As of now, the red, yellow, and green light are constantly on. I cancelled the program running and the lights remained on (I needed to use gpio write 0 to turn them off individually.) I'm not sure why the button is not making a difference. –  Apr 28 '16 at 21:27
  • I think I know your problem, I'll get back to you when I get home in a half hour or so with an answer – Mohammad Ali Apr 28 '16 at 21:29
  • Thank you. I know my code as of now does not perform the functions of a traffic light like I'm trying to accomplish, but I think once I can get my button to work then I will have an easier time working the other lights. –  Apr 28 '16 at 21:30
  • when the button is pushed i know you want to turn the white light on, but do you also want to turn the red light on and for how long? – Mohammad Ali Apr 28 '16 at 21:33
  • So my idea is once the user tells the program to run, the led traffic light will begin going (red...green...yellow...red...etc.) If the user pushes the button, the traffic light goes to red and the white light is on. The red stays on the same duration of time as the white light. Once the white led turns off, the traffic light resumes and changes from white on to white off and red off to green on. –  Apr 28 '16 at 21:39
  • But how long does this white light stay on for? – Mohammad Ali Apr 28 '16 at 21:45
  • I would like to have the white LED to stay on for 5 seconds (5000 ms). Red LED on for 5 seconds, green on for 5 seconds, yellow on for 2 seconds. –  Apr 28 '16 at 22:03
  • 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 May 03 '16 at 05:45

1 Answers1

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;
}
Mohammad Ali
  • 2,383
  • 1
  • 11
  • 18
  • The only error from the compiler says "fatal error: iostream: No such file or directory #include compilation terminated" –  Apr 29 '16 at 04:37
  • Where do I type the g++ filename.cpp? Into the code? As a #include? –  Apr 29 '16 at 04:45
  • How do you have one light on while the others are off and have it switch back and forth at the right time? –  Apr 29 '16 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 – Mohammad Ali Apr 29 '16 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; –  Apr 30 '16 at 19:41
  • When I run it anyways, with the compiling errors, it runs by turning on red then yellow then green and they all stay on. Nothing happens when I press the button. –  Apr 30 '16 at 19:45
  • I fixed walk and make time in seconds an integer, please tell me what error you get now – Mohammad Ali Apr 30 '16 at 20:14
  • error: unknown type name 'Int' Int timeInSeconds = difference * 1000 / CLOCKS_PER_SEC *1000; warning: unused variable 'currentTime' clock_t currentTime = clock(); –  Apr 30 '16 at 20:21
  • Try int without a capital "I" my phones autocorrect thinks that the first letter of every line should be a capital, sorry about that. – Mohammad Ali Apr 30 '16 at 20:23
  • Changed that. Now when I run it the white light and red light are on automatically and stay turned on. When I press and hold the button, the white light turns off and the red and yellow light are on. But this is only while I'm holding down the button, if I just press it once then the white light and red light remains on. –  Apr 30 '16 at 20:43
  • I fixed the red yellow problem, it should work now, if it doesn't please post a video of it running for 20 seconds then you holding it down for 20 seconds. It will make debugging much easier – Mohammad Ali Apr 30 '16 at 23:56
  • No change happened. Here's two photos: http://imgur.com/ZpOmQdk http://imgur.com/AeGfzpM –  May 01 '16 at 19:32
  • I noticed my clock problem that was always setting the timer to zero and have made changes to correct that, i have also compiled the new code on my own linux box so it should be ok. But just in case it doesn't the next time you compile with gcc compile with the parameter '-Wall' gcc will give you extra warnings. Example: gcc -Wall myfile.c -o myfileOut – Mohammad Ali May 01 '16 at 20:58
  • I always compile using -Wall. When I compiled it it said "warning: implicit declaration of function 'sleep'. sleep(5)". When I run it, the program starts with the red and white on. When I hold down the button it starts the traffic light, going red to green to yellow. When I let go of the button it turns on red and white. This is so close but I need the traffic light to start first, then when the button is pressed once but not help down, the pedestrian light works. Then once it turns off, the traffic light resumes. –  May 01 '16 at 22:46
  • try setting the "HIGH" in 'if (digitalRead(0) == HIGH)' to LOW and also try changing the line that says 'sleep(5)' to delay(5); and tell me if that works. – Mohammad Ali May 02 '16 at 00:03
  • Never mind I made the changes for you, please just reupload, compile and try again – Mohammad Ali May 02 '16 at 00:06
  • This code is almost perfect. Wow, you are great. But can you help me with this: the code compiled without error and the button works. However, if I press the button while it was green, it changes to the white light and red light on, but once the white turns off, it resumed in the place where the green left off. So when the white turned off, the yellow turned on and then back to red and then green and the normal traffic light flow. Instead of if I press on green, white on, and once white is off then it goes back to red to green to yellow. Do you know why that is? –  May 02 '16 at 01:46
  • yeah i think i do ill make an edit in like 15 minutes, if you don't mind it would be nice of you if you could vote this up so i can that extra point of reputation – Mohammad Ali May 02 '16 at 01:49
  • All that was needed was one more function call to "resetTime()" inside of the if statement to have the loop restart after the white light turns off – Mohammad Ali May 02 '16 at 01:52
  • Thank you. I will try it soon and let you know. I tried to give you a vote up but unfortunately I don't have enough reputation to do so, I'm sorry. –  May 02 '16 at 02:11
  • haha, well someone else did like this up, and its ok, ill be waiting here in suspense for you to get back to me – Mohammad Ali May 02 '16 at 02:12
  • It works! I am beyond grateful for all your help and advice through this. –  May 02 '16 at 04:03
  • not a problem, best of luck with your project and i hope you reach your goal of becoming a computer engineer one day. On a side note: thats some nice yellow nail polish you were rocking in that second image. – Mohammad Ali May 02 '16 at 04:06