Really nice of you to take the time to write this, but it outputs exactly the same result. each byte on a similar line.
I have the feeling that this function is the answer, but I can't implement it:
Really nice of you to take the time to write this, but it outputs exactly the same result. each byte on a similar line.
I have the feeling that this function is the answer, but I can't implement it:
You could search for PaulS's solution, which doesn't use String, and works.
Here, this should help you do what you need, and more importantly, LEARN more about what you are trying to do:
/*
- Example for Serial2Int
- When reading from the serial monitor, there are two important things to note:
- (1) Bytes are read one at a time. So sending "246" will be read by your
- code as '2', then '4', then '6'. If you want to identify them as related in some
- way, you need a way to determine that. This example uses start and stop bytes.
- (2) Sending a number through the monitor sends it's ASCII representation, not
- the value itself. So typing 3 and hitting enter would send '3' or 51 as per the
- ascii table. To account for this, we will be using atoi(), which takes a null
- terminated array of chars, also known as a string, and produces the int equivalent.
*/// To send a number through the serial monitor, put it between brackets
const char startByte = '<';
const char stopByte = '>';// Maximum characters in an int + null terminated character
const short maxBuffer = 6;void setup() {
Serial.begin(57600);
Serial.println("[Serial2Int]");
}void loop() {
// Stores the characters between the start and stop bytes
static char buffer[maxBuffer];
// Keeps track of spot in buffer
static short index=0;if (Serial.available() > 0 ) {
char inChar = Serial.read();if (inChar==startByte) { // If start byte is received
index=0; // then reset buffer and start fresh
} else if (inChar==stopByte) { // If stop byte is received
buffer[index] = '\0'; // then null terminate
processData(buffer); // and process the data
index=0; // this isn't necessary, but helps limit overflow
} else { // otherwise
buffer[index] = inChar; // put the character into our array
index++; // and move to the next key in the array
}/* Overflow occurs when there are more than 5 characters in between
- the start and stop bytes. This has to do with having limited space
- in our array. We chose to limit our array to 5 (+1 for null terminator)
- because an int will never be above 5 characters */
if (index>=maxBuffer) {
index=0;
Serial.println("Overflow occured, next value is unreliable");
}
}
}void processData(char buffer[]) {
unsigned int value = atoi(buffer); // convert string to int
Serial.print("Value: ");
Serial.println(value);
}
Some more discussion: Reading more than 1 byte. - #5 by system - Programming Questions - Arduino Forum. Or, you can use other people's solutions.... Who's solution worked, and where is PaulS's solution?
I want to try to do the same thing in a more simple way:
int incomingByte = 0; // for incoming serial data
boolean written = false;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}void loop() {
// send data only when you receive data:
if (Serial.available() > 0) Serial.print("I received: ");
while (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.print((char)incomingByte);
written = true;
}
if(written){
Serial.println();
written = false;
}
}
Will this work? I don't have time to try it. I'm also learning here!
P.S. If it doesn't work, first try changing Serial.print((char)incomingByte); to Serial.write(incomingByte);
Will this work?
Is this a new parlour game?
AWOL:
Will this work?
Is this a new parlour game?
If it is, it's not a very good one.
I'm not playing games. What's a parlour game?
A trivial or pointless diversion.
Are you implying something negative about my post?
20 questions now?
Generally, and for very good reasons, it is best to use code tags, and not quote tags when posting code.
I think that you are making a pointless diversion from the actual topic and questions.
I pressed "Copy for Forum". That makes the code (not quote) colorful.
And some questions are still unanswered. ![]()
If you think not using the correct posting technique is pointless, consider the following (contrived, I admit) example:
void setup ()
{
for (int s = 0; s < S_MAX; ++s) {
int pre = image;
~~ for (int i = 0; i < I_MAX ; ++i) {~~
~~ int sub = table~~
~~ ~~ [i] [u]; [color=#CC6600]for[/color] (su = 0; su < SU_MAX; ++su) { table [b] = fn (image [su][sub], 8); } } } } [/quote] and here, in plain: [code]void setup () { for (int s = 0; s < S_MAX; ++s) { int pre = image [s]; for (int i = 0; i < I_MAX ; ++i) { int sub = table [pre] [i] [u]; for (su = 0; su < SU_MAX; ++su) { table [b] = fn (image [su][sub], 8); } } } } [/code]~~ ~~
dkl65:
I pressed "Copy for Forum". That makes the code (not quote) colorful.
Yes, , we are trying to get rid of that.
Will this work? I don't have time to try it.
But you have time for half a dozen more posts on the forum? Get off the forum and try it yourself!
dkl65:
Will this work? I don't have time to try it.
When people post a question I usually try it. It takes about 10 seconds. The forum is usually a lot slower to respond than that. And people might be asleep.
I'm also learning here!
You learn by trying things. Not by asking for the answers.
You learn by trying things. Not by asking for the answers.
Yes, I try things when I have time. Now, I will go do my homework.
I will go do my homework.
And don't forget to tidy your bedroom.
And don't forget to tidy your bedroom.
No need to; I always keep it clean.
Can we stop this "parlour game" or pointless diversion now, and get back on topic? Now, I can only do Arduino on weekends. I'll wait until July, and I can do it almost every day!
dkl65:
And don't forget to tidy your bedroom.
No need to; I always keep it clean.
Can we stop this "parlour game" or pointless diversion now, and get back on topic?
There's a topic?
I like kittens.