I would like to write a simple block of code that recognizes a header byte and writes the follow on byte using serial communication from one Arduino Due to another. Can someone help me with the following non-working code that is supposed to receive a 255, val message repeatedly over serial to change the light intensity of the LED?
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
int startbyte;
int i;
int userInput[2];
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
if (Serial.available() > 0) // Wait for serial input
// Read the first byte
startbyte = Serial.read();
if (startbyte == 255) {
for (i=0;i<1;i++) {
userInput[i] = Serial.read();
val = userInput[0];
digitalWrite(ledPin, val); } // turn intensity of LED pin on according to the value it receives
}
}
}
Can someone help me with the following non-working code that is supposed to receive a 255, val message repeatedly over serial to change the light intensity of the LED?
You have asked the unanswered question of the month. I've been looking for the last month for a simple working code example that takes a string of characters sent to the arduino aerial in, the arduino captures the characters in an array, then a working string is developed from the contents of the array. found a lot of non-working code, lots of suggestions and hints, but nothing simple that actually works. In my case I want to send 1500 from the serial monitor and have a servo attached to the arduino be placed at the 1500us position.
The lineAvailable() function reads from the serial port and puts the characters into an array, as a string. It stops when it sees a carriage return or hits the limit for the number of characters. You can modify the character it stops on.
If you are just reading a single number, after you read it with lineAvailable(), you can use the atoi() function to convert it from characters into an integer.
Thanks for your response. It looks a bit complicated for just trying to read the second byte after the header byte. It looks like your solution may solve zoomkat's challenge with strings and arrays, but it seems a bit complicated for just reading the second byte after a header and writing that second byte to an LED. Any ideas on how to shorten what you wrote in the other thread?
Mike, thanks for the code link. It seems to do what I'm looking for. I changed the '\r' to '.' so I can use the serial monitor. I also moved the "boolean eol = false;" up top as it had a compile error in its origional spot. I'll next try to mix in some servo code and see what happens.
Below is what I have so far to test my servos. Type the servo position (0-180) followed by a period (.) in the serial monitor, then click send or hit enter. The servo then goes to the position sent. A period is used as a terminator as the serial monitor does not add a cr/lf when the string is sent.
// type servo position (0-180) followed by a period (.)
// in serial monitor, then click send or hit enter.
//
#define MAX_LINE 20
boolean eol = false;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup()
{
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
char line[MAX_LINE + 1]; // must be at least 1 char > MAXLINE
//do something other than waiting for serial transfer
if (lineAvailable(MAX_LINE,line))
{
Serial.write(line); // echo back the line we just read
Serial.write("\r\n");
int n;
n = atoi(line); //apparently converts string into number
myservo.write(n); // tell servo to go to position in variable 'pos'
eol = false; // get ready for another line
}
//do something other than waiting for serial transfer
}
boolean lineAvailable(int max_line,char *line)
{
int c;
static int line_idx = 0;
if (max_line <= 0) // handle bad values for max_line
{
eol = true;
if (max_line == 0)
line[0] = '\0';
}
else // valid max_line
{
if (Serial.available() > 0)
{
c = Serial.read();
if (c != -1) // got a char -- should always be true
{
//if (c == '\r') //use with terminal programs and such
if (c == '.') //use with the serial monitor
eol = true;
else
line[line_idx++] = c;
if (line_idx >= max_line)
eol = true;
line[line_idx] = '\0'; // always terminate line, even if unfinished
if (eol)
line_idx = 0; // reset for next line
}
}
}
return eol;
}