Brightness of the LED is lowering instead of increasing

Hello.

I'm learning basics and currently I'm working on controlling the RGB module with the help of a joystick.

This is the code I'm working with :

int x = A0;
int y = A1;
int button = 2;
int blue = 3;
int green = 6;
int red = 5;

void setup ()
{
Serial.begin(9600);
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(2,INPUT_PULLUP);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(6,OUTPUT);
}

void loop () 
{
int xstate = analogRead(A0);
int ystate = analogRead(A1);
int buttonstate = digitalRead(2);
Serial.print("xstate: ");
Serial.print(xstate);
Serial.print("\t");
Serial.print("ystate: ");
Serial.print(ystate);
Serial.print("\t");
Serial.print("buttonstate: ");
Serial.println(buttonstate);
delay(400);

if (xstate > 507)
{
int bluebright = map(xstate, 507, 1023, 0, 255);
analogWrite(blue, bluebright); 
}
else if (xstate < 507)
{
int redbright = map(xstate, 0, 507, 0, 255);
analogWrite(red, redbright); 
}
else if (ystate > 511)
{ 
int greenbright = map(ystate, 511, 1023, 0, 255);
analogWrite(green, greenbright); 
}
else if (ystate < 511)
{
digitalWrite(red,HIGH);
delay(100);
digitalWrite(red,LOW);
delay(100);
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
delay(100);
digitalWrite(blue,HIGH);
delay(100);
digitalWrite(blue,LOW);
delay(100);
}
else if (buttonstate == LOW)
{
digitalWrite(red,HIGH); 
delay(50);
digitalWrite(red,LOW);
delay(50);
digitalWrite(green,HIGH);
delay(50);
digitalWrite(green,LOW);
delay(50);
digitalWrite(blue,HIGH);
delay(50);
digitalWrite(blue,LOW);
delay(50);
}
else 
{
digitalWrite(red,LOW); 
digitalWrite(green,LOW);
digitalWrite(blue,LOW);
}

}

And here's the circuit diagram :

Also, idk why but the RGB module I'm working with lights up green when connecting for red and red when connecting for green. Well, I don't mind it cause I settled with the logic.

Now the main problem is that when I move along the x axis basically ' xstate < 507 ' the red led which is supposed to glow and go to the maximum brightness does the opposite.

It starts from a dim light then to the max. Also the glow isn't as bright as others but whatever it is and when I pull the joystick backwards it glows from low to max and

in between this it sometimes won't function properly and sometimes the blinking would start which is meant for other case.

I think that's because it's too sensitive? Well keeping that aside I'm not able to figure out the opposite logic here.

I'll be glad if someone helps me out with this as I'm a complete noob.

You can use an analogWrite(red, 255 - redbright); to reverse the brightness.

Or you can try to change the mapping int redbright = map(xstate, 0, 507, 255, 0);

You also might want to rethink your logic a bit. By having all those else if statements, only 1 branch will be executed. That means if you move the x axis, it doesn't matter what the y axis is or if the button is pressed or not. Those are really independent events since it is possible to move both x and y as well as press the button. I would break them out as 3 individual if/else statements.

Thank you mate. This worked

Thanks a lot for the suggestion !!
I get your point and I'll make this change in my code and notice the efficiency.
Have a nice day.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.