Can someone please explain what is going wrong in my code?
int Ledpin = 30;
int Ledpin1 = 32;
int Ledpin2 = 34;
int input = 22;
int time = 1000;
int pwm = 0;
void setup(){
pinMode(22, INPUT);
for (int i = 30; i<=35; i++){
pinMode(i,OUTPUT);
digitalWrite(i,LOW);}
}
I am trying to use a digital pin as a pwm output, and right before switching to the next pin when the pin reaches its max or minimum brightness, It will flash off or on and then continue with the code normally. Aside from this, the code is working exactly as I expected it would. Anyone know what could be causing this?
I changed your for loops because you had it going to 35 but, you only list up to pin 34 as an output pin. Maybe that is part of your issue. I also added white space and used autoformat in the Arduino IDE to make the code easier to read.
I don't understand the purpose of your code but, maybe I fixed it by working on the for loops. Test it out and see if you can better describe your problems.
int Ledpin = 30;
int Ledpin1 = 32;
int Ledpin2 = 34;
int input = 22;
int time = 1000;
int pwm = 0;
void setup()
{
pinMode(22, INPUT);
for (int i = 30; i < 34; i += 2)
{
pinMode(i,OUTPUT);
digitalWrite(i,LOW);
}
}
void loop()
{
if(digitalRead(input) == 0)
{
for (int a = 30; a < 34; a += 2)
{
for(int c = 0; c <= 1000; c += 1)
{
digitalWrite(a,HIGH);
delayMicroseconds(c*10);
digitalWrite(a,LOW);
delayMicroseconds(10000 - (c*10));
}
for(int b=0; b <= 1000; b += 1)
{
digitalWrite(a,HIGH);
delayMicroseconds(10000 -(b * 10));
digitalWrite(a,LOW);
delayMicroseconds(b * 10);
}
}
}
}