I am using Gobetwino to control Windows Media Center with my Arduino. I need help getting the volume control to work properly. I am using a slide pot and with the code I am currently using there are issues since the values the Arduino poles from the pot's pin aren't linear (1,2,3,etc.) instead they jump depending on how quickly you move the pot (1,12,23). I want to be able to take the difference between two pot readings and then have the Arduino serial print F9 or F10 (in Gobetwino speak) the a number of times equal to the difference between those two numbers (or the absolute value of the difference between those two numbers) but I do not know how to do this.
The relevant code is as follows:
const int analogInPin0 = A0;
int oldAnalogState0 = 0;
int newAnalogState0 = 0;
int volumeValue = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
newAnalogState0 = analogRead(analogInPin0);
volumeValue = map(newAnalogState0, 0, 1023, 0, 50);
adjustment = volumeValue - oldAnalogState
if(adjustment > 0)
{
//Serial.println("#S|SENDK|[0&{F10}]#") needs to be done adjustment number of times
Serial.println("#S|SENDK|[0&{F10}]#");
delay(10);
}
else if (adjustment < 0)
{
//Serial.println("#S|SENDK|[0&{F9}]#"); needs to be done the absolute value of adjustment number of times
Serial.println("#S|SENDK|[0&{F9}]#");
delay(10);
}
oldAnalogState0 = volumeValue;
}
Any help would be appreciated and please ask questions if I was not clear about anything.
I want to be able to take the difference between two pot readings and then have the Arduino serial print F9 or F10 (in Gobetwino speak) the a number of times equal to the difference between those two numbers (or the absolute value of the difference between those two numbers) but I do not know how to do this.
You have two readings, oldAnalogState0 and volumeValue.
State in the name of a variable used to store an analog value is inappropriate. Mapping that value and storing that in volumeValue is OK, but then calling the previous volume value oldAnalogState0 is not.
You need to properly compute the difference between the values (assuming you start using more reasonable names):
int diff = volumeValue - oldVolumeValue;
Then, you need to decide if the difference is positive or negative:
if(diff > 0)
{
}
else
{
}
Within each block, you need to use a for loop to send the volume up or volume down command.
for(int i=0; i<diff; i++)
{
// Send the volume up command
}
or:
diff = -diff;
for(int i=0; i<diff; i++)
{
// Send the volume down command
}
matmor:
there are issues since the values the Arduino poles from the pot's pin aren't linear (1,2,3,etc.) instead they jump depending on how quickly you move the pot (1,12,23). I want to be able to take the difference between two pot readings and then have the Arduino serial print F9 or F10 (in Gobetwino speak) the a number of times equal to the difference between those two numbers (or the absolute value of the difference between those two numbers) but I do not know how to do this.
You seem to be implying that the output from the pot is speed sensitive, but if so it isn't a pot. Could it be that you are using a logarithmic pot instead of a linear pot? They are very common for volume controls because hearing sensitivity is highly non linear. In that case you have a straight forward mapping job to do, not one involving speed detection.
You seem to be implying that the output from the pot is speed sensitive, but if so it isn't a pot.
Not true. If the potentiometer is moved a lot between one call to analogRead and the next call to analogRead, the values read will differ by more than 1, regardless of the type of potentiometer used.
PS. I think you left the word linear out of that statement.