Offline
Newbie
Karma: 0
Posts: 19
|
 |
« on: January 31, 2013, 03:09:28 pm » |
Have just got the arduino and trying some simple things on it, here one of them; I want to increase the brightness of the LED 20 unit each time I press the push button, quite simple. However, It doesn't work, the LED light tremble and when I push the button it stops trembling. Here's the code; int but = 8; int led = 9; int val = 0;
void setup() { pinMode(but, INPUT); pinMode(led, OUTPUT); }
void loop() { if(digitalRead(but) == 1) { val = val + 20; } analogWrite(led,val); }
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 28
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #1 on: January 31, 2013, 03:13:53 pm » |
I dont think it is actually trembling, I think it is just cycling so fast that it is hard to see anything. Look at the state change example, and debounce example, then try the LED.
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #2 on: January 31, 2013, 03:29:34 pm » |
But at least at the beginning the led must not shine, right ? However, it immediately starts trebling.
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 28
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #3 on: January 31, 2013, 03:36:08 pm » |
However, it immediately starts trebling. Even without the button being pressed? Does your button have a pull down or up resistor? if(digitalRead(but) == 1) This is looking for the button to be HIGH when pressed, so if you have a floating input (no pulling resistor), that would cause the flicker.
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #4 on: January 31, 2013, 03:47:03 pm » |
However, it immediately starts trebling. Even without the button being pressed? Does your button have a pull down or up resistor? if(digitalRead(but) == 1) This is looking for the button to be HIGH when pressed, so if you have a floating input (no pulling resistor), that would cause the flicker. The button I have is that; 
|
|
|
|
|
Logged
|
|
|
|
|
Lost Wages
Offline
Full Member
Karma: 11
Posts: 103
|
 |
« Reply #5 on: January 31, 2013, 03:49:34 pm » |
Here's my try: Connect a SPST N.O. momentary from pin 8 to GND with no resistor. Connect an LED from pin 9 to an LED in series with a current-limiting resistor (220 to 330 ohm) to GND. My code: int but = 8; int led = 9; int val = 0;
void setup() { pinMode(but, INPUT_PULLUP); pinMode(led, OUTPUT); // not needed, but I like to be explicit }
void loop() { if(digitalRead(but) == LOW) { //here: the button is pushed val += 20; // increment the brightness of the LED by 1/24th val = min(val, 255); // maximum PWM value is 255 } else { //here: the button has been released val = 0; // turn the LED OFF }
analogWrite(led,val); // adjust the PWM delay( 250 ); // give yourself a chance to see the result }
should take about 3 seconds from OFF to full brightness. If you need it to be quicker, adjust the delay time the delay also tends to debounce the switch too
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 28
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #6 on: January 31, 2013, 03:53:59 pm » |
Ok those need to be debounced to work properly. So even if you just press it once, being that it is not being debounce, it will put out a LOT of noise. Meaning even if YOU press it once, the arduino will see that it was pressed multiple times, because of that noise. Also did you include a pulling resistor?
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #7 on: January 31, 2013, 04:00:32 pm » |
Here's my try: Connect a SPST N.O. momentary from pin 8 to GND with no resistor. Connect an LED from pin 9 to an LED in series with a current-limiting resistor (220 to 330 ohm) to GND. My code: int but = 8; int led = 9; int val = 0;
void setup() { pinMode(but, INPUT_PULLUP); pinMode(led, OUTPUT); // not needed, but I like to be explicit }
void loop() { if(digitalRead(but) == LOW) { //here: the button is pushed val += 20; // increment the brightness of the LED by 1/24th val = min(val, 255); // maximum PWM value is 255 } else { //here: the button has been released val = 0; // turn the LED OFF }
analogWrite(led,val); // adjust the PWM delay( 250 ); // give yourself a chance to see the result }
should take about 3 seconds from OFF to full brightness. If you need it to be quicker, adjust the delay time the delay also tends to debounce the switch too Thank you, that one worked, but I didn't know input_pullup thing. Ok those need to be debounced to work properly. So even if you just press it once, being that it is not being debounce, it will put out a LOT of noise. Meaning even if YOU press it once, the arduino will see that it was pressed multiple times, because of that noise. Also did you include a pulling resistor?
Don't know what pulling resistor is What exactly is it ?
|
|
|
|
|
Logged
|
|
|
|
|
|
|
Lost Wages
Offline
Full Member
Karma: 11
Posts: 103
|
 |
« Reply #9 on: January 31, 2013, 04:12:13 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 28
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #10 on: January 31, 2013, 04:18:43 pm » |
A "pulling" resistor is my terminology of a resistor being either a pull down or pull up resistor. Now being that most of the time (if not all the time) we don't know what your setup is, so we assume it is being pulled down when it could be pulling up. So by saying "pulling" it means either or.
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #11 on: January 31, 2013, 04:23:22 pm » |
Here's my try: Connect a SPST N.O. momentary from pin 8 to GND with no resistor. Connect an LED from pin 9 to an LED in series with a current-limiting resistor (220 to 330 ohm) to GND. My code: int but = 8; int led = 9; int val = 0;
void setup() { pinMode(but, INPUT_PULLUP); pinMode(led, OUTPUT); // not needed, but I like to be explicit }
void loop() { if(digitalRead(but) == LOW) { //here: the button is pushed val += 20; // increment the brightness of the LED by 1/24th val = min(val, 255); // maximum PWM value is 255 } else { //here: the button has been released val = 0; // turn the LED OFF }
analogWrite(led,val); // adjust the PWM delay( 250 ); // give yourself a chance to see the result }
should take about 3 seconds from OFF to full brightness. If you need it to be quicker, adjust the delay time the delay also tends to debounce the switch too After inspected the code, I realised that you also used "LOW" for if statement with buttons. Why is it so ? I think if the button is not pushed it should be act as "LOW" and the led starts shining, but it doesn't and the code works pretty well. But didn't get the idea underlying.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #12 on: January 31, 2013, 04:25:52 pm » |
Here's my try: Connect a SPST N.O. momentary from pin 8 to GND with no resistor. Connect an LED from pin 9 to an LED in series with a current-limiting resistor (220 to 330 ohm) to GND. My code: int but = 8; int led = 9; int val = 0;
void setup() { pinMode(but, INPUT_PULLUP); pinMode(led, OUTPUT); // not needed, but I like to be explicit }
void loop() { if(digitalRead(but) == LOW) { //here: the button is pushed val += 20; // increment the brightness of the LED by 1/24th val = min(val, 255); // maximum PWM value is 255 } else { //here: the button has been released val = 0; // turn the LED OFF }
analogWrite(led,val); // adjust the PWM delay( 250 ); // give yourself a chance to see the result }
should take about 3 seconds from OFF to full brightness. If you need it to be quicker, adjust the delay time the delay also tends to debounce the switch too After inspected the code, I realised that you also used "LOW" for if statement with buttons. Why is it so ? I think if the button is not pushed it should be act as "LOW" and the led starts shining, but it doesn't and the code works pretty well. But didn't get the idea underlying. Suppose found my answer; "Because the internal pull-up on pin 2 is active and connected to 5V, we read HIGH when the button is open"
|
|
|
|
|
Logged
|
|
|
|
|
Lost Wages
Offline
Full Member
Karma: 11
Posts: 103
|
 |
« Reply #13 on: January 31, 2013, 05:07:24 pm » |
You got it!
|
|
|
|
|
Logged
|
|
|
|
|
|