PaulRB:
Your next problem may be that you have not understood how map() and analogWrite() functions work. For example here:
int Xmin = map(analogRead(X), 0, 512, 0, 255);
If analogRead(X) is zero, then Xmin will be zero and if analogRead(X) is 512, Xmin will be 255, and for the values between, a scaled value will be calculated. Ok, great. But what if analogRead(X) is not between 0 and 512? Then Xmin will not be between 0 and 255. For example if analogRead(X) is 513, then Xmin could be 256. If analogRead(X) is 768 then Xmin will be about 384. So map does not limit the result so that it is between 0 and 255.
analogWrite(xmin, Xmin);
When Xmin is used here, strange things may happen because analogWrite() is expecting the value to be always between 0 and 255. If Xmin is 256, that will have the same result as 0. If Xmin is 384, that will be the same as 128.
Also here:
int Xmax = map(X, 513, 1023, 0, 255);
if analogRead(X) is 512, Xmax could be -1. If analogRead(X) is 200, Xmax could be around -50.
To stop analogWrite() getting a result which is not between 0 and 255, you can do this:
analogWrite(xmin, constrain(Xmin, 0, 255));
Dear Paul,
I tried replying to your entry on Friday, but apparently I forgot to hit the post button, or something else went awry. Sorry about that.
I was unable to work on it yesterday, but today I tried what you have suggested, (thanks!), and I'm starting to get confused.
I read up on INPUT PULLUP, thank you!
I had to re-wire my circuit (for transportation), and for some reason, I updated the code instead of following my previous pin assignments. So, I'm going to paste you my code once again, just to make sure we're on the same page.
int xmin=5;
int xmax=6;
int ymin=9;
int ymax=10;
int center=11;
int X=4;
int Y=3;
int SW=2;
void setup() {
// put your setup code here, to run once:
pinMode(xmin, OUTPUT);
pinMode(xmax, OUTPUT);
pinMode(ymin, OUTPUT);
pinMode(ymax, OUTPUT);
pinMode(center, OUTPUT);
pinMode(X, INPUT);
pinMode(Y, INPUT);
pinMode(SW, INPUT_PULLUP);
digitalWrite(xmin, HIGH);
digitalWrite(xmax, HIGH);
digitalWrite(ymin, HIGH);
digitalWrite(ymax, HIGH);
digitalWrite(center, HIGH);
delay(1000);
digitalWrite(xmin, LOW);
digitalWrite(xmax, LOW);
digitalWrite(ymin, LOW);
digitalWrite(ymax, LOW);
digitalWrite(center, LOW);
Serial.begin(115200);
}
void loop() {
Serial.print("Switch: ");
Serial.print(digitalRead(SW));
Serial.print("\n");
Serial.print("X-axis: ");
Serial.println(analogRead(X));
Serial.print("Y-axis: ");
Serial.println(analogRead(Y));
Serial.print("\n\n");
delay(100);
int Xmin = map(analogRead(X), 0, 512, 50, 255);
int Xmax = map(analogRead(X), 513, 1023, 50, 255);
int Ymin = map(analogRead(Y), 0, 512, 50, 255);
int Ymax = map(analogRead(Y), 513, 1023, 50, 255);
digitalWrite(xmin,constrain(Xmin,0,255));
digitalWrite(xmax,constrain(Xmax,0, 255));
digitalWrite(ymin,constrain(Ymin,0,255));
digitalWrite(ymax,constrain(Ymax,0, 255));
if (digitalRead(SW)==LOW){
digitalWrite(center, HIGH);
delay(100);
digitalWrite(center, LOW);
}
}
As you can see, I deleted my first attempt with IF conditions, to make my code more readable. I also made the thing print actual values to the Serial. I also tried messing around with the mapping function.
Now what happens is: By default, all 4 direction indicating LEDs are on, (the "center" LED is working fine), but if I move the joystick, only 2 directions make the LEDs go dark.
Also, this is clearly some mistake in my code, but what troubles me is that even the LEDs that work, only have an ON or OFF state. No dimming at all.
I am going to try to change the code, and get back to you in half an hour. All suggestions are appreciated!