Hello.
I have got a problem with SoftwareSerial (v2.1 haven't found newer..). I am reading in bluetooth MAC-adresses on my Uno and for debugging I print them by Serial to my computer.
I found a big issue, It wipes all zeroes after : eg. if a recieved MAC looks for example like this :0012:A5:00acde, I recive 12:a5:acde in the PC-screen.
If the MAC looks like :af:33:112233 everything works and I see af:33:112233 in the PC.
nobody here as glas-sphere to look into it to see your code.
You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps
- press Ctrl-T for autoformatting your code
- do a rightclick with the mouse and choose "copy for forum"
- paste clipboard into write-window of a posting
best regards Stefan
Post your code so that we can see what you are doing
The easier you make it to read and copy your code the more likely it is that you will get help
Please follow the advice given in the link below when posting code , use code tags and post the code here
If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags
//BOOTLOAD BURNING:
//_Atmega 328P on breadboard
//_Reset pullup 10k
//_Arduino as ISP
//***********************************************//
//******Car Engine RPM and Shift Light***********//
//******with Arduino, HC-05 Bluetooth Module*****/
#include <Timer.h>
#include <EEPROM.h>
#include <SoftwareSerial.h>
#define mac_save 20
#define DEBUG
#define AUTO
#define testtime 250
#define RxD 7 //Arduino pin connected to Tx of HC-05
#define TxD 8 //Arduino pin connected to Rx of HC-05
#define Reset 9 //Arduino pin connected to Reset of HC-05 (reset with LOW)
#define PIO11 10 //Arduino pin connected to PI011 of HC-05 (enter AT Mode with HIGH)
#define ledpin_green A0 //Arduino output pin for Shift Light Green led
#define ledpin_yellow A1 //Arduino output pin for Shift Light Yellow led
#define ledpin_red 13 //Arduino output pin for Shift Light Red led
#define led_dec A2 //Arduino output pin for decades 7-segment commone anode display
#define led_mon 11 //Arduino output pin for monades 7-segment commone anode display
#define sel_sw 12 //Arduino input for storing curent Shift Light RPM
#define BT_CMD_RETRIES 5 //Number of retries for each Bluetooth AT command in case of not responde with OK
#define OBD_CMD_RETRIES 3 //Number of retries for each OBD command in case of not receive prompt '>' char
#define RPM_CMD_RETRIES 5 //Number of retries for RPM command
int BinaryPins[] = {3, 4, 5, 6}; //Arduino Pins connected to 74LS47 BCD-to-7-Segment
//A,B,C,D// A is LSB
//char mac_read[16];
//char temp[10];
int obdpaus = 600;
int paus = 1000;
char str[35];
char dest[40];
char buf1[10];
char MAC[15]; //original 16
int STR_lenght;
int curr_rpm;
int addr = 0; //EEPROM address for storing Shift Light RPM
unsigned int rpm, rpm_to_disp; //Variables for RPM
int shift_light_rpm ; //Variable for Shift Light RPM
unsigned int decades; //Variable of RPM number decades for 7-seg disp
unsigned int monades; //Variable of RPM number monades for 7-seg disp
boolean but_pressed_flag; //Variable if RPM Shift Light button is pressed
boolean bt_error_flag; //Variable for bluetooth connection error
boolean obd_error_flag; //Variable for OBD connection error
boolean rpm_error_flag; //Variable for RPM error
boolean rpm_retries; //Variable for RPM cmd retries
boolean Saved_MAC = true;
int disp_bright; //Variable for 7-seg disp brightness
SoftwareSerial blueToothSerial(RxD, TxD);
Timer t;
//-----------------------------------------------------//
void setup()
{
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
pinMode(PIO11, OUTPUT);
pinMode(Reset, OUTPUT);
digitalWrite(PIO11, LOW); //Set HC-05 to Com mode
digitalWrite(Reset, HIGH); //HC-05 no Reset
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(ledpin_green, OUTPUT);
pinMode(ledpin_yellow, OUTPUT);
pinMode(ledpin_red, OUTPUT);
pinMode(led_dec, OUTPUT);
pinMode(led_mon, OUTPUT);
pinMode(sel_sw, INPUT);
pinMode(sel_sw, INPUT_PULLUP);
#ifdef DEBUG
Serial.begin(57600);
// EEPROM.write(addr,15); //Set rpm_light to 1500rpm for test
#endif
//Read Stored Shift Light RPM//
shift_light_rpm = EEPROM.read(addr); //Read EEPROM for stored value
EEPROM.get(mac_save, MAC);
if (!digitalRead(sel_sw))
{
EEPROM.put(mac_save, " ");
Saved_MAC = false;
}
#ifdef DEBUG
Serial.print("Sparat MAC ");
Serial.println(MAC);
Serial.print("Sparat Varvtal ");
Serial.println(shift_light_rpm * 100);
#endif
rpm_retries = 0;
but_pressed_flag = false;
disp_bright = 0; //Full Bright
obd_power_on();
if ((shift_light_rpm < 0) or (shift_light_rpm > 99))
{
shift_light_rpm = 0; //if value not correct set it to 0
int a = 35;
while (a > 0)
{
#ifdef DEBUG
Serial.println("RPM lost ");
#endif
digitalWrite(ledpin_red, HIGH);
delay(100);
digitalWrite(ledpin_red, LOW);
delay(100);
//delay(1200);
a--;
}
}
t.every(testtime, rpm_calc); //Every 250ms read RPM value from OBD
//test button
setupBlueToothConnection();
//in case of Bluetoth connection error
if (bt_error_flag)
{
#ifdef DEBUG
Serial.print("error ");
#endif
bt_err_flash();
}
#ifdef DEBUG
Serial.print("Connected");
// blueToothSerial.print("010C1");
#endif
//OBDII initialitation
obd_init();
//in case of OBDII connection error
if (obd_error_flag)
{
obd_err_flash();
}
}
//-----------------------------------------------------//
//---------------flashes slow red light----------------//
//---------Showing power on-----------//
void obd_power_on() {
int b = 2;
while (b > 0)
{
#ifdef DEBUG
Serial.println("power on" );
#endif
digitalWrite(ledpin_red, HIGH);
delay(1000);
digitalWrite(ledpin_red, LOW);
delay(500);
b--;
}
}
//-----------------------------------------------------//
//---------------flashes fast red light----------------//
//-------in case of Bluetooth connection error---------//
//------loops for ever, need to restart Arduino--------//
void bt_err_flash() {
while (1) {
digitalWrite(ledpin_red, HIGH);
delay(200);
digitalWrite(ledpin_red, LOW);
delay(200);
}
}
//-----------------------------------------------------//
//---------------flashes slow red light----------------//
//---------in case of OBDII connection error-----------//
//------loops for ever, need to restart Arduino--------//
void obd_err_flash() {
while (1) {
digitalWrite(ledpin_red, HIGH);
delay(1000);
digitalWrite(ledpin_red, LOW);
delay(1000);
}
}
//-----------------------------------------------------//
//----------Decades 7-seg disp driver------------------//
void sevenSegWriteDec(int digit) {
if (digit > 0) analogWrite(led_dec, disp_bright); else digitalWrite(led_dec, HIGH); //if value is 0 stays OFF
digitalWrite(led_mon, HIGH); //monades 7-seg disp OFF
//convert dec number to 4 bits binary
for (byte i = 0; i < 4; i++) {
int dec_val = digit & 1; //interested only for LSB of digit
digitalWrite(BinaryPins[i], dec_val); //set it to BinaryPin i=0 is LSB and goes to pin A of 74LS47 BCD-to-7-Segment
digit = digit >> 1; //shift right digit 1 bit
}
}
//-----------------------------------------------------//
//----------Monades 7-seg disp driver------------------//
void sevenSegWriteMon(int digit) {
digitalWrite(led_dec, HIGH); //decades 7-seg disp OFF
analogWrite(led_mon, disp_bright); //monades 7-seg disp ON
//convert dec number to 4 bits binary
for (byte i = 0; i < 4; i++) {
int mon_val = digit & 1; //interested only for LSB of digit
digitalWrite(BinaryPins[i], mon_val); //set it to BinaryPin i=0 is LSB and goes to pin A of 74LS47 BCD-to-7-Segment
digit = digit >> 1; //shift right digit 1 bit
}
}
//-----------------------------------------------------//
//----------display stored shift light RPM-------------//
//-----------------------for 3 secs--------------------//
void flash_store(int num) {
unsigned long start = millis(); //set start as current millis
unsigned long dlay = 3000UL; //set delay to 3000ms
while (millis() < (start + dlay)) //for 3 secs
{
//display num
sevenSegWriteDec(num / 10);
delay(10);
sevenSegWriteMon(num % 10);
delay(10);
//turn red led ON
digitalWrite(ledpin_red, HIGH);
digitalWrite(ledpin_green, LOW);
digitalWrite(ledpin_yellow, LOW);
}
}
//-----------------------------------------------------//
//----------Retrieve RPM value from OBD----------------//
//---------convert it to readable number---------------//
void rpm_calc() {
boolean prompt, valid;
char recvChar;
char bufin[15];
int i;
if (!obd_error_flag) { //if no OBD connection error
#ifdef DEBUG
Serial.println ("OBD read");
#endif
valid = false;
prompt = false;
blueToothSerial.print("010C1"); //send to OBD PID command 010C is for RPM, the last 1 is for ELM to wait just for 1 respond (see ELM datasheet)
blueToothSerial.print("\r"); //send to OBD cariage return char
while (blueToothSerial.available() <= 0); //wait while no data from ELM
i = 0;
while ((blueToothSerial.available() > 0) && (!prompt)) { //if there is data from ELM and prompt is false
recvChar = blueToothSerial.read(); //read from ELM
if ((i < 15) && (!(recvChar == 32))) //the normal respond to previus command is 010C1/r41 0C ?? ??>, so count 15 chars and ignore char 32 which is space
{
bufin[i] = recvChar; //put received char in bufin array
i = i + 1; //increase i
}
if (recvChar == 62) prompt = true; //if received char is 62 which is '>' then prompt is true, which means that ELM response is finished
}
if ((bufin[6] == '4') && (bufin[7] == '1') && (bufin[8] == '0') && (bufin[9] == 'C')) { //if first four chars after our command is 410C
valid = true; //then we have a correct RPM response
} else {
valid = false; //else we dont
}
if (valid) { //in case of correct RPM response
rpm_retries = 0; //reset to 0 retries
rpm_error_flag = false; //set rpm error flag to false
//start calculation of real RPM value
//RPM is coming from OBD in two 8bit(bytes) hex numbers for example A=0B and B=6C
//the equation is ((A * 256) + B) / 4, so 0B=11 and 6C=108
//so rpm=((11 * 256) + 108) / 4 = 731 a normal idle car engine rpm
rpm = 0;
for (i = 10; i < 14; i++) { //in that 4 chars of bufin array which is the RPM value
if ((bufin[i] >= 'A') && (bufin[i] <= 'F')) { //if char is between 'A' and 'F'
bufin[i] -= 55; //'A' is int 65 minus 55 gives 10 which is int value for hex A
}
if ((bufin[i] >= '0') && (bufin[i] <= '9')) { //if char is between '0' and '9'
bufin[i] -= 48; //'0' is int 48 minus 48 gives 0 same as hex
}
rpm = (rpm << 4) | (bufin[i] & 0xf); //shift left rpm 4 bits and add the 4 bits of new char
}
rpm = rpm >> 2; //finaly shift right rpm 2 bits, rpm=rpm/4
}
}
if (!valid) { //in case of incorrect RPM response
rpm_error_flag = true; //set rpm error flag to true
rpm_retries += 1; //add 1 retry
rpm = 0; //set rpm to 0
#ifdef DEBUG
Serial.println("RPM_ERROR");
#endif
if (rpm_retries >= RPM_CMD_RETRIES) obd_error_flag = true; //if retries reached RPM_CMD_RETRIES limit then set obd error flag to true
}
}
//----------------------------------------------------------//
//---------------------Send OBD Command---------------------//
//--------------------for initialitation--------------------//
void send_OBD_cmd(char *obd_cmd) {
char recvChar;
boolean prompt;
int retries;
if (!(obd_error_flag)) { //if no OBD connection error
#ifdef DEBUG
Serial.println(" OBD connect ");
Serial.print("SPARAD MAC ");
Serial.println(MAC);
#endif
prompt = false;
retries = 0;
while ((!prompt) && (retries < OBD_CMD_RETRIES)) { //while no prompt and not reached OBD cmd retries
blueToothSerial.print(obd_cmd); //send OBD cmd
blueToothSerial.print("\r"); //send cariage return
#ifdef DEBUG
Serial.print ("Retry OBD ");
Serial.println(retries);
#endif
while (blueToothSerial.available() <= 0); //wait while no data from ELM
while ((blueToothSerial.available() > 0) && (!prompt)) { //while there is data and not prompt
recvChar = blueToothSerial.read(); //read from elm
#ifdef DEBUG
Serial.write(recvChar);
#endif
if (recvChar == 62) prompt = true; //if received char is '>' then prompt is true
}
retries = retries + 1; //increase retries
delay(2000);
}
if (retries >= OBD_CMD_RETRIES) { // if OBD cmd retries reached
obd_error_flag = true; // obd error flag is true
#ifdef DEBUG
Serial.println(" OBD not found ");
#endif
}
}
}
//----------------------------------------------------------//
//----------------initialitation of OBDII-------------------//
void obd_init() {
digitalWrite(ledpin_red, HIGH);
obd_error_flag = false; // obd error flag is false
send_OBD_cmd("ATZ"); //send to OBD ATZ, reset
delay(obdpaus);
digitalWrite(ledpin_red, !digitalRead(ledpin_red));
send_OBD_cmd("ATSP0"); //send ATSP0, protocol auto ATEO?
// send_OBD_cmd("ATE0");
delay(obdpaus);
digitalWrite(ledpin_red, !digitalRead(ledpin_red));
send_OBD_cmd("0100"); //send 0100, retrieve available pid's 00-19
delay(obdpaus);
digitalWrite(ledpin_red, !digitalRead(ledpin_red));
send_OBD_cmd("0120"); //send 0120, retrieve available pid's 20-39
delay(obdpaus);
digitalWrite(ledpin_red, !digitalRead(ledpin_red));
send_OBD_cmd("0140"); //send 0140, retrieve available pid's 40-??
delay(obdpaus);
digitalWrite(ledpin_red, !digitalRead(ledpin_red));
send_OBD_cmd("010C1"); //send 010C1, RPM cmd
delay(obdpaus);
digitalWrite(ledpin_red, !digitalRead(ledpin_red));
digitalWrite(ledpin_red, LOW);
}
//----------------------------------------------------------//
//-----------start of bluetooth connection------------------//
void setupBlueToothConnection()
{
Serial.print("SPARAD MAC ");
Serial.println(MAC);
#ifndef AUTO
bt_error_flag = false; //set bluetooth error flag to false
enterATMode(); //enter HC-05 AT mode
delay(500);
sendATCommand("RESET"); //send to HC-05 RESET
delay(600);
sendATCommand("PSWD=1234");
sendATCommand("ORGL"); //send ORGL, reset to original properties
sendATCommand("ROLE=1"); //send ROLE=1, set role to master
sendATCommand("CMODE=0"); //send CMODE=0, set connection mode to specific address
//--------------------------------for memory----------------------------------------
sendATCommand("BIND=AABB,CC,112233"); //OBDII
//-------------------------------------------------PROBLEM--------------
// //ELM327 Sends 001D:A5:991951 =>1DA5,99,1951
// UM25C 0015:A3:003FDA =>15,A3,3FDA
//---------------------------------------------------------------------
//sendATCommand("BIND=49C1,B2,A18250");
//sendATCommand("BIND=6C70,F0,E53C5C");
// sendATCommand("BIND=98D3,33,80AE4D"); //UNO
//sendATCommand("BIND=7514,3B,084E2F");
// sendATCommand("BIND=1122,33,DDEEFF"); //send BIND=??, bind HC-05 to OBD bluetooth address
sendATCommand("INIT"); //send INIT, cant connect without this cmd
delay(1000);
sendATCommand("PAIR=AABB,CC,112233,20"); //OBDII
//sendATCommand("PAIR=49C1,B2,A18250,20");
//sendATCommand("PAIR=6C70,F0,E53C5C,20");
//sendATCommand("PAIR=98D3,33,80AE4D,20"); //UNO
//sendATCommand("PAIR=7514,3B,084E2F");
//sendATCommand("PAIR=1122,33,DDEEFF,20"); //send PAIR, pair with OBD address
delay(1000);
//
//sendATCommand("PSWD=1234");
delay(1000);
sendATCommand("LINK=AABB,CC,112233"); //OBDII
//sendATCommand("LINK=49C1,B2,A18250");
//sendATCommand("LINK=6C70,F0,E53C5C");
//sendATCommand("LINK=98D3,33,80AE4D"); //UNO
//sendATCommand("LINK=7514,3B,084E2F");
//sendATCommand("LINK=1122,33,DDEEFF"); //send LINK, link with OBD address
delay(1000);
// sendATCommand("rname=?");
//sendATCommand("PSWD=1234");
delay (1000);
enterComMode(); //enter HC-05 comunication mode
delay(500);
//}
#endif
//-------------------------------------------------Auto config O B D test---------------------------------------------
#ifdef AUTO
//----------------------------------------------------------//
//-----------start of bluetooth connection------------------//
//void setupBlueToothConnection()
//{
bt_error_flag = false; //set bluetooth error flag to false
if (!Saved_MAC)
{
enterATMode(); //enter HC-05 AT mode
delay(500);
//{
MAC_ID();
//}
//sendATCommand("CMODE=1");
convert("BIND=");
sendATCommand(dest);
convert("PAIR=");
strcat(dest, ",20");
sendATCommand(dest);
//sendATCommand("CMODE=1");
//}
convert("LINK=");
sendATCommand(dest);
}
/*}
SendATCommand("
convert("BIND=");
sendATCommand(dest); */
delay (1000);
enterComMode(); //enter HC-05 comunication mode
delay(500);
//}
#endif
//-------------------------------------------Auto end-----------------------------------------------------------------------------------
}
void convert( char *command)
{
#ifdef DEBUG
Serial.println (command);
#endif
strcpy(dest, command);
strcat(dest, MAC);
paus = 1000;
#ifdef DEBUG
Serial.print("buffertest convert ");
Serial.println(dest);
#endif
}
void MAC_ID()
{
sendATCommand("RMAAD");
//delay(1000)
sendATCommand("PSWD=0000");
sendATCommand("ROLE=1");
sendATCommand("RESET"); //send to HC-05 RESET
//sendATCommand("ROLE=1");
sendATCommand("CMODE=0"); //send CMODE=0, set connection mode to specific address
sendATCommand("INQM=0,5,9");
sendATCommand("INIT");
delay(paus);
sendATCommand("INQ");
delay(paus);
MAC[4] = (',');
MAC[7] = (',');
#ifdef DEBUG
for (int i = 0; i <= STR_lenght; i++)
{
Serial.print(i);
Serial.print(" : ");
Serial.println(MAC[i]);
}
Serial.print("Totalt ");
Serial.println(MAC);
#endif
// UNO 98D3,33,80AE4D
char buf1[10] = "RNAME?";
strcpy(dest, buf1);
//strcat(dest, "; ");
strcat(dest, MAC);
paus = 1000;
#ifdef DEBUG
Serial.print("buffertest uno ");
Serial.println (buf1);
#endif
sendATCommand(dest);
if ((str[7] == 'o') && (str[8] == 'b') && (str[9] == 'd') or ((str[7] == 'O') && (str[8] == 'B') && (str[9] == 'D')) or ((str[7] == 'U') && (str[8] == 'n') && (str[9] == 'o')) or ((str[7] == 'U') && (str[8] == 'N') && (str[9] == 'O')))
{
#ifdef DEBUG
Serial.print("MAC OK ");
Serial.print(MAC);
#endif
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
EEPROM.put(mac_save, MAC);
Saved_MAC = true;
}
}
//----------------------------------------------------------//
//------------------reset of HC-05--------------------------//
//-------set reset pin of HC-05 to LOW for 2 secs-----------//
void resetBT()
{
digitalWrite(Reset, LOW);
delay(2000);
digitalWrite(Reset, HIGH);
}
//----------------------------------------------------------//
//--------Enter HC-05 bluetooth moduel command mode---------//
//-------------set HC-05 mode pin to LOW--------------------//
void enterComMode()
{
blueToothSerial.flush();
delay(500);
digitalWrite(PIO11, LOW);
resetBT(); //
delay(500);
blueToothSerial.begin(38400); //default communication baud rate of HC-05 is 38400
}
//----------------------------------------------------------//
//----------Enter HC-05 bluetooth moduel AT mode------------//
//-------------set HC-05 mode pin to HIGH--------------------//
void enterATMode()
{
blueToothSerial.flush();
delay(500);
digitalWrite(PIO11, HIGH);
resetBT();
// delay(500);
//digitalWrite(PIO11,LOW);
delay(500);
blueToothSerial.begin(38400);//HC-05 AT mode baud rate is 38400
}
//----------------------------------------------------------//
void sendATCommand(char *command)
{
digitalWrite(ledpin_red, HIGH);
char recvChar, extra;
int h, l;
int i, retries;
int r = 5;
boolean OK_flag;
if (!(bt_error_flag)) { //if no bluetooth connection error
retries = 0;
OK_flag = false;
while ((retries < BT_CMD_RETRIES) && (!(OK_flag))) { //while not OK and bluetooth cmd retries not reached
blueToothSerial.print("AT"); //sent AT cmd to HC-05
if (strlen(command) > 1) {
blueToothSerial.print("+");
blueToothSerial.print(command);
}
blueToothSerial.print("\r\n");
#ifdef DEBUG
Serial.println(command);
#endif
while (blueToothSerial.available() <= 0); //wait while no data
i = 0;
while (blueToothSerial.available() > 0) // while data is available
{
recvChar = blueToothSerial.read(); //read data from HC-05
#ifdef DEBUG
Serial.print(i);
Serial.print(" recieved ");
Serial.println(recvChar);
#endif //////_________________________________Referens____________________________________________________________
if ((command == "INQ") && (i >= 5) && (i <= 18))
{
if ((i == 9 && recvChar != (':')) || (i == 12 && recvChar != (':'))) //
Serial.println("wrong adress ");
MAC[i - 5] = recvChar;
}
//if (i<2){
str[i] = recvChar; //put received char to str
//i=i+1;
i = i + 1;
if ((command == "INQ") && (i == 19))
{
//#ifdef DEBUG
Serial.print("Recieved address: ");
Serial.println(MAC);
//#endif
}
if (str[0] == '+')
delay(500);
}
STR_lenght = i;
#ifdef DEBUG
//Serial.print("Recieved address: ");
//Serial.println(MAC);
delay(100);
Serial.print(" Lenght ");
Serial.println(STR_lenght);
Serial.println(str[STR_lenght - 4]);
Serial.println(str[STR_lenght - 3]);
#endif
retries = retries + 1; //increase retries
if (((str[0] == 'O') && (str[1] == 'K')) or ( (str[STR_lenght - 4] == 'O') && (str[STR_lenght - 3] == 'K'))) //if response is OK then OK-flag set to true
{
OK_flag = true;
#ifdef DEBUG
Serial.println(" OK recieved ");
#endif
}
if (command == "PAIR");
{
if ((str[0] == 'F') && (str[1] == 'A') && (str[2] == 'I') && (str[3] == 'L'))
{
#ifdef DEBUG
Serial.println("wrongl pin");
#endif
sendATCommand("PSWD=1234");
}
}
delay(1000);
}
if (retries >= BT_CMD_RETRIES) { //if bluetooth retries reached
bt_error_flag = true; //set bluetooth error flag to true
}
}
digitalWrite(ledpin_red, LOW);
}
//----------------------------------------------------------//
void loop() {
while (!(obd_error_flag)) { //while no OBD comunication error
if ((rpm >= 0) && (rpm < 10000)) { //if rpm value is between 0 and 10000
rpm_to_disp = int(rpm / 100); //devide by 100, cause we have only two 7-seg disps
decades = rpm_to_disp / 10; //calculate decades
monades = rpm_to_disp % 10; //calculate monades
#ifdef DEBUG
if (rpm != curr_rpm)
{
Serial.print (" Varvtal ");
Serial.println (rpm);
Serial.print(" Shift ");
Serial.println(shift_light_rpm * 100);
curr_rpm = rpm;
}
#endif
sevenSegWriteDec(decades); //display decades to decades 7-seg disp
delay(10);
sevenSegWriteMon(monades); //display monades to monades 7-seg disp
delay(10);
if (shift_light_rpm > 0) { //if shift light rpm is >0
if (rpm_to_disp >= shift_light_rpm - 10) digitalWrite(ledpin_green, HIGH); else digitalWrite(ledpin_green, LOW); //green led on -1000rpms
if (rpm_to_disp >= shift_light_rpm - 5) digitalWrite(ledpin_yellow, HIGH); else digitalWrite(ledpin_yellow, LOW); //yellow led on -500rpms
if (rpm_to_disp >= shift_light_rpm)
{
#ifdef DEBUG
Serial.println("Shift UP");
#endif
digitalWrite(ledpin_red, HIGH);
}
else digitalWrite(ledpin_red, LOW); //red led on stored shift light rpm value
}
else digitalWrite(ledpin_green, HIGH); //if no value is stored in EEPROM
if (rpm_error_flag) { //if rpm error yellow led ON
digitalWrite(ledpin_green, LOW);
digitalWrite(ledpin_yellow, HIGH);
digitalWrite(ledpin_red, LOW);
}
//if button pressed then store current rpm to EEPROM
if ((!digitalRead(sel_sw)) && (!but_pressed_flag)) {
but_pressed_flag = true;
shift_light_rpm = rpm_to_disp;
EEPROM.write(addr, shift_light_rpm);
flash_store(shift_light_rpm);
#ifdef DEBUG
Serial.print("Sparat i EEPROM ");
Serial.print (shift_light_rpm);
Serial.println(" RPM");
#endif
}
if (!(digitalRead(sel_sw))) but_pressed_flag = false;
}
else //if no correct rpm value received
{
digitalWrite(led_dec, HIGH);
digitalWrite(led_mon, HIGH);
digitalWrite(ledpin_red, LOW);
digitalWrite(ledpin_green, LOW);
digitalWrite(ledpin_yellow, LOW);
}
t.update(); //update of timer for calling rpm_calc
}
if (obd_error_flag) obd_err_flash(); //if OBD error flag is true
}
//-----------------------------------------------------//
//-------Just a demo-test for 7-seg disp and leds------//
void demo() {
//Turn 7-seg disp OFF
digitalWrite(led_dec, HIGH);
digitalWrite(led_mon, HIGH);
//Shift Light LEDS Test
for (int i = 0; i < 3; i++) {
digitalWrite(ledpin_green, HIGH);
digitalWrite(ledpin_yellow, HIGH);
digitalWrite(ledpin_red, HIGH);
delay(50);
digitalWrite(ledpin_green, LOW);
digitalWrite(ledpin_yellow, LOW);
digitalWrite(ledpin_red, LOW);
delay(50);
}
//7-seg diplays test
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
sevenSegWriteDec(8);
delay(10);
sevenSegWriteMon(8);
delay(10);
}
//Turn 7-seg disp OFF
digitalWrite(led_dec, HIGH);
digitalWrite(led_mon, HIGH);
delay(50);
}
}
This is fully working program, uncleaned, under developement, but elm327 and UM25c don't work by earlier mentioned reasons.
You are working on an informatic project and what is needed most in informatic projects is informtation:
software-serial works only reliable on rather slow baudrates on atmel chips
your codes says
Atmega 328P on breadboard
-
what frequency?
-
What oscillator?
-
Can you give some hints where in your code you are sending the mac-adresses?
The more informational details you provide the faster you will receive help
That is the reason why this is important to read
best regards Stefan
I’m on my smartphone so hard to read your code but this seems it could be an issue of printing hex data where leading 0 are not shown if you use Serial.print(x, HEX);
hello.
breadboard drives on internal 8MHz, But it doesn't matter if I use an UNO, NANO or breadboard. I have gone through all bluetooth devices I found at home, and all that don't have zero after : in the address works.
It erases all zeroes until it finds another character.
Reg. Joakim
SoftwareSerial has nothing to do with the missing zeroes. Try this
void setup()
{
Serial.begin(115200);
byte x = 0x08;
Serial.println(x);
}
void loop()
{
}
then try this
void setup()
{
Serial.begin(115200);
byte x = 0x08;
if (x < 0x10)
{
Serial.print(0);
}
Serial.println(x);
}
void loop()
{
}
Hi again..
I forgot to mention that it is reading that causes the problem.
void sendATCommand is where it reads the MAC and reports to the PC.
Reg. Joakim
I am having some trouble finding
blueToothSerial.begin(9600); // or whatever baud rate you use
It is not in setup() nor in
setupBlueToothConnection()
Then there is the matter of
Timer t;
That timer uses interrupts, that may (and probably will) interfere with swSerial.
@joakim72 -> the code you share does tons of things. We can find blueToothSerial.begin(38400);
in enterComMode()
or enterATMode()
but it's not called by sendATCommand()
so your code expects the mode to be set before hand.
The setup() calls setupBlueToothConnection()
which does not set the mode but does call sendATCommand()
so I'm unsure how this is even supposed to work.
EDIT: misread, you do call enterATMode();
Regarding the Mac address, it seems you get that from EEPROM. How did it get there in the first place?
Under void entercomMode()
blueToothSerial.begin(38400);
Well i didn't look there, it was confusing enough not to find it in setup(), for simplicities sake, put it there.
I was already wondering how the code was working at all ? Anyway, that leaves the timer as a possible interference. But could you just show me the part where the MAC address is actually received (without all debug msgs) and concatenated . Your code is not small and although it is structured, i find it hard to read.
yeah, that's the point of my question above
Regarding the Mac address, it seems you get that from EEPROM. How did it get there in the first place?
It is not the EEprom, it is when it reads the bluetooth that it will connect to.
This is ment to work automatic, and it does as long as the bluetooth MAC don't have a zero after :.
The MAC in wrong.png is taken from a device with mac 00:1D:A5:09:91:95
(hc-05 uses this format xxxx:xx:xxxxxx)
It is what comes after INQ: that is interresting.
can you make our life easy and show us in the code where you extract the MAC ?
Under
void sendATCommand(char *command)
Here is one mistake:
(command == "INQ")
You can't compare a character pointer to a string constant. Use:
(strcmp(command, "INQ") == 0)
you have like 50 occurrences of sendATCommand()... can't you be a bit more specific?
So you were expecting:
+INQ:001D:A5:099195
and getting
+INQ:1D:A5:99195,0,7FFF\r\nOK\r\n
It looks like the HC-05 is not sending leading zeros in the three fields of the MAC address. The AT Command Reference I found online shows these example responses:
+INQ:2:72:D2224,3E0104,FFBC
+INQ:1234:56:0,1F1F,FFC1
+INQ:1234:56:0,1F1F,FFC0
+INQ:1234:56:0,1F1F,FFC1
+INQ:2:72:D2224,3F0104,FFAD
+INQ:1234:56:0,1F1F,FFBE
+INQ:1234:56:0,1F1F,FFC2
+INQ:1234:56:0,1F1F,FFBE
+INQ:2:72:D2224,3F0104,FFBC OK
I expect that SoftwareSerial is acting no differently than HardwareSerial would. You will have to parse the input without relying on fixed width fields.