Go Down

Topic: arduino college project (Read 584 times) previous topic - next topic

Craigy81

Need help with a bit of code ye Arduino geniuses. I have a pedestrian crossing system.Normal 3 traffic lights and 2 pedestrian lights. Starts off with your green traffic light on and red pedestrian on.When pedestrian button is pressed,traffic goes orange and then red while pedestrian light goes from red to green. The pedestrian is now crossing and the duration of this green light is 6 seconds. if the pedestrian reaches the other side before the 6 second duration of the light he hits another pedestrian button to signify he has reached the other side of the road and the simulation goes back to the start. HEEEEELLLLLLLLPPPPPPPP!!!!

spycatcher2k

OK - £500 and I'll do it.
Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.

Smajdalf

There is no question so how do you expect anyone to help you. Do you think people here can read thoughts to know what you need?
How to insert images: https://forum.arduino.cc/index.php?topic=519037.0

Whandall

#3
Apr 21, 2016, 08:21 pm Last Edit: Apr 21, 2016, 08:29 pm by Whandall
Take this as an example.
I start with lights off, so the first press of any button starts the lamps.

I would suggest to remove the switch to green-button, because it will be misused.
Nice trick to kill elderly people crossing slowly...

Code: [Select]
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>

const byte pinCarRed = 2;
const byte pinCarYellow = 3;
const byte pinCarGreen = 4;

const byte pinPedRed = 5;
const byte pinPedGreen = 6;

const byte pinButCross = 7;
const byte pinButClear = 8;

const unsigned int yellowTime = 2000;
const unsigned int redTime = 6000;

enum states {
  tlOff,
  tlNormal,
  tlYellow,
  tlRed,
};

byte currentState = tlOff;
unsigned long stateEntered;
unsigned long topLoop;

Bounce butCross;
Bounce butClear;

void setup() {
  pinMode(pinCarRed, OUTPUT);
  pinMode(pinCarYellow, OUTPUT);
  pinMode(pinCarGreen, OUTPUT);
  pinMode(pinPedRed, OUTPUT);
  pinMode(pinPedGreen, OUTPUT);
  butCross.attach(pinButCross, INPUT_PULLUP);
  butClear.attach(pinButClear, INPUT_PULLUP);
  Serial.begin(115200);
  Serial.println(F("Pedestrian Crossing"));
  switchLamps();
}

void loop() {
  topLoop = millis();
  butCross.update();
  butClear.update();
  switch (currentState) {
    case tlOff:
      if (butCross.fell() || butClear.fell()) {
        switchState(tlNormal);
      }
      break;
    case tlNormal:
      if (butCross.fell() || butClear.fell()) {
        switchState(tlYellow);
      }
      break;
    case tlYellow:
      if (topLoop - stateEntered > yellowTime) {
        switchState(tlRed);
      }
      break;
    case tlRed:
      if ((topLoop - stateEntered > redTime) || butClear.fell()) {
        switchState(tlNormal);
      }
      break;
  }
}

void switchState(byte newState) {
  Serial.print(F("state "));
  Serial.print(newState);
  Serial.print(F(" from "));
  Serial.print(currentState);
  Serial.print(F(" @ "));
  Serial.println(topLoop);
  currentState = newState;
  stateEntered = topLoop;
  switchLamps();
}

void switchLamps() {
  switch (currentState) {
    case tlOff:
      digitalWrite(pinCarRed, LOW);
      digitalWrite(pinCarYellow, LOW);
      digitalWrite(pinCarGreen, LOW);
      digitalWrite(pinPedRed, LOW);
      digitalWrite(pinPedGreen, LOW);
      break;
    case tlNormal:
      digitalWrite(pinCarRed, LOW);
      digitalWrite(pinCarYellow, LOW);
      digitalWrite(pinCarGreen, HIGH);
      digitalWrite(pinPedRed, HIGH);
      digitalWrite(pinPedGreen, LOW);
      break;
    case tlYellow:
      digitalWrite(pinCarRed, LOW);
      digitalWrite(pinCarYellow, HIGH);
      digitalWrite(pinCarGreen, LOW);
      digitalWrite(pinPedRed, HIGH);
      digitalWrite(pinPedGreen, LOW);
      break;
    case tlRed:
      digitalWrite(pinCarRed, HIGH);
      digitalWrite(pinCarYellow, LOW);
      digitalWrite(pinCarGreen, LOW);
      digitalWrite(pinPedRed, LOW);
      digitalWrite(pinPedGreen, HIGH);
      break;
  }
}
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

DVDdoug

#4
Apr 21, 2016, 08:40 pm Last Edit: Apr 21, 2016, 08:50 pm by DVDdoug
How far did you get, or do you need help getting started?

If you're having trouble getting started, start with some of the Examples.  If you understand the Blink Example, the Blink Without Delay Example, and the Digital Read Example, you are well on your way to making a LED/light sequence and making it respond to a button-press.

Do you have any electronics or programming experience?   

Quote
arduino college project
What's your major?   What class is this for?    ...It makes a difference in the kind of advice we'll give you.    ...It will be helpful to know if you're a 1st year electronics student, or a 4th year urban planning student with no electronics or programming experience trying to solve pedestrian traffic issues.

I assume this is your first Arduino project?

Is this "life size" with real traffic lights, or just with LEDs?

Craigy81

Sorry for bleak description guys, its my first project so not much experience. I have it working as normal no problem,but when i try add the second pedestrian crossing button it goes wrong. I am simulating this with l.e.d.s on a breadboard. I will attach what i have done so far

Whandall

No wonder, as you are trying to do all the stuff with delays, good luck with that approach.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

dave-in-nj

#7
Apr 22, 2016, 02:44 am Last Edit: Apr 22, 2016, 02:45 am by dave-in-nj
No wonder, as you are trying to do all the stuff with delays, good luck with that approach.
delays are either bad, very bad or perfect.

in any case where you do one thing at at time, think of Mrs. Finigan.
she is driving and wants to stop to get a soda.
all traffic stops, everywhere, noting moves.
she gets her soda, and all traffic everywhere resumes.
very bad.
we get around that in two ways.


if(switch== high){
    delay(1);
    delaycounter = delaycounter + 1
}
else
{
delaycounter = 0
}
if(delaycounter >= 5000){
   do that thing you do
}

see ?  the pause is so minimal that other things can go about their business.
read about the preferred way as  blinkwithoutdelay


TomGeorge

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up