Led coding oversight. help please.

I’m two weeks new to Arduino Family fun night. I just wanna say thank you to all tho’s who actively revise and help others as well as take the time to explain in the amount of detail as you have. There’s no way I could be wear I am in two weeks without such a great community effort.
Ok the brown nosing is over. Srry

I’m currently working on a fan controller.I know been done blah. But this one will have adafruits new touch screen mounted in a friends pc front panel controlling three fans and a sleuth of RGB LED’s in a clear pc case. among other things. “evil laugh”

I rarely ask for help before I’ve gone over my problem several times dug deep into fourms and ref’d the help pages. Ego thing i guess. And perhaps it cause it’s 5am and I’m just over looking something silly.

I’m using a mega and I’ve set up a One button 3 stage output (i.e Low, Med, High) As I’ve stated It’s going to be used for fan speed in the end but right now I have it hooked up to a single led
for debugging. My problem is when it starts all is fine setup sets the led on low(85) I click the button It changes to Med(170) I click the button again It goes to high(255) but after one sec It jumps back to Low(85) on it’s own. Now I do want it to go to Low Med Hight then start over but only when i Press the button. I figure the problem resides in the Second If statement.

But I’m just Missing it. If you could just have a quick look at my code.

Anyways suggestions Would be Extremely Helpful .

Code:

/* Fan Controller/Led three stage brightness (i.e. Low, Med, High)

One button three stages then starts over again.

circuit:

  • pin 8 to 200ohm to LED to ground
  • pushbutton attached to pin 2 from +5V
  • 10K resistor attached to pin 2 from ground
    */

int FanButton1 = 2; // assign button 1 to pin 2 on board.
// int FanButton2 = 3; I plan to add more buttons so these are commented out.
// int FanButton3 = 4;

int Fan1 = 8; // assign Fan to pin 8

int Fanstate1 = 0; // Variable for reading fan speed state.
// int Fanstate2 = 0;
// int Fanstate3 = 0;
int FanSpeed = 85 ; // Fan speed set to low on start.

void setup () {
pinMode(FanButton1, INPUT); // Sets button as input
analogWrite(Fan1, FanSpeed);
Serial.begin(9600); // open the serial port at 9600 bps: Used for Debug
Serial.println(FanSpeed);
}

void loop() {

Fanstate1 = digitalRead(FanButton1); // check button state

if (Fanstate1 == HIGH && FanSpeed <=255) { // check if button is pressed & less/then full power
FanSpeed = FanSpeed + 85; // increase in three steps 255/3=85 (I.E. Low,Med,High)
analogWrite(Fan1, FanSpeed); // Adjusts Fan to Current FanSpeed
Serial.println(FanSpeed);
delay(1000);
}

if (Fanstate1 == HIGH && FanSpeed >=255) { // check if button is press & full power
FanSpeed = 85; // Sets speed back to 85 if speed has reached 255
analogWrite(Fan1, FanSpeed); // Adjust Fan to current FanSpeed
Serial.println(FanSpeed);

}

}

[/table]

do you have a pulldown connected to the AnalogInput? if not, when the switch is open, noise may look like a switch input

In addition to the resistor that is needed, you could improve your coding a bit:

    if (Fanstate1 == HIGH)
    {
        FanSpeed += 85;  // increase in three steps  255/3=85   (I.E. Low,Med,High)
        if(FanSpeed > 255)
        {
            FanSpeed = 85; // Sets speed back to 85 if speed has exceeded 255
        }
        analogWrite(Fan1, FanSpeed);   // Adjusts Fan to Current FanSpeed
        Serial.println(FanSpeed);
        delay(1000);
    }

Only two if tests are required - one to determine if the switch was pressed and one to determine if the speed is out of range. When the button is pressed, the speed is increased, unconditionally. Then, a test is performed on the new value, and it is brought back in range if it went out of range.

You can add additional Serial.print(ln)() statements, to monitor where in the code it is, to see if "floating pin" (a google search term for you) is your problem.

I'll check out floating pin thanks. as to the resistor If you actually looked at my code it's states there is one in CIRCUIT:

Is There someThing wrong with your Shift key?

If you actually looked at my code it's states there is one in CIRCUIT:

The comments are there for people that can't read code. I generally ignore them, since most newbie comments are either wrong or totally obvious.

Just because there is a comment there that says that the circuit SHOULD use an external resistor is no guarantee that you DID use an external resistor, so we ask.

True I see your point Sorry. And yes ShIfT ItS a BaD HaBit. lOl j/k

OH I almost forgot thank you very much "PaulS" That fixed it.