im making a project with an LED and thumbstick. i want it so that as the thumbstick goes forward as far as it can, the LED does not come on, then when the thumbstick is pulled back towards the center(513) the LED comes on and gets brighter as it reaches the center. im having trouble getting the part where as it goes forward the LED is not on and im wondering if there is a specific command to use? ive been trying if/else/while statements but ive yet to make it work, or is there a better way to use the mapping to achieve this?
int buttonPin=2;
int LEDState=0;
int LEDpin=11;
int buttonNew;
int buttonOld=1;
int light=5;
int dt=100;
int apin=A0;
int aVal;
int brightness;
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin,INPUT);
pinMode(apin,INPUT);
digitalWrite(buttonPin,HIGH);
pinMode(LEDpin,OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
aVal=analogRead(apin);
buttonNew=digitalRead(buttonPin);
Serial.print("Your button is ");
Serial.println(aVal);
delay(50);
if (aVal<513) {
brightness=map(aVal, 513, 0, 255, 0);
analogWrite(LEDpin,brightness);
}
if (aVal>=513) {
brightness=map(aVal, 513, 1023, 0, 255);
analogWrite(LEDpin,brightness);
}
}
Of course it is possible. Perhaps you think making this "is it possible" challenge will motivate the forum members to solve the problem for you? You are not the first to try this tactic.
Post your attempt to solve this problem. If the forum members are convinced that you have made a valiant attempt, you will receive all the help you need.
You are going to need a flag variable (e.g. a Boolean variable) to record whether or not the thumbstick has been pushed forward to its limit, or not. Initially it will be false. When the thumbstick exceeds a threshold slightly lower than the max reading, set the flag to true. When calculating the led brightness, if the flag is false, force the brightness to be zero, otherwise use the map() function as suggested by @paulpaulson .
This must be quite confusing for you, having 3 X Paul giving you suggestions!
I mean that was never my intent in the first place, I’m new to arduino and while I assumed it was possible my knowledge is very limited and I thought it might not work with the way I was controlling the LED, which seems to be the case as mapping is a much better alternative. I also edited the last sentence to be more specific with my question which was where that was. I didn’t think it was necessary to retype, especially after Paulrb pointed out what it might imply. But if you think it’s better to leave it in I can edit it again.
I am not asking you to change anything one way or the other, and I don't even recall what it was you said.
Just pointing out that the microcontroller can be programmed ("coded") to do many things, limited only by its memory capacity and since it operates at many millions of operations each second, it can appear to perform many different procedures "simultaneously" simply because it only spends a tiny fraction of its available time on each - the concept of "time-sharing".
Code to do this operates in a loop which contains each individual task as a set of instructions which obeys the rule that it never causes any delay, it does what in needs in a continuous pass and then falls through into the following task process and anything else which needs to be done - particularly something which is to be done at a later time - will be addressed (or at least tested) on the next pass through that section.
So asking a question as to whether the processor can perform more than one task when each task is known to be simple to perform, is clearly misguided.