New to coding, need help.

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...

Here is my code:

pushbutton = a proximity sensor in my case.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
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:
const int buttonPin = 8; // the number of the pushbutton pin
const int ledrPin = 13; // Red Pin
const int ledyPin = 12; // yellow Pin
const int 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?

Thanks so much
-Joey
Last edited on
closed account (zb0S216C)
InfamousXTC wrote:
"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?

Wazzak
@ Framework

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.
Last edited on
closed account (zb0S216C)
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/

Wazzak
Last edited on
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.
Topic archived. No new replies allowed.