Hey there,
I'm trying to read the RPM from an ecu using a Canbus shield by requesting the ecu the rpm and and so far it's working (I can print it to serial). What I need to do next is convert the char buffer to a number(?). I'd like to do certain functions depending on a RPM range such as 0-1000, 1001-2000, 2001-3000 rpm...I understand how to implement the range thing but what I wanted to do is something like that (but would actually work) :
#include <Canbus.h>
char buffer[64];
int relay = 9;
void setup() {
pinMode (relay, OUTPUT);
Serial.begin(9600);
Serial.println();
if (Canbus.init(CANSPEED_500))
{
Serial.println("CAN OK @ 500");
}
}
void loop() {
Canbus.ecu_req(ENGINE_RPM, buffer);
delay(100);
if ((buffer > 0) && (buffer < 1001))
{
digitalWrite(relay, HIGH) ;
}
else
{
digitalWrite(relay, LOW) ;
}
}
Something even better would be to go look for the right Canbus message in the ecu message stream without having to request it but that will be for another time. I need help to convert the char buffer so I can do something like what I want. I'd like help for the answer but also how to get there. Thank you very much!
befournier:
Hey there,
I'm trying to read the RPM from an ecu using a Canbus shield by requesting the ecu the rpm and and so far it's working (I can print it to serial). What I need to do next is convert the char buffer to a number(?). I'd like to do certain functions depending on a RPM range such as 0-1000, 1001-2000, 2001-3000 rpm...I understand how to implement the range thing but what I wanted to do is something like that (but would actually work) :
#include <Canbus.h>
char buffer[64];
int relay = 9;
void setup() {
pinMode (relay, OUTPUT);
Serial.begin(9600);
Serial.println();
if (Canbus.init(CANSPEED_500))
{
Serial.println("CAN OK @ 500");
}
}
void loop() {
Canbus.ecu_req(ENGINE_RPM, buffer);
delay(100);
if ((buffer > 0) && (buffer < 1001))
{
digitalWrite(relay, HIGH) ;
}
else
{
digitalWrite(relay, LOW) ;
}
}
Something even better would be to go look for the right Canbus message in the ecu message stream without having to request it but that will be for another time. I need help to convert the char buffer so I can do something like what I want. I'd like help for the answer but also how to get there. Thank you very much!
What is your message looked like? Remember you must null-terminated your buffer in order to use any string function.
arduino_new:
What is your message looked like? Remember you must null-terminated your buffer in order to use any string function.
When I print the buffer to the serial I get the rpm the ecu was at when I sent the command. ie : 1000 . I did try using atoi but I could not make it to work but I will retry using your link thanks.
befournier:
When I print the buffer to the serial I get the rpm the ecu was at when I sent the command. ie : 1000 . I did try using atoi but I could not make it to work but I will retry using your link thanks.
You must absolutely be sure that you null-terminated your string, printing the "correct" value does not necessary mean so. You can either send a null-terminated string or null-terminate it yourself by the receiving.
befournier:
I did try using atoi but I could not make it to work
How exactly did you try?
You need to make another variable to hold the result. Something like:
int myRPM = 0; // this is the "other variable" I mean
myRPM = atoi(buffer); // convert atoi to a number and put that number in myRPM
Serial.print("Number of RPM: ");
Serial.println(myRPM);
if (myRPM<1000)
{
Serial.println("It is slower than 1000 RPM");
}
else
{
Serial.println("It is 1000 RPM or faster");
}
In witch base are you working?
Are you working witch normal or special signs (like writing every single digit and not printing it)?
How do you transform normally a 'array of digits', called number rappresentation, into a number? Do the same. Remekver only that '0'!=0, but '1'=1+'0' ecc. So....
Sorry for the delay, thank you for all the reply!
This is an updated code :
#include <Canbus.h>
char buffer[64];
//int relay = 9;
int cht = 0;
void setup() {
cht = atoi(buffer);
// pinMode (relay, OUTPUT);
Serial.begin(9600);
//Serial.println();
if (Canbus.init(CANSPEED_500))
{
Serial.println("CAN OK @ 500");
}
}
void loop() {
Canbus.ecu_req(ENGINE_COOLANT_TEMP, buffer);
delay(100);
Serial.print ("data from pcm : ");
Serial.println (buffer);
if (cht > 132);
{
Serial.println("Cylinder head temperature is over 132c") ;
}
}
and the serial debug is
CAN OK @ 500
data from pcm : 132
Cylinder head temperature is over 132c
data from pcm : 132
Cylinder head temperature is over 132c
data from pcm : 132
Cylinder head temperature is over 132c
data from pcm : 132
Cylinder head temperature is over 132c
...
Instead of RPM, I chose a temperature sensor because I can short the cylinder head temperature sensor pin of the ecu on my bench to ground and fake a 132 degrees Celsius(it's currently shorted to gnd) for testing purposes (instead of having to start the engine). As you can see, it act as if it was over 132 Celsius so I suppose the data is not as I think it is but I can't figure out what I'm doing wrong. If I Serial.print cht I get 0. All help is really appreciated! Thanks!
Delta_G:
You keep getting new data for buffer, but the only time you ever get a value for cht is at the start of setup. So it just keeps using that same value. It isn't going to automatically change cht just because you changed what's in buffer.
Then you have a semicolon on the end of your if statement:
if (cht > 132);
So that says, if cht is greater than 132 then run an empty statement. The part after that that prints something does not depend on that if statement. An if statement only runs one statement conditionally. That statement can be a single line of code. It can be a compound statement in braces { and }. Or it can be the empty statement from the semicolon. The last option there never makes any sense.
Haha! Both are obvious! Thanks for the review. I will update, retest and report back!