I'm having problems trying to figure out what is actually occuring within the program.
The serial portion is what is confusing me. Within the loop I do not understand what the ch variable is doing and the conditions of the if (index <5 && isDigit(ch)) { I know index must be less then 5. but I dont get the isDigit(ch)
const int ledPin = 13; // LED hooked to pin 13
int blinkDelay; // blink rate is determined by this variable
char strValue[6]; // must be big enough to hold all the digits
// and the 0 that terminates the string the
int index = 0; // the index into the array storing the received digits
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if(Serial.available())
{
char ch = Serial.read();
if(index < 5 && isDigit(ch))
{
strValue[index++] = ch; // add the ASCII character to the string
}
else
{
// here when the buffer is full or on the first non digit
strValue[index] = 0; // terminate the string with a 0
blinkDelay = atoi(strValue); // use atoi to convert the string to an int
index = 0;
}
}
blink();
}
void blink()
{
digitalWrite(ledPin, HIGH);
delay(blinkDelay/2); // wait for half the blink period
digitalWrite(ledPin, LOW);
delay(blinkDelay/2); // wait for the other half
}
ch is a character variable, and holds the one character read from the serial port. The index < 5 test is making sure that there is room in the array for the character. The isDigit() test is making sure that the character is a digit, not a 'G' or a ';', for instance. Since the array is to be converted to an integer value, it wouldn't do to pass an array containing "SEVEN", would it?
Yes. That is what it considers when deciding whether to return true or false.
So, Serial.read is a number that I write into the serial monitor?
Serial.read() returns one character that you entered in the Serial Monitor application. It does not know if that character represents a numeric digit, like '3', or a letter, like 'G'. That is isDigit()s role to distinguish numbers from letters.
Nothing happens to the LED, when I do put a number of 1-5.
And hit send? What option do you have selected for the Serial Monitor application to append to every message? Something that is NOT a digit needs to be sent to signal the end of the number, and trigger the action.
What is the Arduino receiving? Serial communication is bi-directional, you know.
Simple servo code that might be of interest. A number is sent from the serial monitor as characters, captured, and converted to number for servo control.
// zoomkat 10-14-11 serial servo test
// use a microseconds value like 1500 in serial monitor
// for IDE 0022 and later
// Powering a servo from the arduino usually DOES NOT WORK.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7); //the pin for the servo control
Serial.println("servo-test-22"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); // allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
myservo.writeMicroseconds(readString.toInt()); //convert readString to number for servo
readString=""; //empty for next input
}
}
infinite4566:
I'm merely placing a 1 through 5 with no sort of trigger and hitting send.
The input isn't processed until you send a non-digit or you send the 6th character. This is assuming that you have "no line ending" selected in the Serial monitor. Try changing the drop down to "newline" and send something like "200" which is much more visible than a 2 millisecond blink.
What do you mean by what the Arduino is receiving?
He means you can use Serial.print to send data to the screen that might be helpful in debugging.
That example works great!
I'm using an Arduino Cookbook and going through it page by page to understand the c language. It; however, does not explain well what is actually happening in english within the program. Thank you very much for your help zoomkat and PaulS
He means you can use Serial.print to send data to the screen that might be helpful in debugging.
When using the serial monitor no data is sent to the Arduino until you hit return. At that point you have the option of appending various "standard" line ending characters to mark the end of the data. These are:
\r - Keyboard line endings (CR, carriage __r__eturn)
\n - Unix file line endings (LF, __n__ew line)
\r\n - Windows file line endings (CRLF)