Hi,
In the arduino IDE it is possible to write myVar=Serial.parseInt() to get ints from the rx/tx. How do i do the same thing in MAPLE IDE?
Thanks!
//Carl
Hi,
In the arduino IDE it is possible to write myVar=Serial.parseInt() to get ints from the rx/tx. How do i do the same thing in MAPLE IDE?
Thanks!
//Carl
1. Read the serial and parse the int. The arduino team is taking this bastardized c++ approach to io way over the edge.
2. Look at how wiring http://www.wiring.org.co approaches this. That team has its head screwed on.
You could extend the print class but it seems a little silly.
Try something like this and then figure out if you want to make it a function or not.
you could also change the delimter to while its a digit.
char delimeter=',';//or '\r' or whatever.
char c;
int i=0;
char intBuffer[24];
while (i<23&&Serial1.avaliable()&&(c=Serial1.read()!=delimeter) {
intBuffer[i++]=c;
}
intBuffer[i]=0; //end of string.
myint=atoi(intBuffer);
A few points:
0. The Serial and SerialUSB classes aren't well designed for doing this sort of thing. An extra character is always read to determine that the entire value (e.g. number) has been read, but the character can't be put back onto the input buffer (in the way ungetc for FILE is used), so it is awkward to write clean code that doesn't lose the extra characters.
1. It might or might not be appropriate to do Serialxx.available(). If the buffer happens to be full, then all of the buffered characters dealt with *before* another character arrives, then no more available()
might *not* mean the digits of the number have all been read.
2. The number could be constructed as the characters are read, without buffering them. It will silently lose significant digits, but that seems no worse than behaving as if the number has been delimited when a char array is full.
Here's a quick & dirty approach that only handles numbers composed of ASCII digits, without a leading sign:
#include <ctype.h>
long getlong() { // assumes SerialUSB is the input stream, but could be a parameter
long value = 0L;
// isdigit is likely a macro, and we don't want to risk calling
// read() twice e.g. isdigit((c = SerialUSB.read())) , so do this:
unsigned char c = SerialUSB.read();
while (isdigit(c)) {
value = value *10 + (c - '0');
c = SerialUSB.read();
}
return value;
}
(Full disclosure: I am not a member of Leaflabs staff.)
(Warning: I haven't compiled or tested that code)
Hi, thanks for the answers!
I tried your code gbulmer, and it works great. Just that some times my program "misses" to recive the data. Probably an error in the rest of my code but anyway, after some googling I desided to try with the I2C bus instead. I want to connect an olimexino (for the things in my application that needs fast performence) and an arduino (for LCD etc).
Code for olimexino:
#include <Wire.h>
int x;
void setup()
{
Wire.begin(30,29); // join i2c bus (SDA=pin 30, SCL=pin29)
}
void loop()
{
Wire.beginTransmission(4); // transmit to device #4
Wire.send("TEST1 "); // sends six bytes
Wire.send(x); // sends int
Wire.endTransmission(); // stop transmitting
x++; //same: x=x+1;
delay(500); //:P
}
code for arduino:
#include <Wire.h>
void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop()
{
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
But it does not work. Thankful for help.
//Carl
The int is not initialised(aka you didn't give it an initial value), which can cause problems.
Also, with the Arduino's receiveEvent, I'd recommend doing this:
void receiveEvent(int howMany)
{
while(Wire.available() > 0) // loop through all but the last
{
Serial.print(Wire.read()); // print the character
}
Serial.println(""); // print a new line
}
Lastly, I'd recommend using the new Wire library which has a few bug fixes over the Wire library included with IDE v0.0.12. You can download it here:
http://forums.leaflabs.com/topic.php?id=1033&page=3#post-11460
Thanks! I got it working now, I had to press the reset button on the Olimexino after I started the serial monitor. But I just realised that I have to have the arduino as master and the olimexino as slave. I tried the code
#include <HardWire.h>
#include <Wire.h>
#include <WireBase.h>
#include <LiquidCrystal.h>
TwoWire TestPort(29,30); //SCL,SDA
int datatosend=123;
void setup(){
TestPort.begin();
TestPort.onReceive(dataready);
TestPort.onRequest(datarequest);
}
void loop(){
}//loop
void dataready(int nobytes){
}
void datarequest(){
TestPort.send(datatosend);
}
but it does not compile.
Sorry for not understanding this to well, but thank you again for all the answers.
Is "TwoWire" the hardware I2C bus?
Currently both the old and the new Wire libraries do not support being a slave device or multi-master buses. You don't need to include all of the header files (.h files) if you don't need them. In this case, you only need to include <Wire.h> as it contains the software implementation of I2C, if you want to use the hardware I2C, you need specific pins. Additionally, is that code for the Arduino or the Olimexino, I'm assuming it's the Olimexino but wanted to be sure. If that's for the Arduino, the Wire library I made only supports devices which rely on libmaple (such as The Maple and Olimexino).
You must log in to post.