Hello, I am new to Arduino programming and was wondering if it's possible(more likely how) to repeat a (if) statement until it becomes false. I am attaching the code.
However this is the block that I need help with. But should you want to see the full, please reference the attached.
if (buttonState == 1) {
for (int brightness = 0; brightness <= 255; brightness++) {
for (int rsBright = 3; rsBright <= 11; rsBright++) {
analogWrite(rsBright, brightness);
}
}
}
On a second though please do look at the full code. But I know the rest works.
Edit
So this is now solved but a new problem has popped up. Once I let go of the button, the loop doesn't stop. What can I do so that it stops doing the (while) loop once the button is let go of?
New code:
while (buttonValue == 1) {
for (int brightness = 0; brightness <= 255; brightness++) {
for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
analogWrite(runSet1, brightness);
delay(1);
}
}
}
Edit
So I got the time to edit the code and everything works perfectly. As well as learning many new things, I got to understand Arduino coding better.
Thank you econjack for identifying flaws and gaps in my code.
And pert, thanks for showing me the proper format for asking questions.
Here is the new code:
If anything can be approved please don't hesitate to identify it.
const int potInPut = A0; //which set to run
const int pot2InPut = A1; //brightness
static const int analogPins[] = {3, 5, 6, 9, 10, 11};
const int buttonInPut = 2;
void setup() {
Serial.begin(9600);
for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
pinMode(runSet1, OUTPUT);
}
}
void loop() {
int pot1Value = analogRead(potInPut);
int pot2Value = analogRead(pot2InPut);
int outputValue = map(pot2Value, 0, 1023, 0, 255);
int buttonValue = digitalRead(buttonInPut);
if (buttonValue == 1) { //Ok, the button is pressed
while (digitalRead(buttonValue) != 1) { //While its pressed do the following
for (int brightness = 0; brightness <= 255; brightness++) {
for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
analogWrite(runSet1, brightness);
delay(1);
}
}
}
}
if (pot1Value < 400) {
for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
analogWrite(runSet1, outputValue);
delay(100);
digitalWrite(runSet1, LOW);
delay(30);
}
} else if (pot1Value > 500) {
for (int runSet1 = 11; runSet1 >= 3; runSet1--) {
analogWrite(runSet1, outputValue);
delay(100);
digitalWrite(runSet1, LOW);
delay(30);
}
} else if (pot1Value > 400 && pot1Value < 500) {
for (int runSet1 = 3; runSet1 <= 11; runSet1++) {
digitalWrite(runSet1, LOW);
}
}
Serial.print("Case Trigger =");
Serial.print(pot1Value);
Serial.print("\t Brightness =");
Serial.print(outputValue);
Serial.print("\tButton Value =");
Serial.println(buttonValue);
delay(1);
}
SelfMade2.0.ino (1.74 KB)