Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- iammuhammed
- Date:
- 2021-12-28
- Revision:
- 4:0da8deb04107
- Parent:
- 3:21d083a76ac4
File content as of revision 4:0da8deb04107:
#include "mbed.h" DigitalOut PIN_RED(D8); DigitalOut PIN_AMBER(D9); DigitalOut PIN_GREEN(D10); DigitalIn p1(D0); // representing sensor for people DigitalIn p2(D1); DigitalIn p3(D2); DigitalIn p4(D3); DigitalIn c1(D4); // representing sensor for cars DigitalIn c2(D5); DigitalIn c3(D6); DigitalIn c4(D7); // lights onboard for pedestrian DigitalOut ledR(LED_RED); DigitalOut ledG(LED_GREEN); // initiliasing variables int count_p ; int count_c; Timer t; bool pedestrian = false; void Count(){ count_p = 0; count_c = 0; if(p1 == 1){ count_p = count_p + 1; if(t.read() == 0){ t.start(); } } if(p2 == 1){ count_p = count_p + 1; if(t.read() == 0){ t.start(); } } if(p3 == 1){ count_p = count_p + 1; if(t.read() == 0){ t.start(); } } if(p4 == 1){ count_p = count_p + 1; if(t.read() == 0){ t.start(); } } if(c1 == 1){ count_c = count_c + 1; } if(c2 == 1){ count_c = count_c + 1; } if(c3 == 1){ count_c = count_c + 1; } if(c4 == 1){ count_c = count_c + 1; } } void Red(){ PIN_RED = 1; PIN_AMBER = 0; PIN_GREEN = 0; }; void Red_Amber(){ PIN_RED = 1; PIN_AMBER = 1; PIN_GREEN = 0; }; void Amber(){ PIN_RED = 0; PIN_AMBER = 1; PIN_GREEN = 0; } void Green(){ PIN_RED = 0; PIN_AMBER = 0; PIN_GREEN = 1; } void Stop(){ // when our changetraffic light function finishes at is green.. Amber(); wait(3); // turn off amber, then turn red on for 5 seconds Red(); } void Start(){ } void changeLights(){ if(pedestrian == true){ // red and amber on for 2 seconds ( red is already on though) Red_Amber(); wait(2); // turn off red and amber, then turn on green Green(); wait(3); pedestrian = false; } else { // green off, Amber on for 3 seconds Amber(); wait(3); // turn off amber, then turn red on for 5 seconds Red(); wait(5); // red and amber on for 2 seconds ( red is already on though) Red_Amber(); wait(2); // turn off red and amber, then turn on green Green(); wait(3); } } void P_Green(){ ledR = 1; ledG = 0; } void P_Red(){ ledR = 0; ledG = 1; } //void Fault(){ // // Amber(); // P_Red(); // // while(true){ // // // wait(0.5); // PIN_AMBER = 0; // ledR = 1; // wait(0.5); // PIN_AMBER = 1; // ledR = 0; // // } // //} int main() { ledR = 0; ledG = 1; Green(); wait(3); while (true) { pedestrian = false; // checks for count every 5 seconds wait(5); Count(); if (count_p > count_c){ Stop(); P_Green(); wait(20); P_Red(); t.reset(); //check variable for our changelight function pedestrian = true; } else if (count_p > 0 & t.read() > 60){ Stop(); P_Green(); wait(20); P_Red(); t.reset(); //check variable for our changelight function pedestrian = true; } changeLights(); } }