Learning code ... the ups and downs

Thought id post this example to show what im going through in the learning and understanding prossess of coding. For the most part it is very frustrating ... i would have very limited understandig at the moment and practically new to coding have tried before but stopped as other things came up.

But have started again and finding it hard going ... but am having some success ..

Number one reason is not being able to find any site with a teaching process .... have found some great sites that show very good tutorials eg tronixstuff ... 

and googling i find examples of code everywhere .... but breaking them down to understand is another thing ... even though i can adapt and change some of the code to do something else ... trying to combine different tutorials together to create something is proving very difficult .

Have done some code for servos ... leds and ultrasonic sensors (even managed to controll servos with photoresistors by making up code last year) but it was all just basic tutorials i follwed and changed parts that i understood.

Tutorials are great as you just copy what they do and it works but understanding why so you can then write some thing different is another thing . Even if they are titled for Beginners the key factors that seem obvious to the person doing the tutorial are not obvious to me (lol maybe im very slow)

 

on a positive note i wanted to do some coding with buttons and i came across a small site which was doing a good job with the tutorials and explanations until i came to this ....  

 "Here is an example that uses one button to set your LED to different brightnesses."

 "uses button presses to switch an led between, off,dim,medium and high brightness"

So i set my breadboard up and copied the code and ran it. Did not work they way i figured it should in fact led stayed lit but looked like it was flashing very quickly ... so as he suggested i put a delay in at end of code and still no joy so changed delay from end to near begenning ... this made a difference and i could now see what was going on a bit better ... 

on powering up led comes on then just cycled through the dim medium high off .. if you pressed button led would stay on but looked like it was flashing fast from dim to medium. 

So this to me was not what it was ment to do ... i figured it should only change from  off to dim with a button press from dim to medium if i push button and medium to high with another button press and high to off with another button press.

But as the code compiled without a error i was at a loss to what the problem was .....

 so i went throught the process in my head of what i wanted to happen eg board checks pin if not pressed should be off ...  if pin gets pressed should light up dim and stay in that state until button gets pressed again and continue in this way until count is up ...

then went back to looking at code. First thing that didnt look right was the use of the word ledMode ... so looked this up got nothing ... figured why "ledMode" through out the code when clicking a button was what was needed to change the state of the led. decided to try buttonMode but got back a error ... looked at code again decided buttonPinMode as button was called buttonPin ... still a error until i realised before void loop there was a int ledMode =1 ... so changed that to int buttonPinMode =1 and changed all the ledMode in programm to buttonPinMode

... now realised buttoPinMode and ledMode not code words just descriptive names and anything could have being uses as long as before void loop part of program it had being named eg to check i changed buttonPin to cow and buttonPinMode to cowMode and it runs lol

was getting close but still had a problem until i realised in original program it checks to see if button is high or low and continues down through the code and then loops .... i thought it should check to see if button pressed and if low rpeat till it got pressed then run through the rest of the code

so added 

if (digitalRead(buttonPin)== LOW)

 return;

 if (digitalRead(buttonPin)==HIGH)

i wanted to check if button was low (therefore not pressed) and not sure why i put return as i dont think i used it before but hoped this would loop it back until the next part got a high button reading ... then i added a few delays and when i ran code it did what i wanted ....

1st ran programm led stayed off 

2nd pressed button led comes on Dim and stays that way 

3rd press button again and led brightens and stays that way 

4th press button again and led now High and full brightness 

5 press button again and led now turns off.

And now i cant beleive i figured out how to fix this ... going through the process in my head was the big part as i could see clearly what i beleived the programm should do ... so hopefully in future if i dont understand the code but if i can understand clearly the out come i will find a way to write the code to get it to work .... if you read this far im posting the original code and my code so you can see the differences 

first up the original 

Dimmer-Button
 Written Aug 2011 by 

Uses button presses to switch an LED between off, dim, medium, and high brightness.
*/

// constants for this sketch
const int buttonPin = 2; // pushbutton pin
const int led = 9; // LED pin

// variables for this sketch
int ledMode = 1; // variable for recording button presses

void setup()
{
// initialize the output pins:
pinMode(led, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop()
{
// check if the pushbutton is pressed.
if (digitalRead(buttonPin) == LOW)
{
// if it is, count a press
ledMode = ledMode + 1;

// if we've counted to an invalid mode 
if (ledMode == 5)
{
  // reset to mode 1
  ledMode = 1;
}

}

// Detect the current mode and set the LED appropriately
if (ledMode == 1)
{
//Mode 1 - LED is off
digitalWrite(led, LOW);
}
else if (ledMode == 2)
{
//Mode 2 - LED is dim
analogWrite(led, 64);
}
else if (ledMode == 3)
{
//Mode 3 - LED is medium
analogWrite(led, 128);
}
else
{
//Mode 4 - LED is bright
digitalWrite(led, HIGH);
}

}




const int buttonPin = 8;       // pushbutton pin
const int led = 12; // LED pin

// variables for this sketch
int buttonPinMode = 1; // variable for recording button presses

void setup()
{
// initialize the output pins:
pinMode(led, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop()
{
digitalWrite(buttonPin,LOW);

// check if the pushbutton is pressed.
if (digitalRead(buttonPin)== LOW)
return;
if (digitalRead(buttonPin)==HIGH)
{
// if it is, count a press
buttonPinMode = buttonPinMode + 1;

// if we've counted to an invalid mode 
if (buttonPinMode == 5)
{
  // reset to mode 1
  buttonPinMode = 1;
}

}

// Detect the current mode and set the LED appropriately
if (buttonPinMode == 1)
{
//Mode 1 - LED is off
digitalWrite(led, LOW);
delay(200);
}
else if (buttonPinMode == 2)
{
//Mode 2 - LED is dim
analogWrite(led, 64);
delay(200);
}
else if (buttonPinMode == 3)
{
//Mode 3 - LED is medium
analogWrite(led, 128);
delay(200);
}
else
{
//Mode 4 - LED is bright
digitalWrite(led, HIGH);
delay(200);

}

}

I have not seen a pull-up go by…

Just curious as to how you have your button connected. Are you familiar with the use of pull-up resistors? Are you using one on the breadboard? Can we get a clear in-focus picture of your set-up? --I think this might end up being an easy one.

i have a 10k resistor

i have a 10k resistor connected to one leg and going to  ground on ground rail of breadboard and one leg connected to positive rail of breadboard (3v batterypack being used) and opposite side of button diagonaly from leg connected to positive side i have leg connected to pin 8 of arduino … plus i have ground connected from breadboard to ground on arduino…

normally id post picture or video but wife gone off with my iphone lol

but as there is no problem i decided not to wait on pictures … problem was with lads code which is the first one posted … second code is my rewrite which got the button and led to do what i initally thought it was going to do using his code.

 

 

The first question I had was

button debounce? Where is it?

If you search the internet for button debounce, you will see that a button is not ON or OFF, but, OFF most of the time and only ON more or less when you press the button. I believe, adding a delay to the end of your code block for the 
if (digitalRead(buttonPin) == HIGH) {
  buttonPinMode += 1;
  if (buttonPinMode == 5) {
    buttonPinMode = 0;
  }
  delay(100); // add a delay to account for button bounce 100mS should be sufficient
}

I would also get rid of the if that checks for the Pin being LOW and the return;.

I agree with Max that learning something like flowcharting or psuedocode is very useful when programming. Your post brings up an interesting point. There are a number of tutorial sites that tell how to use this hardware and that, but, not so many that teach how to code with respect to people searching for microcontroller help.

The reason your code works when the first listing didn’t, in my opinion, is still the issue with button debounce. The if statement that checks for buttonPin LOW and returns if that is true, may be just slow enough to debounce the button presses. Your return; actually restarts the loop(), if I understand correctly. It is really bad programming practice. :stuck_out_tongue:

For future reference

inputs in arduino currently do not need to be specified. This means the only pins that require direction setting in the setup() are outputs.

ok on my code the

ok on my code the delay(100); put in that spot works great and i can get rid of

 if (digitalRead(buttonPin)==LOW)

return;

on lads code putting the delay there makes a difference but still dose not run the way he intended i also had to increase it to 800 … as led lights and cycles through the dim / medium/ bright / off  … as soon as the program starts even before i press the button … if i press button it holds and if i press within the 800 i can change it to the next state but again have to hold the button to keep it in that state … if i let button go and dont press for a second it goes back to cycling through the different states.

lol just fixed that … he has if (digitalRead(buttonPin)==LOW) …  he should have started with if (digitalRead(buttonPin)==HIGH)

now with that change and your delay added to that spot program runs fine …

thanks for that … i feel iv learned something from this …