Pages: [1]   Go Down
Author Topic: Really need help with Circuit  (Read 598 times)
0 Members and 1 Guest are viewing this topic.
South Africa - JHB
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Amazingly water is wet.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys, OK im loosing my mind here.

Im using a virtual applet that shows the application is working.
Code:
//Save some memory, use bytes and booleans rather than ints for pin numbers and pin states. (pin numbers are <255, so an int isnt needed)
const byte buttonPin = 9;             // the number of the pushbutton pin

// variables will change:
boolean buttonState = 0;                  // Variable for reading the pushbutton status
byte ledPin = 0;
boolean loopwait = 0;

#define maxLEDs 9 //This replaces anything which say 'maxLEDs' with the number 9
byte ledPins[maxLEDs] = {0,1,2,3,4,5,6,7,8}; //set your LED pins here

void setup() {
  //pinMode(ledPin, OUTPUT);       // Initialize the LED pin as an output:
  pinMode(buttonPin, INPUT);     // Initialize the pushbutton pin as an input:
  for (byte i = 0; i < maxLEDs; i++){
    pinMode(ledPins[i], OUTPUT); //Set all of the different LEDs to outputs.
  }
}

void loop(){
  buttonState = digitalRead(buttonPin);  // read the state of the pushbutton value:
  if (buttonState == HIGH) {                 // check if the pushbutton is pressed.
    if (loopwait == 0) {
      // turn LED on:   
      byte randomNumber = random(100); //You want all statements to check against the same number, not generate a new one for each elseif() statement
      if (randomNumber < 20){
        ledPin = 0;
      }
      else if (randomNumber < 40){
        ledPin = 1;
      }
      else if (randomNumber < 60){
        ledPin = 2;
      }
      else if (randomNumber < 70){
        ledPin = 3;
      }
      else if (randomNumber < 80){
        ledPin = 4;
      }
      else if (randomNumber < 90){
        ledPin = 5;
      }
      else if (randomNumber < 94){
        ledPin = 6;
      }
      else if (randomNumber < 98){
        ledPin = 7;
      }
      else if (randomNumber < 101){
        ledPin = 8;
      }
      digitalWrite(ledPins[ledPin], HIGH);
      loopwait = 1;
    }
  } else {
    loopwait = 0;
    digitalWrite(ledPins[ledPin], LOW);       // turn LED off:
  }
}

but I cant get the wiring right at all on this. And I cant find anything at the moment. Can anyone help, I keep getting it running random at massive speed when switch is open and when closed it stops.

Can anyone assist either my code is wonky or the wiring is wonky.
Logged

South Africa - JHB
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Amazingly water is wet.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This Basically the Circuit I have currently.  smiley-cry
Im really sucking at this. I gotta study more.

https://www.dropbox.com/s/q5fv9qehnxwnb05/Arduino.PNG
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This Basically the Circuit I have currently.  smiley-cry
Im really sucking at this. I gotta study more.

https://www.dropbox.com/s/q5fv9qehnxwnb05/Arduino.PNG

First thing is that you need a pull-down resistor wired from pin D9 to ground, 10k ohms will work fine. Second thing is all those LED each require a series current limiting resistor of around 200-500 ohm each, then remove the single 10 (?) ohm resistor. As you have it the leds will get dimmer and dimmer as you light more then one at a time.

Lefty
« Last Edit: July 09, 2012, 03:15:15 pm by retrolefty » Logged

South Africa - JHB
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Amazingly water is wet.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey retrolefty,

The lights will only have one light at a time with the application thats running (well it is supposed to) this was why I only utilized one Resistance.
Ill try adding the resistor to the Pin 9 from button, hopefully this solves my issue.

Thanks.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34210
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Do as you are told with the LEDs, or at least change the 10R to at least 220R.
Logged

0
Offline Offline
Full Member
***
Karma: 2
Posts: 125
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can skip the resistor on pin 9 entirely if you connect the other side of the switch to GND and do a
Code:
digitalWrite(9, HIGH)
to enable the internal pull-up resistor. This will give you LOW when switch is closed and HIGH when it is open. Connecting it to 5V like in the picture however will give you HIGH when the switch is closed and ??? when it is open.
Logged

0
Offline Offline
Full Member
***
Karma: 2
Posts: 125
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You also want to try and avoid using pins 0 and 1 in Arduino projects because they are the debugging pins. All you have to do is offset some of your LEDs. And definitely use a resistance > 100 ohms.
Logged

Pages: [1]   Go Up
Jump to: