Pages: [1]   Go Down
Author Topic: Help i'm lost  (Read 718 times)
0 Members and 1 Guest are viewing this topic.
Mercer, PA USA
Offline Offline
Jr. Member
**
Karma: 2
Posts: 55
Arduino & Led's Rock:-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am only a few days into programing with Arduino and am loosing too much sleep:-( i have programed PLC's to do what i want but in a ladder format and it is a bit confusing trying to do it with Arduino but i want to learn as i can think of dozens of uses for them.

so anyway i am trying to make 7x leds go off (LOW) one at a time from left to right when a button (inPut 1) is pressed then come back on after 30sec in the order they went off

then when button 2 (inPut 2) is pressed do the same thing but from right to left, then come back on after 30sec in the order they went off

and when button 3 (inPut 3) is pressed do the same thing but from the center out, then come back on after 30sec in the order they went off

i would like to have a pot on inPut 0 that controls the speed at witch they go off/back on  (but i think i can figure this part out)



i have been trying to figure out how to setup multiple buttons (i can't find anything in the learning section on multiple buttons?)
but here is some code i am playing with. i know i am not doing this right but i am trying to understand things so i am starting simple and trying to find things others have made to see how they work

  int led1Pin = 13;                // choose the pin for the 1 LED
  int led2Pin = 12;              // choose the pin for the 2 LED
  int led3Pin = 11;                // choose the pin for the 3 LED
  int led4Pin = 10;              // choose the pin for the 4 LED

  int input1Pin = 1;               // choose the input pin (for pushbutton 1)
  int input2Pin = 2;               // choose the input pin (for pushbutton 2)
  int input3Pin = 3;               // choose the input pin (for pushbutton 3)
  int input4Pin = 4;               // choose the input pin (for pushbutton 4)

  int val1 = 0;                    // variable for reading the pin status
  int val2 = 1;                    // variable for reading the pin status
  int val3 = 2;                    // variable for reading the pin status
  int val4 = 3;                    // variable for reading the pin status

void setup() {
  pinMode(led1Pin, OUTPUT);      // declare LED 1 as output
  pinMode(led2Pin, OUTPUT);      // declare LED 2 as output
  pinMode(led3Pin, OUTPUT);      // declare LED 3 as output
  pinMode(led4Pin, OUTPUT);      // declare LED 4 as output

  pinMode(input1Pin, INPUT);     // declare pushbutton 1 as input
  pinMode(input2Pin, INPUT);     // declare pushbutton 2 as input
  pinMode(input3Pin, INPUT);     // declare pushbutton 3 as input
  pinMode(input4Pin, INPUT);     // declare pushbutton 4 as input

  digitalWrite(input1Pin, HIGH);
  digitalWrite(input2Pin, HIGH);
  digitalWrite(input3Pin, HIGH);
  digitalWrite(input4Pin, HIGH);

}

void loop(){
  val1 = digitalRead(input1Pin);  // read input value
  if (val1 == HIGH) {            // check if the input is HIGH
    digitalWrite(led1Pin, LOW);  // turn LED OFF
  } else {
    digitalWrite(led1Pin, HIGH); // turn LED ON
  }
      
      val2 = digitalRead(input2Pin);  // read input value
  if (val2 == HIGH) {            // check if the input is HIGH
    digitalWrite(led2Pin, LOW);  // turn LED OFF
  } else {
    digitalWrite(led2Pin, HIGH); // turn LED ON
  }
   val3 = digitalRead(input3Pin);  // read input value
  if (val3 == HIGH) {            // check if the input is HIGH
    digitalWrite(led3Pin, LOW);  // turn LED OFF
  } else {
    digitalWrite(led3Pin, HIGH); // turn LED ON
  }
   val4 = digitalRead(input4Pin);  // read input value
  if (val4 == HIGH) {            // check if the input is HIGH
    digitalWrite(led4Pin, LOW);  // turn LED OFF
  } else {
    digitalWrite(led4Pin, HIGH); // turn LED ON
  }


}


This code is along the right idea for my project so i think if i can get the buttons figured out i can add some (if/else) code in here and maybe make it work

/*
 *  Knight Rider Overkill
 *
 *  By wiring a potentiometer to analog inputs 0 and 1 I can adjust the
 *  speed and brightness.
 *
 * In ASCII art the extra wiring looks something like this:
 *
 *          +-+ potentiometer
 *          |0|
 *          +-+
 *         / | \
 *      5v   |   Gnd
 *           |
 *         inputPin
 */
int speedPin = 0;  // pin to read the potentiometer for speed
int intenPin = 1;  // pin to read the potentiometer for brightness

#define NUMLIGHTS 5
int pins[NUMLIGHTS] = { 5, 6, 9, 10, 11 };

void setup()
{
  int lightPin;

  for (lightPin=0 ; lightPin < NUMLIGHTS ; lightPin++) {
    pinMode(pins[lightPin], OUTPUT);
  }
}

void loop()
{
  static int pos = 0;       // the position of the brightest light in the light array
  static int direction = 1; // the direction the bright spot is travelling (1 or -1)
  int light;
  int speed = analogRead(speedPin);  // how fast the light moves
  int inten = analogRead(intenPin) >> 2;  // read the value and divide by 4 to get range 0 .. 255

  if (inten > 255) inten = 255;

  for (light=0 ; light < NUMLIGHTS ; light++) {
    if (light == pos) {  // The light at this position is set bright      
      analogWrite(pins[light], inten);
    } else if ( light == (pos+1) || light == (pos-1)) {
      // This makes the two lights adjacent to the bright one glow at reduced intensity.
      // It makes for a nicer effect
      analogWrite(pins[light], inten>>4);
    } else {
      // Digital I/O pins 5 & 6 don't seem to go dark if I do analogWrite(pins[light], 0)
      // By doing digitalWrite it all looks correct
      digitalWrite(pins[light], 0);
    }    
  }

  // move the light position
  pos += direction;
  // if we've reached the end, reverse directions
  if (pos >= (NUMLIGHTS-1)) direction = -1;
  if (pos <= 0) direction = 1;
  delay(speed);
}


also if i get this figured out for my next project i would like to know if you can set a button to learn an IR code then map it to buttons?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

#define N_LEDS 4
 const byte ledPins [N_LEDS]= {13, 12, 11, 10};
 const byte inputPins [N_LEDS]= {1, 2, 3, 4};              
 byte vals [N_LEDS];                    // variable for reading the pin status

void setup() {
 for (int i = 0; i < N_LEDS; ++i) {
   pinMode(ledPins [i], OUTPUT);
   pinMode(inputPins [i], INPUT);  
   digitalWrite(inputPins [i], HIGH);
 }
}
..
..
 for (int i = 0; i < N_PINS; ++i) {
   val[i] = digitalRead(inputPins [i]);
   digitalWrite (ledPins [i], val[i] ? LOW : HIGH);
 }


Is a start to make it easier/shorter.
(PS if posting code, please use the "Code" (#) button above.)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Mercer, PA USA
Offline Offline
Jr. Member
**
Karma: 2
Posts: 55
Arduino & Led's Rock:-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

that is amazing how much simpler that looks but i am still trying to figure out how it works

 why do you use?
Code:
const byte ledPins
is that better/different then
Code:
int pins


if i change the 4 to 7 and add 3 more numbers in the N_LEDS code
i could extend the chain right?

Code:
#define N_LEDS [color=#ff0000]4[/color]
 const byte ledPins [N_LEDS]= {13, 12, 11, 10};

how/where does the
Code:
void loop()
come in?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use "byte" because it's smaller than "int", and I use "const" (constant) in this case because I don't plan on changing the values of the LED pins.

If you want to change the number of LEDs, yes, just change the value of N_LEDS and add the new pins to the declaration of "LEDpins".


"loop ()" is the bit at the bottom,
Code:
for (int i = 0; i < N_PINS; ++i) {
   val[i] = digitalRead(inputPins [i]);
   digitalWrite (ledPins [i], val[i] ? LOW : HIGH);
 }
Here, like yours, we read the state of the pin and assign the inverse to the LED.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Mercer, PA USA
Offline Offline
Jr. Member
**
Karma: 2
Posts: 55
Arduino & Led's Rock:-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i am tiring to get it do do something so i can figure how it works
i changed all the errors i could figure out (i don't know if they are right but i think it makes scene)
but i am getting this error and am not sure how to fix it

 In function 'void loop()':
error: 'val' was not declared in this scope

Blue is what i have changed/added/messed up

Quote

 
#define N_LEDS 4
#define N_PINS 4
 const byte ledPins [N_LEDS]= {13, 12, 11, 10};
 const byte inputPins [N_PINS]= {1, 2, 3, 4};
 #define N_LEDS 4                    // variable for reading the pin status
 #define N_PINS 4

void setup() {
 for (int i = 0; i < N_LEDS; ++i) {
   pinMode(ledPins , OUTPUT);
   pinMode(inputPins , INPUT);
   digitalWrite(inputPins , HIGH);
 }
}
void loop () {
  for (int i = 0; i < N_PINS; ++i)
 {
   val = digitalRead(inputPins );
   digitalWrite (ledPins , val ? LOW : HIGH);
 }


 



 
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Before "setup ()"

you need:

Code:
byte val [N_LEDS];

Or you could put it at the start "loop()"


(I spelt it incorrectly in my example, sorry. Also, unless you're going to use the values someplace else, you don't actually need an array, you could simply say
Code:
   digitalWrite (ledPins [i], ! digitalRead(inputPins [i]));
Isn't 'C' great?)

Also, if you've got separate values for "N_PINS" and "N_LEDS" you should really have separate loops for initialisation, in case you add an LED, or remove a button.

Code:
for (int i = 0; i < N_LEDS; ++i) {
  pinMode(ledPins [i], OUTPUT);
}

for (int i = 0; i < N_PINS; ++i) {
  pinMode(inputPins [i], INPUT);
  digitalWrite(inputPins [i], HIGH);
}

« Last Edit: June 19, 2009, 09:32:40 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Mercer, PA USA
Offline Offline
Jr. Member
**
Karma: 2
Posts: 55
Arduino & Led's Rock:-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i guess i don't understand
how do i know if i want to have different values for
Quote
Also, if you've got separate values for "N_PINS" and "N_LEDS" you should really have separate loops for initialisation, in case you add an LED, or remove a button.

also if say i wanted to add a button to control an output unrelated to anything else in the program how would i go about doing it without messing up the rest of the program?
could i just add it into the

const byte inputPins [N_PINS]= {1, 2, 3, 4, 52};
or would i just make a separate line
Code:
int inputPin = 52;
?

Thanks

Quote

 
#define N_LEDS 4
#define N_PINS 4
 const byte ledPins [N_LEDS]= {13, 12, 11, 10};
 const byte inputPins [N_PINS]= {48, 50, 52, 49};
 #define N_LEDS 4                    // variable for reading the pin status
 #define N_PINS 4
 byte val [N_LEDS];

void setup() {
 for (int i = 0; i < N_LEDS; ++i) {
   pinMode(ledPins , OUTPUT);
   pinMode(inputPins , INPUT);
   digitalWrite(inputPins , HIGH);
 }
}
void loop () {
  for (int i = 0; i < N_PINS; ++i)
 {
   val = digitalRead(inputPins );
   digitalWrite (ledPins , val ? LOW : HIGH);
 }
}

 
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The reason I put the LED pins and button pins into arrays was because what they were doing in your original sketch all looked roughly the same.
On the other hand, if you've got a bunch of LEDs that form a meter, and another that just indicates the state of a switch, then it may not make sense to group it with the others.

It also jus happened that in your original sketch, you had the same number of LEDs as switches; this may not always be the case.
« Last Edit: June 19, 2009, 09:54:03 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: