Im gunna be honest, Im not even sure what language this is. I JUST started trying to learn how to code a few hours ago. Im just trying to get help because I really want to learn how to program micro controllers and such. ANYWAYS, here is my problem:
I got an ARDUINO Uno to help teach myself how to code for the first time by messing with other peoples code. Everything works fine except one thing, Im basically making a stoplight with a motion sensor. I got it so when a vehicle is detected by the sensor it will wait 5 seconds then turn green, the problem occurs when a vehicle leaves, It turns the green off and the yellow on, but when it goes to turn the yellow off and the red on it doesn't turn the yellow off.... So basically the red and yellow light stay on at the end of the cycle...
/*
Button
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <>http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to
// set pin numbers:
constint buttonPin = 8; // the number of the pushbutton pin
constint ledrPin = 13; // Red Pin
constint ledyPin = 12; // yellow Pin
constint ledgPin = 11; // Green pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledrPin, OUTPUT);
pinMode(ledyPin, OUTPUT);
pinMode(ledgPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn Green LED on:
delay(5000);
digitalWrite(ledrPin, LOW);
digitalWrite(ledyPin, LOW);
digitalWrite(ledgPin, HIGH);
}
else {
// turn Red LED on:
digitalWrite(ledgPin, LOW);
digitalWrite(ledyPin, HIGH);
delay(5000);
digitalWrite(ledyPin, LOW);
digitalWrite(ledrPin, HIGH);
}}
If you guys cant help me, would you be so kind as to point me towards where I can ask for help?
"I JUST started trying to learn how to code a few hours ago."
InfamousXTC wrote:
"Everything works fine except one thing, Im basically making a stoplight with a motion sensor."
I'm confused. You're new to programming -- literally by a few hours -- and you're attempting to develop a motion sensor program? Wouldn't it be better to learn the basics first?
Thats 100% correct haha, this is how I taught myself HTML and CSS, I looked at loads and loads of example codes and just modified them and guessed and checked them and combined them with other examples until I got what I wanted and I learned from my mistakes. Which is what I'm doing right here. haha, sorry if its a little strange, but I've taught myself this way once, and its the most fun and easiest way for me to learn.
I think you'll find that C/C++ is a whole new ball-game compared to HTML & CSS. C/C++ has a bad temper; one wrong move, and it beats you up -- metaphorically speaking, of course.
Since you're neophyte, I'd recommend that you read this (with emphasis on "recommend"): http://www.cplusplus.com/doc/tutorial/
I'm currently learning by using the Arduino tutorials for beginners on their website. Which is where I've been for the past few hours, I've got access to a lot of hardware and some experience with the hardware thanks to DeVry University. I'm going to be taking my first programming course next semester and right now I'm just tinkering with everything. Ill defiantly take a look at the cplusplus tutorials also, but I'm kicking myself in the ass right now trying to get this light to turn off. I guess Ill just have to ask one of my professors ahah. Thanks anyways!
Sorry about the double post, but I just wanted to say that I did figure out how to do this. Although it isnt using programming ahah. I just decided to use a NOR gate, and whenever the Red LED and the Green LED are LOW (which is the ONLY situation the yellow led should be on) it will turn on the yellow LED.