Go Down

Topic: Issue Setting Up Circuit for 5 Button/Fade LED's (Read 507 times) previous topic - next topic

plausiblephysicist

When tasked with setting up one LED to fade on and then off after reading an input voltage, and to also have that same LED digitalWrite on/off due to a button press, we were fine. However, when expanding our code to include 5 LED's (each which should be able to fade due to input voltage, and turn on/off due to a button), and expanding our circuit to a breadboard, we can only get the fade function to work. 3 of the 5 LEDs immediately write HIGH, the other 2 remain LOW. The read pins do not trigger as programmed, and the LEDs change state erratically when the arduino board is tapped and 2 LEDs get triggered by one read pin irregularly. I don't know what to make of this. Do you?

Here's our set-up


In the instance that you would like to see our code:
Code: [Select]
int voltage0, voltage1, voltage2, voltage3, voltage4 = 0;
int fade0 = 11;
int fade1 = 10;
int fade2 = 9;
int fade3 = 6;
int fade4 = 5;
int buttonPin0 = 4;
int buttonPin1 = 3;
int buttonPin2 = 2;
int buttonPin3 = 1;
int buttonPin4 = 0;
int analog0 = 0;
int analog1 = 1;
int analog2 = 2;
int analog3 = 3;
int analog4 = 4;
int buttonPushCounter0, buttonPushCounter1, buttonPushCounter2, buttonPushCounter3, buttonPushCounter4 = 0;
int buttonState0, buttonState1, buttonState2, buttonState3, buttonState4 = 0;
int lastButtonState0, lastButtonState1, lastButtonState2, lastButtonState3, lastButtonState4 = 0;

void setup()
{
   pinMode(fade0, OUTPUT);
   pinMode(fade1, OUTPUT);
   pinMode(fade2, OUTPUT);
   pinMode(fade3, OUTPUT);
   pinMode(fade4, OUTPUT);
 
   pinMode(analog0, INPUT);
   pinMode(analog1, INPUT);
   pinMode(analog2, INPUT);
   pinMode(analog3, INPUT);
   pinMode(analog4, INPUT);
   
   pinMode(buttonState0, INPUT);
   pinMode(buttonState1, INPUT);
   pinMode(buttonState2, INPUT);
   pinMode(buttonState3, INPUT);
   pinMode(buttonState4, INPUT);
}

void loop()
{
  whoopie(fade0, voltage0, analog0);
  whoopie(fade1, voltage1, analog1);
  whoopie(fade2, voltage2, analog2);
  whoopie(fade3, voltage3, analog3);
  whoopie(fade4, voltage4, analog4);
  button(buttonState0, buttonPin0, lastButtonState0, buttonPushCounter0, fade0);
  button(buttonState1, buttonPin1, lastButtonState1, buttonPushCounter1, fade1);
  button(buttonState2, buttonPin2, lastButtonState2, buttonPushCounter2, fade2);
  button(buttonState3, buttonPin3, lastButtonState3, buttonPushCounter3, fade3);
  button(buttonState4, buttonPin4, lastButtonState4, buttonPushCounter4, fade4);
}

void whoopie(int led, int voltage, int analog)
{
   voltage = analogRead(analog);
   
    if(voltage > 950 && voltage <= 1029)
    {
      for(int brightness = 0; brightness <255; brightness +=20)
      {
        analogWrite(led, brightness);
        delay(5);
      }
     
      for(int brightness = 255; brightness > 0; brightness -=4)
      {
        analogWrite(led, brightness);
        delay(50);   
      }
    }
  }

void button(int buttonState, int buttonPin, int lastButtonState, int counter, int led)
{
    buttonState = digitalRead(buttonPin);
    if (buttonState != lastButtonState)
    {
      if (buttonState == HIGH)
      {
        counter++;
      }
     
    delay(50);
   
    }

  lastButtonState = buttonState;

  if (counter % 2 == 0)
  {
    digitalWrite(led, HIGH);
  }
  else
  {
    digitalWrite(led, LOW);
  }
}

Grumpy_Mike

You have not wired it up correctly. I can see that the second from the lowest LED is not connected to ground. It also looks like you are not using the pins that your software says you are.
A clearer picture would help, that inr is quite blurred.

plausiblephysicist

I know that all the LEDs are connected to ground. When using the analogRead pins as triggers for the fade function, all 5 LEDs trigger and fade respectively. I've double checked all the pins and connections.

Here's a description of what happens:
From just uploading the program and using a wire directly from the 5V output pin to the read pins--

  • Initially, LEDs out of pins 11, 10, and 9 are on. LEDs out of pins 6 and 5 are off

  • When I put a 5V input into pin 4, the corresponding LED out of pin 11 turns off. The LED does not turn back on when 5V is again connected to pin 4

  • When I put 5V into pin 3, LEDs out of both pin 10 and pin 9 turn off, and do not turn on agian when 5V is again connected. There isnt any intra-LED contact on my breadboard.

  • The LEDs out of pins 6 and 5 are initially off for unknown reasons, and don't turn on when 5V is connected to pins 1 or 0, respectively. 



From just uploading the program and using a resistor from a GND pin into the read pins and touching the 5V wire to the read pin side of the resistor--

  • Same initial LED conditions as above

  • When the resitor is connecting GND and pin 4, and the 5V touches the pin 4 side of teh resistor, LED out of pin 11 turns off, but returns to on as soon as the 5V is removed from the resistor wire

  • When the resistor is in pin 3 and the 5V touches it, both LEDs out of pins 10 and 9 turn off, and turn on again when the 5V is removed from the resistor

  • When the resistor is in pin 2 and the 5V touches it, the LED out of pin 9 turns off and then on when the 5V is removed

  • As soon as the resistor is in pin 1, the LED out of pin 6 turns on. When the resistor is removed, the LED turns off. When the 5V is touching the resistor, both LEDs out of pins 6 and 9 turn off, then on again when the 5V is removed. When the resistor is removed, LEDs out of pins 6 and 9 turn off and stay off.

  • When the resistor is in pin 0, there are no visible effects in any conditions.


Grumpy_Mike

Quote
I know that all the LEDs are connected to ground.

Then prove it. Write a simple sketch that tests out your LEDs by just turning them on one at a time.

Your code is totally screwed.
You have
Code: [Select]
whoopie(fade0, voltage0, analog0);
When you have not even initialized a value for voltage0, then in the function what ever value you passed in that variable is then replaced by an analogue read.
You are doing an analogue read without having anything connected to the analogue port, that gives random values as the port is floating.
Basically I think you don't know how functions work, you pass them variables and they work on the value of those variables. There is no point in passing a global variable to a function.

Long and convoluted explanations of what happens when are no good without you saying what you expect to happen.

plausiblephysicist

#4
Jun 24, 2014, 03:52 pm Last Edit: Jun 24, 2014, 05:49 pm by plausiblephysicist Reason: 1
Issue:
Quote
Then prove it. Write a simple sketch that tests out your LEDs by just turning them on one at a time.



Answer:
Quote
When using the analogRead pins as triggers for the fade function, all 5 LEDs trigger and fade respectively.


Could there be an issue of not getting enough current to the last two, as our circuits are set in parallel?

Quote
When you have not even initialized a value for voltage0, then in the function what ever value you passed in that variable is then replaced by an analogue read.


Voltage0 is initialized in the very first line of the code. The purpose of voltage being set equal to the analogRead is so that our if statement can use it in its parameters.

Quote
You are doing an analogue read without having anything connected to the analogue port, that gives random values as the port is floating.


Code: [Select]
if(voltage > 950 && voltage <= 1029) This keeps the lower float values from being used. That's also why we initialized the voltage variable.

Quote
Long and convoluted explanations of what happens when are no good without you saying what you expect to happen.

Here's what we want:

5 LEDs are set up. Touching the +5V to an analog read pin once will trigger a respective LED to fade in then fade out. Touching +5V to a digital read pin will turn one respective LED on, touching again will turn it off. Even in the face of incorrect coding, I still don't see why the LEDs do not react in the same manner when their digital read is triggered, or why three of the LEDs initially turn HIGH, and two turn LOW.

I understand that functions are meant to be passed variables, and to then work based upon the values of those variables, seeing as that's what I did. Example:
Code: [Select]
whoopie(fade0, voltage0, analog0);

Obviously we're passing the whoopie function variables (fade, voltage, and analog). The reason for calling it multiple times is because we have multiple LEDs acting within the same code, but with varying voltage and read pins. I'm not very versed in the pros/cons of global vs. local variables. Would it be better to use local variables instead?

I just observed that when
Code: [Select]
buttonState = digitalRead(buttonPin) is changed to
Code: [Select]
buttonState = analogRead(buttonPin)  all 5 LEDs turn on initially.

Grumpy_Mike

If you are not going to listen to what you are doing wrong why have you bothered asking the question?
You are so busy trying to vindicate yourself that you will find it impossible to learn anything.

If as you think you are doing everything correctly why do you think it dosn't work?

plausiblephysicist

Sir, I do not mean to ignore advice or to vindicate my actions. I know that there must be something wrong, but I don't know what the cause of the problem is. In answering as I have, I attempt to be thourough in order to locate the issue with either my code or my set-up. I've tried to explain what I expect to happen. I checked my ground connections, I explained my reasoning for using open analog read pins, and have checked that all my variables have been intitiallized.

This leads me to believe that the issue lies elsewhere, yet I do not see it. When I use the beforeposted code with only one set of variables for one LED, both the fade and the button function as intended. When four more LEDs and four more sets of variables are added, the before posted problems arise.

Grumpy_Mike

Quote
I know that there must be something wrong, but I don't know what the cause of the problem is.

So you start by fixing the things that are wrong and that are pointed out to you as wrong.

Your whoopie function is passed three variables. One of which is totally useless because:-
1) It has no value when you pass it.
2) What ever that value is you immediately overwrite by setting it to a value read from an analogue port.
Correct this. If you do not consider this is wrong then don't bother reading this post any further and take up knitting.

The variables
Code: [Select]
int voltage0, voltage1, voltage2, voltage3, voltage4 = 0;
Are never used, they never have any values assigned to them, so remove them or use them, so your assertion that:-
Quote
and have checked that all my variables have been intitiallized.

Is quite simply wrong. I can see from your code that you have not. If you think you have then you do not understand what you are doing, we can help but only when you stop denying that there is nothing wrong.

Now your button state function:-
Code: [Select]
button(buttonState0, buttonPin0, lastButtonState0, buttonPushCounter0, fade0);
Lots of things wrong,
1) You use buttonState to define a pin in the setup function, all these values are zero so you set the mode of pin zero to be an input five times.
2) You then go and use the same variable name to hold the value of that button.
3) The variables lastButtonState0 to lastButtonState5 never get updated because the updating happens inside the function and it is not passed back out of the function so your global variables remain unchanged.
Fix these things, if it still does not work post the fixed code again and give a brief description of what is does and what you want it to do.

Prove that your LEDs are working by writing a small sketch to turn them on and off in turn. Do not give me the rubbish:-
Quote
I checked my ground connections

Because unless you test them, no amount of visual checking will prove they are write. Your tests that you think prove they are right are not very good tests.
Draw a schematic of what you thing you have wired up, you photograph is not sufficient.


plausiblephysicist

I removed the voltage variables. How to I use the varialbes of buttonState and lastbuttonState while avoiding setting them as pin 0?

Here's my set-up.

Grumpy_Mike

Quote
Here's my set-up.

No it is not.
Or if it is you can never light up the LEDs because you are shorting them out.

Quote
avoiding setting them as pin 0

In the setup function use:-
Code: [Select]
pinMode(buttonPin0, INPUT);

Quote
How to I use the varialbes of buttonState and lastbuttonState

Code: [Select]
lastButtonState0 = button(buttonPin0, lastButtonState0, buttonPushCounter0, fade0);
Then your function needs to be:-
Code: [Select]
int button( int buttonPin, int lastButtonState, int counter, int led)
{
    buttonState = digitalRead(buttonPin);
    if (buttonState != lastButtonState)
    {
      if (buttonState == HIGH)
      {
        counter++;
      }
     
    delay(50);
   
    }

  lastButtonState = buttonState;

  if (counter % 2 == 0)
  {
    digitalWrite(led, HIGH);
  }
  else
  {
    digitalWrite(led, LOW);
  }
  return lastButtonState; }


Now the thing that will not work is that your buttonPushCounter0 will never be updated because you can not increment it in the function and have that value returned from a function. You can only return one variable from a function and that is now  lastButtonState.
Anyway maker those changes and post your code and we will take it from there.

plausiblephysicist

#10
Jun 27, 2014, 09:56 pm Last Edit: Jun 27, 2014, 09:58 pm by plausiblephysicist Reason: 1
After applying those changes, I recieve this error message: "too many arguments to function 'void button(int, int, int, int)'"

Code: [Select]
Arduino: 1.5.6-r2 (Linux), Board: "Arduino Uno"

sketch_jun27a.ino: In function 'void loop()':
sketch_jun27a:6: error: too many arguments to function 'void button(int, int, int, int)'
sketch_jun27a:49: error: at this point in file
sketch_jun27a:6: error: too many arguments to function 'void button(int, int, int, int)'
sketch_jun27a:50: error: at this point in file
sketch_jun27a:6: error: too many arguments to function 'void button(int, int, int, int)'
sketch_jun27a:51: error: at this point in file
sketch_jun27a:6: error: too many arguments to function 'void button(int, int, int, int)'
sketch_jun27a:52: error: at this point in file
sketch_jun27a:6: error: too many arguments to function 'void button(int, int, int, int)'
sketch_jun27a:53: error: at this point in file
sketch_jun27a.ino: In function 'void button(int, int, int, int)':
sketch_jun27a:78: error: 'buttonState' was not declared in this scope
sketch_jun27a:100: error: return-statement with a value, in function returning 'void'

 This report would have more information with
 "Show verbose output during compilation"
 enabled in File > Preferences.


I ran the example blink code on each of the LEDs in their current set-up , they all work. I now see that in the fritz picture, 220 ohm resistors are shown, I'm using 2200 ohm resistors.  

Grumpy_Mike

Quote
I now see that in the fritz picture, 220 ohm resistors are shown, I'm using 2200 ohm resistors. 

It is irrelevant what the resistor is. The LED in not actually in the circuit. Both its legs are connected to the same point it is a short.

Quote
After applying those changes,

Which you have not posted as requested.

You are getting the errors because you have not applied them correctly.

I am seriously thinking you are just wasting time here. Have you any idea what you are doing? Is this a school project? You show so little understanding and are so unwilling to cooperate and you do not listen to the advice given you. If your attitude does not improve I will be leaving this thread.

luisilva

I dont like this too:
Code: [Select]
  button(buttonState4, buttonPin4, lastButtonState4, buttonPushCounter4, fade4);

You are not thinking that the value of "lastButtonState4" or "buttonPushCounter4", etc. will get the value that is inside the function, right? Because if you need this (and I think you need) you must use pointers and not this!

Go Up