const int Pin10 = 10; // the pin that the LED is attached to
void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(Pin10, OUTPUT);
}
void loop() {
byte brightness;
// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
brightness = Serial.read();
// set the brightness of the LED:
if (brightness = 0) {
analogWrite(Pin10, 0);
}
else{
analogWrite(Pin10, 255);
}
delay(10);
}
}
void loop()
{
if (Serial.available() >=6)
{
Serial.read(); // skip the letter a
analogWrite(PINA, serial.read());
Serial.read(); // skip letter b
analogWrite(PINB, serial.read());
Serial.read(); // skip letter c
analogWrite(PINC, serial.read());
}
}
a more intelligent design could be :
void loop()
{
if (Serial.available() >=2)
{
char c = Serial.read(); // skip the letter a
int val = serial.read(); // read the value byte
switch (c)
{
case 'a' : analogWrite(PINA, val); break;
case 'b' : analogWrite(PINB, val); break;
case 'c' : analogWrite(PINC, val); break;
default: break;
}
}
}
this latter would accept more complex strings
If the value is send in digits e.g. "a123b234c3" you need do something more, that is left as an exercise for you
Who/what are you responding to? Use the quote function (the button with the yellow icon next to the button with the # icon) to quote what you are referring to.
When receiving serial data to the arduino, does it always come in byte form?
Yes. Remember, though, that chars and bytes are the same size, so character data can be retrieved easily. The return type for the Serial.read() function is an int, so that the function can return a -1 to indicate that an error occurred (which it could not do if the return type was byte), not so that it can return ints (double bytes).