Show Posts
Pages: [1]
1  Using Arduino / Project Guidance / Re: Beginner Traffic light project on: February 17, 2013, 05:12:08 pm
THANK YOU, Ive been wondering why I kept getting error messages.  So I declare the functions after the loop and just reference them inside it.  I think this is what Ive been fighting the whole time.  I just ran my original code and it went through fine.  In the library now, when I get home I'll load it on the board and let you know how it worked.
2  Using Arduino / Project Guidance / Re: Beginner Traffic light project on: February 17, 2013, 02:02:07 pm
Code:
int carRed = 10; // assign the car lights
int carYellow = 9;
int carGreen = 8;
int pedRed = 11; // assign the pedestrian lights
int pedGreen =12;
int button = 2; // button pin
int crossTime = 5000; // time allowed to cross
int ledDelay = 5000;
int digitalDelay = 10000;
unsigned long changeTime; // time since button pressed



void setup() {
  pinMode(carRed, OUTPUT);
  pinMode(carYellow, OUTPUT);
  pinMode(carGreen, OUTPUT);
  pinMode(pedRed, OUTPUT);
  pinMode(pedGreen, OUTPUT);
  pinMode(button, INPUT);
}

void loop(){
 
   void greenLight(){
   digitalWrite(carGreen, HIGH);
   digitalWrite(pedRed, HIGH);
   digitalWrite(carRed, LOW);
   digitalWrite(carYellow, LOW);
   digitalWrite(pedGreen, LOW);
   delay(digitalDelay);
  }
 //===========================================
  void yellowLight(){
   digitalWrite(carYellow, HIGH);
   digitalWrite(pedRed, HIGH);
   digitalWrite(carRed, LOW);
   digitalWrite(carGreen, LOW);
   digitalWrite(pedGreen, LOW);
   delay(5000);
  }
  //==========================================
  void redLight(){
   digitalWrite(carRed, HIGH);
   digitalWrite(pedGreen, HIGH);
   digitalWrite(carGreen, LOW);
   digitalWrite(carYellow, LOW);
   digitalWrite(pedRed, LOW);
   delay(digitalDelay);
  }
}

Now Im getting an error " a function-definition is not allowed here before '{' token" highlighted on the very first void function within voidloop.  Im guessing this is because Im not allowed to have functions within functions?  I did this with c++ recently and it seemed to work.
3  Using Arduino / Project Guidance / Re: Beginner Traffic light project on: February 17, 2013, 01:47:25 pm
Just read your edit.  Im writing the functions now.
4  Using Arduino / Project Guidance / Re: Beginner Traffic light project on: February 17, 2013, 01:46:14 pm
I set all the lights to low at the beginning because I was getting a few stragllers when the code first started running but after it ran once it was fine.  Setting all the lights to LOW at the beginning fixed it.
5  Using Arduino / Project Guidance / Re: Beginner Traffic light project on: February 17, 2013, 01:44:30 pm
Alright loaded up and working.  I got this part down, including pedestrian lights with my normalOperation() code, but lets start working off your code so I can learn some thing.  The digitalWrite(pedColors', state) are for a seperate set of red and light pedestrian lights that change along with the car traffic light.  Heres a link to the two projects so you can get a better idea of what im lookinng at.  Its basically a trumped up project 4 pg.33

http://www.cs.unca.edu/bruce/Spring13/180/ASKManualRev5.pdf:

Code:
digitalWrite(carGreen, LOW);
  digitalWrite(carRed, LOW);
  digitalWrite(carYellow, LOW);
  digitalWrite(pedGreen, LOW);
  digitalWrite(pedRed, LOW);
  digitalWrite(carRed, HIGH);
digitalWrite(pedGreen, HIGH);
delay(ledDelay);
digitalWrite(pedGreen, LOW);
for (int x=0; x<10; x++) {
    digitalWrite(pedGreen, HIGH);
    delay(250);
digitalWrite(pedGreen, LOW);
    delay(250);
  }
digitalWrite(carRed, LOW);
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
delay(ledDelay);
digitalWrite(carGreen, LOW);
digitalWrite(carYellow, HIGH);
delay(3000);
digitalWrite(carYellow, LOW);
digitalWrite(pedRed, LOW);

}
note; ledDelay obviously being an int described at the top. 
Also I really like how you broke up the code with
\*=================================
6  Using Arduino / Project Guidance / Re: Recommended basic C++ Tutorial please on: February 17, 2013, 01:02:50 pm
If you go to youtube and put in "Bucky C++" youll find a link to thenewboston channel.  There you'll find tutorials on all sorts of programming taught by a guy named bucky roberts.  It can get kind of corny and VERY basic at the beginning but it is a good introduction to c++ terminology.  Im at the initial stages of learning the language myself and I find my problem to be that there is a lack of definite tutorial and project pairing out there.  I can learn all the terminology and syntax I want but if Im not given an idea on how it can be used it's hard to make it stick.  Basically just missing that "OOOOOOOOHHHH NOW I get it".
7  Using Arduino / Project Guidance / Re: Beginner Traffic light project on: February 17, 2013, 12:49:28 pm
Thank you for the response.  I must stress the fact that I am an absolute beginner to the arduino and programming in general.  So it would be best if any info posted would be in the style of explaining to a five year old.  Trust me I will not get offended, Id rather read through micellanious instruction and get the point driven home than left wondering. 

A.)  If I were to create specific banks of code, Im guessing like creating classes like c++, would I put these at the beginning of voidloop() or would they have to be in voidsetup(). or else where. 
B.)  The way Im trying to go about it is to have turning the green light on as a specific function and then based on the if statements, be turned off at different time intervals in the two separate lines of code pertaining to whether the button has been pressed or not. 

I am available to do IM or skype or a google+ hang out if any one is interested in knowing exactly how a newbie is looking at this stuff. 
8  Using Arduino / Project Guidance / Re: Beginner Traffic light project on: February 17, 2013, 12:40:38 pm
Code:
// Project 4 - Interactive Traffic Lights
int carRed = 10; // assign the car lights
int carYellow = 9;
int carGreen = 8;
int pedRed = 11; // assign the pedestrian lights
int pedGreen =12;
int button = 2; // button pin
int crossTime = 5000; // time allowed to cross
int ledDelay = 5000;
unsigned long changeTime; // time since button pressed

// declare state, volatile has to do w/interrupt
volatile int state = LOW;

void setup() {
  pinMode(carRed, OUTPUT);
  pinMode(carYellow, OUTPUT);
  pinMode(carGreen, OUTPUT);
  pinMode(pedRed, OUTPUT);
  pinMode(pedGreen, OUTPUT);
  pinMode(button, INPUT); // button on pin 2
  // turn on the green light
  digitalWrite(carGreen, HIGH);
  digitalWrite(pedRed, HIGH);
 
/* RISING cause when ur button is pressed it changes from LOW to HIGH to LOW, so rly any of the commands wld do
but if you had a sit like flipping a switch then you wld have to choose the correct one */
  attachInterrupt(0, buttonPressed, RISING);
}

void loop() {
 
 
 if (state == LOW){
   normalOperation();}
 
  /* check if button is pressed and if it is
  over 5 seconds since last button press */
  /* Added another 2 brackets around the second part cause the        >5000 part wasnt inside em */
if (state == HIGH) {
    // Call the function to change the lights
    changeLights();
  }
 

void normalOperation(){
  delay(10000);
digitalWrite(carGreen, LOW);
  digitalWrite(carRed, LOW);
  digitalWrite(carYellow, LOW);
  digitalWrite(pedGreen, LOW);
  digitalWrite(pedRed, LOW);
  digitalWrite(carRed, HIGH);
digitalWrite(pedGreen, HIGH);
delay(ledDelay);
digitalWrite(pedGreen, LOW);
for (int x=0; x<10; x++) {
    digitalWrite(pedGreen, HIGH);
    delay(250);
digitalWrite(pedGreen, LOW);
    delay(250);
  }
digitalWrite(carRed, LOW);
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
delay(ledDelay);
digitalWrite(carGreen, LOW);
digitalWrite(carYellow, HIGH);
delay(3000);
digitalWrite(carYellow, LOW);
digitalWrite(pedRed, LOW);

}
void changeLights() { 
  delay(5000);
  digitalWrite(carGreen, LOW);
  digitalWrite(carRed, HIGH); // red on
  delay(1000); // wait 1 second till its safe
digitalWrite(pedRed, LOW); // ped red off
  digitalWrite(pedGreen, HIGH); // ped green on
  delay(crossTime); // wait for preset time period
  // flash the ped green
for (int x=0; x<10; x++) {
    digitalWrite(pedGreen, HIGH);
    delay(250);
digitalWrite(pedGreen, LOW);
    delay(250);
  }
  // turn ped red on
  digitalWrite(pedRed, HIGH);
  delay(500);
digitalWrite(carYellow, HIGH); // yellow on
  digitalWrite(carRed, LOW); // red off
  delay(1000);
digitalWrite(carGreen, HIGH);
  digitalWrite(carYellow, LOW); // yellow off
// record the time since last change of lights
  changeTime = millis();
  // then return to the main program loop

/* return state to low after this loop so that the normal operation will run again and someone cant just keep hitting the button and stay in pedestrian mode. */
state = LOW;
}

//**** Added function to change state to high w/buttonpress
void buttonPressed(){
   state = HIGH;}
9  Using Arduino / Project Guidance / Beginner Traffic light project on: February 16, 2013, 03:16:38 pm
Hey every one,
Im new to the arduino and I was given two separate assignments where I pulled some code out of a book, they were both successful now Im trying to create an actual fully functional traffic light.  The gist is I want a traffic light to run under a certain loop constantly but then when a button is pressed I want it to give the green light a set amount of time before turning on a pedestrian light.  Say in the normal loop the light is green for 15s and I press the button at 5s, have the light stay on another 5s before changing to red to allow for a pedestrian. run this loop once then move back to the normal light.  Im using an interrupt function (which Im still wrapping my head around) and you might be able to see what I had in mind, or I might have just created a hack job.  Ill paste my code below and any other hints to help me expedite use of this forum would be welcome with much appreciation.

Thank you,


// Project 4 - Interactive Traffic Lights
int carRed = 10; // assign the car lights
int carYellow = 9;
int carGreen = 8;
int pedRed = 11; // assign the pedestrian lights
int pedGreen =12;
int button = 2; // button pin
int crossTime = 5000; // time allowed to cross
int ledDelay = 5000;
unsigned long changeTime; // time since button pressed

// declare state, volatile has to do w/interrupt
volatile int state = LOW;

void setup() {
  pinMode(carRed, OUTPUT);
  pinMode(carYellow, OUTPUT);
  pinMode(carGreen, OUTPUT);
  pinMode(pedRed, OUTPUT);
  pinMode(pedGreen, OUTPUT);
  pinMode(button, INPUT); // button on pin 2
  // turn on the green light
  digitalWrite(carGreen, HIGH);
  digitalWrite(pedRed, HIGH);
 
/* RISING cause when ur button is pressed it changes from LOW to HIGH to LOW, so rly any of the commands wld do
but if you had a sit like flipping a switch then you wld have to choose the correct one */
  attachInterrupt(0, buttonPressed, RISING);
}

void loop() {
 
 
 if (state == LOW){
   normalOperation();}
 
  /* check if button is pressed and if it is
  over 5 seconds since last button press */
  /* Added another 2 brackets around the second part cause the        >5000 part wasnt inside em */
if (state == HIGH) {
    // Call the function to change the lights
    changeLights();
  }
 

void normalOperation(){
  delay(10000);
digitalWrite(carGreen, LOW);
  digitalWrite(carRed, LOW);
  digitalWrite(carYellow, LOW);
  digitalWrite(pedGreen, LOW);
  digitalWrite(pedRed, LOW);
  digitalWrite(carRed, HIGH);
digitalWrite(pedGreen, HIGH);
delay(ledDelay);
digitalWrite(pedGreen, LOW);
for (int x=0; x<10; x++) {
    digitalWrite(pedGreen, HIGH);
    delay(250);
digitalWrite(pedGreen, LOW);
    delay(250);
  }
digitalWrite(carRed, LOW);
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
delay(ledDelay);
digitalWrite(carGreen, LOW);
digitalWrite(carYellow, HIGH);
delay(3000);
digitalWrite(carYellow, LOW);
digitalWrite(pedRed, LOW);

}
void changeLights() { 
  delay(5000);
  digitalWrite(carGreen, LOW);
  digitalWrite(carRed, HIGH); // red on
  delay(1000); // wait 1 second till its safe
digitalWrite(pedRed, LOW); // ped red off
  digitalWrite(pedGreen, HIGH); // ped green on
  delay(crossTime); // wait for preset time period
  // flash the ped green
for (int x=0; x<10; x++) {
    digitalWrite(pedGreen, HIGH);
    delay(250);
digitalWrite(pedGreen, LOW);
    delay(250);
  }
  // turn ped red on
  digitalWrite(pedRed, HIGH);
  delay(500);
digitalWrite(carYellow, HIGH); // yellow on
  digitalWrite(carRed, LOW); // red off
  delay(1000);
digitalWrite(carGreen, HIGH);
  digitalWrite(carYellow, LOW); // yellow off
// record the time since last change of lights
  changeTime = millis();
  // then return to the main program loop

/* return state to low after this loop so that the normal operation will run again and someone cant just keep hitting the button and stay in pedestrian mode. */
state = LOW;
}

//**** Added function to change state to high w/buttonpress
void buttonPressed(){
   state = HIGH;}
10  Using Arduino / Installation & Troubleshooting / Re: Windows 7 Unknown Device FIXED on: January 16, 2013, 06:07:25 pm
worked on 32 bit as well thank you.
Pages: [1]