But now I want to use a POT to control it. But I've got stuck because I'm not sure how to convert the POT reading from a INT into a BYTE. Does anyone have any ideas? Do I have to create an array with all the possible BYTEs or is there an easier way to convert between INTs and BYTES?
Here's my code:
Serial1.print("VSV ");
Serial1.print(0x0A,BYTE); // set the voloume. Needs to be a value between 0x00 and 0xFE (0x00 being high)
Serial1.print(0x0D,BYTE);
Your code sample shows "VSV " "carriage-return/line-feed", equivalent to
"Serial1.println ("VSV ");
does mean it's wrong? Sorry am a bit confused as the code I entered works but I don't know how to convert the POT reading which would be an Interger into a BYTE, maybe I'm missing something simple.
I just need to be able to change this: 0x0A,BYTE
from my Pot reading
I used that because I thought it translated to 10 in decimal?
Sorry for the confusion but I'm still stuck as to how to insert a byte that's based from a INT from the pot. It doesn't seem to work if I just send it 10 but does if I send it as a BYTE equivalent.
Explanation: "analogRead" returns a value 0..1023, but you need a value 0..255.
HOWEVER: the manual seems to suggest that the louder you expect the thingy to be, the lower you should specify the value.
So, depends how you've got your pot wired whether you use:
byte volumeVal = map (potVal, 0, 1023, 0, 255);
or
byte volumeVal = map (potVal, 0, 1023, 255, 0);
// ------------------------------------------------------------
// readVolumePot - reads the potentiometer for volume
// ------------------------------------------------------------
void readVolumePot(){
potVal = analogRead(0); // read the value from the sensor
// maps the pot value to between 0 and 90.
// The manual says the volume settings are between 0 and 254.
// Where 0 sets the volume to it's highest.
// But I found that the best range is between 0 - 90
byte potReading = map (potVal, 0, 1023, 90, 0);
if (potReading != lastPotReading){
setVolume(potReading);
lastPotReading = potReading;
Serial.println("Setting volume");
}
}
// ------------------------------------------------------------
// setVolume - sets VMUSIC's volume based on readVolumePot
// ------------------------------------------------------------
void setVolume(byte volumeVal){
// This is the command that needs to be sent to VMUSIC
Serial1.print("VSV ");
Serial1.print(volumeVal,BYTE);
Serial1.print(0x0D,BYTE);
Serial.println("Volume has been set");
}
It's using a Arduino Mega so you need to change the Serial settings if you want to use it on anything else
I used that because I thought it translated to 10 in decimal?
So, it doesn't go to eleven then?
Sorry for the misunderstanding - after very many years in this game, I see 0x0d and 0x0a in close proximity, and I just see CR/LF (or in this case, LF/CR!)