Help with troubleshooting led project

Hello!

I have this led project which has gotten me some troubles for a while. I have 3 strings of 10 leds parallel in each, driven by 2n2222 from the arduino pins 9, 10 and 11. What I want to do is that in one switch position I fade all the leds with a potentiometer, in another position all leds are on, and one string at a time fades off and back on. It works fine when it is connected to Uno, but in the project board in startup it seems to choose one of the modes and then the switch do anything. So I guess the code is fine?

Connections:
Pin 7 to +5v
Pin 8 to gnd
Pin 9 & 10 to crystal with 22pf caps to gnd
Pin 15, 16 & 17 to 2,7kOhm resistors to the bases of the 2n2222.
Pin 22 to gnd
Pot wiper to pin 23, lugs 1&3 to gnd and +5v
Switch (spdt toggle) lug to pin 25 which is connected to gnd through 9,1k resistor, and the other lug to +5v

Hope you can decipher what I just wrote. Thanks in advance, any help is welcome!

const int buttonPin = 2;     
const int ledPin =  9;      
const int ledPin1 =  10;    
const int ledPin2 =  11;     
const int lowestPin = 9;
const int highestPin = 11;
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9;
const int analogOutPin1 = 10;
const int analogOutPin2 = 11;

// variables will change:
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin


void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  pinMode(ledPin2, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
  for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++) { 
    pinMode(thisPin, OUTPUT); 
  Serial.begin(9600);   
  }
}


void loop(){
  int reading = digitalRead(buttonPin);
 
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  if ((millis() - lastDebounceTime) > debounceDelay) {
    buttonState = reading;
  }
  
  digitalWrite(ledPin, buttonState);

  lastButtonState = reading;

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    //fade leds    
    for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++) { 
    // fade the LED on thisPin from off to brightest:
    for (int brightness = 200; brightness >= 20; brightness--) {
      analogWrite(thisPin, brightness);
      delay(5);
    } 
    // fade the LED on thisPin from brithstest to off:
    for (int brightness = 20; brightness < 200; brightness++) {
      analogWrite(thisPin, brightness);
      delay(5);
    } 
    // pause between LEDs:
    delay(10);
  }  } 
  else {
    // fade with pot
  sensorValue = analogRead(analogInPin);            
 
  outputValue = map(sensorValue, 0, 1023, 0, 255);  
  
  analogWrite(ledPin, outputValue);           
  analogWrite(ledPin1, outputValue);           
  analogWrite(ledPin2, outputValue);           

 
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);   

   delay(2);   
  }
 }}

Hi villl, Welcome to the Arduino forums! :-)

if (reading != lastButtonState) {
    lastDebounceTime = millis();
  if ((millis() - lastDebounceTime) > debounceDelay) {
    buttonState = reading;
  }

I don't understand why your code worked originally, but the section above does seem to show why it's not working now. lastDebounceTime is always set to millis() right before you do the second if statement. This means that millis()-lastDebounceTime is never going to be greater than debounceDelay; especially set to 50! Therefore buttonState = reading; will never run, and if buttonState never changes neither will your mode.

I hope that helps. :-)

I also noticed that you initialised the led output pins twice (apart from ledpin1). And you also initialised Serial.begin 3 times, as it's within the for loop used to initialise the led pins. It seems to me that you struggle to keep track of which commands are within which sets of brackets, it might help to practise indenting your code better. :-)

Good luck with your project!

Deeksie

I recommend that you adopt a different coding style. Put each { and } on separate lines with matching pairs indented by the same amount, and with the code between them indented one level further. This makes it far easier to see the control structure in your code and also to see where you have got the structure wrong (as you have done in several places).

There is another established style that puts the { on the end of the preceding line. This produces shorter code (takes up less lines on the screen) which was handy when screens only had 25 lines - these days that's not such an advantage. You'll often see this style used, but I don't recommend it because it does not make the matching pairs of braces so obvious.

Thank you for your help!
I tried cleaning the code a bit (look any better?) Also I took off the debounce part and replaced it with “if” commands. But now I’m experiencing a weird problem. When I put my finger close to the right side of the chip on the project board, it switches into the fade-chase mode, and when I move the finger away, it goes back to the pot-fade mode. The crystal is on that side of the board, anything to do with that?

Here’s the code:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  9;      // the number of the LED pin
const int ledPin1 =  10;      // the number of the LED pin
const int ledPin2 =  11;      // the number of the LED pin
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
int buttonState = 0;             // the current reading from the input pin


void setup() 
{
   pinMode(buttonPin, INPUT);     
   
   for (int thisPin =ledPin; thisPin <= ledPin2; thisPin++) 
  { pinMode(thisPin, OUTPUT);    
  }
}


void loop()
 {
  
   buttonState = digitalRead(buttonPin);
 
    if (buttonState == HIGH)
  { 
    for (int thisPin =ledPin; thisPin <= ledPin2; thisPin++) 
    { for (int brightness = 200; brightness >= 20; brightness--) 
      { analogWrite(thisPin, brightness);
        delay(2);
       } 
    for (int brightness = 20; brightness < 200; brightness++) 
      { analogWrite(thisPin, brightness);
        delay(2);
       } 
    delay(100);
    }
  } 
    if (buttonState == LOW)  
  {
    sensorValue = analogRead(analogInPin);            
    outputValue = map(sensorValue, 0, 1023, 0, 255);  
    analogWrite(ledPin, outputValue);           
    analogWrite(ledPin1, outputValue);           
    analogWrite(ledPin2, outputValue);           

  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);   

   delay(2);   
  }
 }

But now I'm experiencing a weird problem. When I put my finger close to the right side of the chip on the project board, it switches into the fade-chase mode, and when I move the finger away, it goes back to the pot-fade mode.

Sounds like a floating pin. Have you got a pulldown on "buttonPin"?

Thanks everyone for your help! I got it now, I had connected the switch to analogpin not digital :facepalm:. Now it works as it should :)

That should not make any difference, see:- http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Yeah but that's the problem I thought it was connected to digital :D