I'm kinda new to arduino and also to C++ and im struggling with (prolly some verry easy) code.
Im trying to change the 3 different color brightnesses in a RGB led by using 3 tactile switches and 1 potmeter which only works for the color assigned to a tactile switch.
I got it all working and I can change all 3 values with the pot, however when I switch from 1 tactile switch to another the brightness of that color instantly jumps to the value of the pot.
While I would like it to only start from its "old" value, I want the potmeter to wait until the original value of the color is reached and then start working again.
I know I can better use an encoder for this, but since I wanna replace the pot by a fader soon I would really like to learn how to do this.
For those who have no idea what my problem is, im trying to achieve the pot to work like "(soft)takeover midi" as in Ableton or Traktor (DAW/ DJ software).
however when I switch from 1 tactile switch to another the brightness of that color instantly jumps to the value of the pot.
Your code makes that happen.
I want the potmeter to wait until the original value of the color is reached
Read that sentence. Think about whether or not it is even remotely possible for a potentiometer to wait. Is it even remotely possible for a potentiometer to know anything about a previous value?
Now, it IS possible for the Arduino to know about the current potentiometer reading and the current color value, and to decide whether or not to use the current value.
Think, too, about what should happen if the current blue value is 127, and you want to make it 34. What does "wait for the potentiometer to get to 127" mean, in this case? Describe exactly what you want to have happen (not even considering how you would code it).
Yes I know this should be done by code and that the potmeter doesnt know its own old value.
And yes I would like to have the arduinos code to know the value of a color and the value of the potmeter, so that when I for example:
Value red = 90, and the value from the potmeter = 120, now when I hold the tactile switch for red, the value red shoots up to 120 because the potmeter is on that value.
I need it to only start working again when the value of the pot is 90 like the value red.
So that when I press the tactile switch for red I first have to turn down the potmeter from 120 to value 90 before the pot starts adjusting the red value again.
I have a few rough ideas on how to achieve this by saving the red value as another variable but I cant seem to get it working and also I dont know if thats the way to go.
Here is my best guess at what you want. I had to use a pointer to the current color to update because without it the logic was getting convoluted and everything had to be written out three different times. Was just easier this way.
I don't have a pot or RGB out and handy to test this, so I'll leave that bit to you.
Compiles but untested:
const byte ledPinRed = 9;
const byte ledPinBlue = 10;
const byte ledPinGreen = 11;
const byte buttonPinRed = 2;
const byte buttonPinBlue = 3;
const byte buttonPinGreen = 4;
const byte potPin = A0;
byte potVal;
byte currentRed = 0;
byte currentBlue = 0;
byte currentGreen = 0;
byte* currentUpdating = ¤tRed;
byte buttonStateRed;
byte buttonStateBlue;
byte buttonStateGreen;
byte lastStateRed;
byte lastStateBlue;
byte lastStateGreen;
bool falling = false;
bool updating = false;
bool colorChange = false;
void setup(void){
pinMode(buttonPinRed, INPUT);
pinMode(buttonPinBlue, INPUT);
pinMode(buttonPinGreen, INPUT);
}
void loop(void) {
buttonStateRed = digitalRead(buttonPinRed);
buttonStateBlue = digitalRead(buttonPinBlue);
buttonStateGreen = digitalRead(buttonPinGreen);
// read the pot and divide by four to map to 0 - 255
potVal = 255 - (analogRead(potPin) >> 2);
// if a button is pressed and wasn't last pass, then we are changing the color to update
if(buttonStateRed == HIGH && lastStateRed == LOW) {
colorChange = true;
currentUpdating = ¤tRed;
}
else if(buttonStateBlue == HIGH && lastStateBlue == LOW){
colorChange = true;
currentUpdating = ¤tBlue;
}
else if(buttonStateGreen == HIGH && lastStateGreen == LOW){
colorChange = true;
currentUpdating = ¤tGreen;
}
// if we are changing colors then see if we are going up or down or are already equal
if (colorChange){
updating = false;
if(potVal > *currentUpdating){
falling = true;
}
else if(potVal < *currentUpdating){
falling = false;
}
else {
updating = true;
}
colorChange = false;
}
// if we are still waiting
if(!updating){
// if we are waiting to pass going down and have
if(falling && potVal <= *currentUpdating){
updating = true;
}
// or we are waiting to pass going up and have
else if(!falling && potVal >= *currentUpdating){
updating = true;
}
}
if(updating){
*currentUpdating = potVal;
}
analogWrite(ledPinRed, currentRed);
analogWrite(ledPinBlue, currentBlue);
analogWrite(ledPinGreen, currentGreen);
lastStateRed = buttonStateRed;
lastStateBlue = buttonStateBlue;
lastStateGreen = buttonStateGreen;
}
when the value of the pot is 90 like the value red.
So, you want to increase red from 90 to 190, and the pot is at 180. You want to turn the pot all the way to 90, with nothing happening, so that you can then turn the pot to 190, having the value of the pot again be applied to the red pin?
I'm just trying to understand your requirement.
Two more pots would certainly be a lot easier. And cheaper than an encoder.
It is possible to use a pot to increment a value as it is moved either side of centre - commonly used with joysticks which. conveniently, have a spring to make them go back to centre when you let go.
Then you have a variable holding the value for each colour and, when you switch colours the Arduino continues using the old value unless you use the joystick to change the value.
If you are using a pot that does not self centre you could write code that ignores the position of the joystick until it is first moved back to the centre.
Thanks alot Delta_G! this is almost what I was looking for, it sure does only work from the values I wanted them to work now! only thing is that with your code I control the color I last selected with the switch instead of only the color/colors im holding down the switches of. But ill get that to work!
Thanks again!
Dunno how it works on this forum but if this threads has to be closed, can be done now, im happy
TammoGrasmeijer:
Dunno how it works on this forum but if this threads has to be closed, can be done now, im happy
If you wish, you can modify your original post and edit the title to include the word "SOLVED". But please don't delete the original title as it will still be useful for other readers.