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 byUses 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 pressesvoid 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);
}
}