I wanted a simple control of voltage over USB serial, where i type in a number in the serial moniter and it sets that number to the pin, but i know that the serial is sending the ascii numbers or something... anyway here's my code.
// code for receiving a value followed by a letter
// that writes the value to an analog pin corrosponding to that letter
int analogPins[] = {3, 5, 6, 9, 10, 11}; // 'a','b','c','d','e','f'
void setup()
{
Serial.begin(9600);
}
void loop()
{
serviceSerial();
}
// serviceSerial expects data in the form:
// "255a" - analog writes 255 to pin 3
// "127c" - analog writes 127 to pin 6
// "0f" - analog writes 0 to pin 11
void serviceSerial()
{
static int val = 0;
if ( Serial.available()) {
char ch = Serial.read();
if(ch >= '0' && ch <= '9') // is ch a number?
val = val * 10 + ch - '0'; // yes, accumulate the value
else if(ch >= 'a' && ch <= 'f'){ // is ch a letter for one of the analog pins?
int pin = analogPins[ch - 'a']; // use the letter to index into the pin array
analogWrite(pin, val); // write the previously received val to the pin
}
}
}
sending a value followed by a letter writes the value to an analog pin corrosponding to the letter :
send: "255a" to analog writes 255 to pin 3
send "127c" to analog writes 127 to pin 6
send: "0f" to analog write 0 to pin 11
I have not run the code but it should get you going in the right direction.
450 is out of the legal range of 0-255 (decimal). You'll need to either add some error-checking and reject numbers greater than 255, or accept that the code will truncate the number to 8 bits.