Pages: [1]   Go Down
Author Topic: Completly stumped on why this will not work  (Read 398 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

SOLVED     I went back over it and it was really quite simple after like two days of trouble shooting i realized that i was not resetting the leds after each time the loop went around so once it turned on it had no reason to turn back off. Thank you to everyone that helped me get though this.      Well this is a great start for me apparently and man do i hate seeing two of my post on the front  page but i have an issue. While i just got over my last problem i finished my little program to get three little lights to blink randomly.  The problem is the fact that there are not turning off as soon as the program loads into the uno all of the leds just light up and no matter what i do they stay on if i press the button or unplug it change the delay in the loop i can not figure out what i have gone wrong with in this code.
Code:
int first_red_LED = 0;
int second_red_LED = 0;
int green_LED = 0;
int button = 0;
void setup()
{
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(2, INPUT);
    button = digitalRead(13);
  button = LOW;
}

void loop(){
  first_red_LED = 0;
  second_red_LED = 0;
  if (true == true){
    first_red_LED = random(2);
    second_red_LED = random(8);
  }
  if ( button == LOW) {
    if ( first_red_LED  == 1 && (second_red_LED  == 2 || second_red_LED  == 4))
    {
      digitalWrite(3, HIGH);
    }
    if ( first_red_LED == 1)
    {
      digitalWrite(5, HIGH);
    }
    if ( second_red_LED == 2 || second_red_LED  == 4)
    {
      digitalWrite(4, HIGH);
    }
  }
  else
  {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
  }
  delay(250);
}
« Last Edit: December 30, 2013, 01:02:18 pm by 3_141592_Co » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well i just checked and everything is in the right place and it does compile and run so that was not the problem.
« Last Edit: December 29, 2013, 09:27:32 pm by 3_141592_Co » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i can not figure out what i have gone wrong with in this code.
You have not put each { on a new line. You have not used Tools + Auto Format to properly indent your code. You really should do both. As an experienced programmer, you might want to revert to putting the { on the line with the statement (I do not). As a newbie, the { goes on a line by itself. Trust me, it will make your code structure a lot more obvious.
Logged

Victoria, Australia
Offline Offline
Edison Member
*
Karma: 46
Posts: 1372
Electronics Engineer/Industrial Control
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, could you post a circuit diagram, or pic of hand drawn circuit, and a pic of your project.
This will help greatly to locate a solution to this problem.

Tom.... smiley
Logged

Everything runs on smoke, let the smoke out, it stops running......VK3DMK

Offline Offline
Edison Member
*
Karma: 36
Posts: 1219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Tell us (a lot) more.

How is the button connected, to the +5V or to GND ?   Do you use a resistor ?

I hope you do have resistors with those leds and they are connected to the GND.

What is the goal ?
You have a sketch that (randomly) turns leds on, while a button is pressed. As long as the button is pressed, they will not turn off. Since the delay is 110ms, I guess that all three leds are on after 1 to 3 seconds.
Do you want each led to blink randomly independent, or is there some special reason for the linked-together randomness ?

You could try a different kind of led blinking (not tested):
Code:
analogWrite (first_red_LED, random (256));
analogWrite (second_red_LED, random (256));
analogWrite (green_LED, random (256));
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK i will be uploading picture as soon as my phone uploads it but is the picture is not the best it is the same design as the 2nd project in arduino project book all that is different is the code. So the point is that it is random and when the two red leds light up they are too make the gree one light up only when the other two ones are on and just for fun i am defently using resitors
https://www.google.com/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&docid=iN7jErVinohBJM&tbnid=Y6TQUDLJy89L8M:&ved=0CAUQjRw&url=http%3A%2F%2Felectronics.stackexchange.com%2Fquestions%2F56992%2Fpull-down-resistor-for-arduino-input-to-read-low-when-no-voltage-from-switch&ei=s-PAUreZE7ffsATnxoKIDQ&bvm=bv.58187178,d.eW0&psig=AFQjCNGwZkZOz9M42ki_2QwLZ8UWS36EDw&ust=1388459314627912

even if i do not press the the button they do not turn off.
« Last Edit: December 29, 2013, 10:05:54 pm by 3_141592_Co » Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not eliminate the  possibility of hardware error and replace ONE of the LED's with build in one on pin 13?" Than set the "button" to LOW in Setup - thus bypassing  the button hardware in Loop.
Than add some Serial prints in places, right now you are just flying blindfolded.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK i just moved the button stuff to the setup part and i changed it to port 13 and nothing happened differently  i would like to add with my first out of the book setup it worked with the book codes so i believe it is my code i managed to mess up
« Last Edit: December 29, 2013, 10:17:13 pm by 3_141592_Co » Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read my reply again.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am still confused
Logged

NL
Offline Offline
Newbie
*
Karma: 1
Posts: 30
Tech Tinkerer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Assuming you use a normal Arduino like a uno or so, there is a led and a resistor connected to PIN 13 on the pcb.
This makes it harder to use as input. I usually avoid using pin 13 as an input.

First thing i would do is move the button to another pin, define the new pin as input with pullup and have the button switch to ground (As you detect button == LOW).

See if that fixes things. (I did not look in detail at the rest of the code).

Thanks,
Guido
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok so i have done all of those fixes that have been stated before non of them have worked i am starting to think it is either is that random is always making the value true or that the numbers do eventuly find the right number but it does not reset each time the loop comes around the way i want it to it also could be a combination fo these two things. Some more info on what it is doing the first red lights up then comes the second one imidiatly followed by the green led light it supposed to do that but it never resets and starts over.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Some more info
that would be useful would be your latest code. We don't guess what you changed.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are reading your button state only in setup, not in your loop. So it looks just once to its state and then never ever looks at your switch again.
Good luck!
Logged

Victoria, Australia
Offline Offline
Edison Member
*
Karma: 46
Posts: 1372
Electronics Engineer/Industrial Control
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, sorry, see POST #3.

Thanks Tom.... smiley
Logged

Everything runs on smoke, let the smoke out, it stops running......VK3DMK

Pages: [1]   Go Up
Jump to: