so I have been trying for quite a few hours now to figure out this project with very little to no progress at all. here is my task or problem statement.
Problem Statement: First program: solve this problem using only "while" loops inside the loop function. The first program will loop eight times – i.e. for eight button pushes for a button on pin #2. Connect an LED to pin #3 (PWM pin). The brightness of the LED will be adjusted using the PWM control, each time the button (pin-2) is pushed. First button push – LED goes from Off to 100% brightness. Second button push will decrease brightness to 60%. Third button push will decrease LED brightness to 30%. Fourth button push will turn Off the LED. The while loop will count eight button pushes, cycling through the LED brightness levels twice. After eight button pushes, exit the while loop and enter an infinite while loop – i.e. trap the program in an infinite loop. Second program, solve the program above (first program) using only "for" loops instead
of while loops.
Im having trouble really even getting started, the previous task I did using If statements was a lot easier.
any pointers or tips, or any kind of assistance at all would be greatly appreciated.
Yes this project is school work, would like help but not answers (like I don't want it done for me)
const int ledPin = 3;
const int buttonPin = 2;
int preState = 0;
int buttonState = 0;
int counterNumber = 0;
void setup() {
pinMode(buttonPin,INPUT_PULLUP);
pinMode(ledPin,OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
while (buttonState == HIGH && preState == 0){
analogWrite (ledPin, 255);
}
preState = 1;
buttonState = digitalRead(buttonPin);
}
I had more code but it wasn't working right so I have been attempting starting over from the beggining. right now with this code it doesn't acknowledge that i pressed a button the led is just always on.
i previously wrote a program that counted button presses using if statements, and then terminated the code using a while loop at the end, that project was pretty simple. however im confined to only using while loops in my first program and im not really familiar with how to use them at all.
Not much. Not gonna squint at a few snaps. Srsly, just draw the components, and lines between them to show what wires hook what to what.
Take a few minutes, google "schematic diagram", choose images and try you best.
It is never too early to learn, and this basic level of diagram will not take long to do.
You will find it is the way ppl share information about the hardware. And it will always be among the top 2 or 3 things those who want to help will ask for.
Besides being a good thing to have yourself before you start plugging up you circuit.
Hi,
write a part of the code and the rest you must complete.
where you have ....... is for you to continue and see if you understand the logic.
Good luck.
// https://forum.arduino.cc/t/using-while-loops-to-pwm-an-led-controlled-by-a-button/965889/5
const int ledPin = 3;
const int buttonPin = 2;
int counterNumber = 0;
//--------------------------------------------------
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
//--------------------------------------------------
void loop() {
counterNumber = 1; // Refresh my counter
while (counterNumber < 9 ) // Do 8 Times
{
if (digitalRead(buttonPin) == LOW) // Verify if button was pressed
{
delay(30); // Avoid bouncing
if (digitalRead(buttonPin) == LOW) // Verify if button stay pressed
{
counterNumber++; // Increment my counter
}
}
if (counterNumber == 1) // If my counter is 1
{
analogWrite (ledPin, 255); // Light LED full
}
}
if ( counterNumber == 2)
{
// ................
}
if ..........
{
................
}
}
In the code in this post (#5) you never update buttonState inside the while loops. The last knowledge we have of buttonState is that before the while loop was entered - and so it will stay.
Something inside the while loop must change to be able to break out.
So this is one version I had because I wasn't sure if i was allowed to use if statement in my loop. so this is what I came up with, but it only turns the led on to 100% on the first button press and doesn't change after any other button press.
const int ledPin = 3;
const int buttonPin = 2;
int counterNumber = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
counterNumber = 1;
while (counterNumber < 9)
{
if (digitalRead(buttonPin) == LOW)
{
delay(150);
if (digitalRead(buttonPin) == LOW)
{
counterNumber++;
}
}
}
if (counterNumber == 1)
{
analogWrite(ledPin, 255);
}
if (counterNumber == 2)
{
analogWrite(ledPin, 155);
}
if (counterNumber == 3)
{
analogWrite(ledPin, 76);
}
if (counterNumber == 4)
{
analogWrite(ledPin, 0);
}
if (counterNumber == 5)
{
analogWrite(ledPin, 255);
}
if (counterNumber == 6)
{
analogWrite(ledPin, 155);
}
if (counterNumber == 7)
{
analogWrite(ledPin, 76);
}
if (counterNumber == 8)
{
analogWrite(ledPin, 0);
}
}
I also tried it with the sample of code you provided, however my full code doesn't seem to light the LED at all. not sure what im doing wrong on either versions of the code.
cool I really get that I do, but that's not my current task on hand.
if it's not 'good' enough for you to provide meaningful help with, just don't respond, I don't have the time to sit down and practice my schematic diagrams as I am currently practicing this problem statement.