PWM Errors - Beginner

Hi there! I’m very new to C++ so I’ll probably make plenty of mistakes in my code.
So my goal is to create a arduino that will take the inputs of a Freematics OBDII UART adapter and use the rpm of the car to adjust the PWM duty cycle by 1 point for every 20 rpm the car climbs. For example, when rpm = 20 PWM = 1/255, rpm = 200 PWM = 10. I’ve given this a shot and have received errors, but I’m also having trouble with the libraries where as my friend is not. If anyone could lend a hand I would be greatly appreciative!

#include <Arduino.h>
#include <Wire.h>
#include <OBD.h>
COBD obd;

void setup() {
// put your setup code here, to run once:
//Below for PWM pin
pinMode(3, OUTPUT);
obd.begin();
while (!obd.init());
}

void loop(){
// put your main code here, to run repeatedly:
int value;
int ledPWM;
//the pwm for the LED’s should equal the rpm speed / 255 I think
ledPWM = value / 255;
//if the rpm (value) is enough to equal 255 (or 5100rpm) or more then cap value at 255 until it falls below
if (value <= 255)
value = 255;

if (obd.read(PID_RPM, value)) {
//Past this needs to be revised
analogWrite(3, value);

}
}

Hi,
Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

This will present it in a format that is easy to read.
What model Arduino are you using and what revision IDE?

Tom… :slight_smile:

And please post the COMPLETE error message as well; newer IDEs have a button to copy it so you can paste it here.

void loop(){
 // put your main code here, to run repeatedly:
 int value;
 int ledPWM;
//the pwm for the LED's should equal the rpm speed / 255 I think
 ledPWM = value / 255;

You haven't assigned a value to "value", yet you divide it by 255.

Thanks for the replies, so I’m using the arduino Nano V4, This is the code in the code tags

#include <Arduino.h>
#include <Wire.h>
#include <OBD.h>
COBD obd;

void setup() {
 // put your setup code here, to run once:
//Below for PWM pin
 pinMode(3, OUTPUT);
 obd.begin();
 while (!obd.init());
}

void loop(){
 // put your main code here, to run repeatedly:
 int value;
 int ledPWM;
//the pwm for the LED's should equal the rpm speed / 255 I think
 ledPWM = value / 255;
//if the rpm (value) is enough to equal 255 (or 5100rpm) or more then cap value at 255 until it falls below
         if (value <= 255)
         value = 255;

if (obd.read(PID_RPM, value)) {
   //Past this needs to be revised
   analogWrite(3, value);

 }
}

I’m not sure what revision IDE I’m using, but this is the error code as well

Arduino: 1.6.7 (Windows 10), Board: “Arduino Nano, ATmega168”

C:\Users\TEST\Documents\Arduino\RPM_LED\RPM_LED.ino: In function ‘void loop()’:

RPM_LED:22: error: ‘ledPWM’ was not declared in this scope

ledPWM = value / 255;

^

RPM_LED:23: error: expected ‘(’ before ‘ledPWM’

if ledPWM <= 255;

^

exit status 1
‘ledPWM’ was not declared in this scope

This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

AWOL:

void loop(){

// put your main code here, to run repeatedly:
int value;
int ledPWM;
//the pwm for the LED’s should equal the rpm speed / 255 I think
ledPWM = value / 255;



You haven't assigned a value to "value", yet you divide it by 255.

Thanks for your reply!
The ‘value’ was taken from an example used in an example sketch for the OBDII UART Adapter, apparently its what the rpm value gets assigned to.

Cheers!

Can you post a link to the OBD library? I had to take everything related to that out after which there are no compile errors.

sterretje:
Can you post a link to the OBD library? I had to take everything related to that out after which there are no compile errors.

I'm getting compile errors and my mate and I couldn't figure out why, we had everything installed exactly the same, but here it is :wink:

Hi,
Which sketch in the examples?

Tom… :slight_smile:

Your error message states that you’re using IDE 1.6.7.

I copied the library files to the sketch directory, changed the include for the library to use the sketch directory (using double quotes instead of < and >) and did not have a problem compiling in IDE 1.6.6.

I used your sketch from reply #4 . Can’t help further.

sterretje:
Your error message states that you’re using IDE 1.6.7.

I copied the library files to the sketch directory, changed the include for the library to use the sketch directory (using double quotes instead of < and >) and did not have a problem compiling in IDE 1.6.6.

I used your sketch from reply #4 . Can’t help further.

I think that may be my issue, I’m possibly not doing something right as I continuously get this error:

Arduino: 1.6.7 (Windows 10), Board: "Arduino Nano, ATmega168"

RPM_LED:1: error: empty filename in #include

 #include ""Arduino.h""

          ^

RPM_LED:2: error: empty filename in #include

 #include ""Wire.h""

          ^

RPM_LED:3: error: empty filename in #include

 #include ""OBD.h""

          ^

exit status 1
empty filename in #include

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

TomGeorge:
Hi,
Which sketch in the examples?

Tom... :slight_smile:

I tried a few from the OBD example sketches, but that specific one was rpm_led_uart and I receive this error

Arduino: 1.6.7 (Windows 10), Board: "Arduino Nano, ATmega168"

In file included from C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp:10:0:

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp: In member function 'virtual void COBD::write(const char*)':

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.h:22:17: error: 'Serial1' was not declared in this scope

 #define OBDUART Serial1

                 ^

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp:112:2: note: in expansion of macro 'OBDUART'

  OBDUART.write(s);

  ^

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp: In member function 'virtual void COBD::begin()':

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.h:22:17: error: 'Serial1' was not declared in this scope

 #define OBDUART Serial1

                 ^

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp:290:2: note: in expansion of macro 'OBDUART'

  OBDUART.begin(OBD_SERIAL_BAUDRATE);

  ^

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp: In member function 'virtual byte COBD::receive(char*, byte, int)':

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.h:22:17: error: 'Serial1' was not declared in this scope

 #define OBDUART Serial1

                 ^

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp:302:7: note: in expansion of macro 'OBDUART'

   if (OBDUART.available()) {

       ^

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp: In member function 'virtual void COBD::end()':

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.h:22:17: error: 'Serial1' was not declared in this scope

 #define OBDUART Serial1

                 ^

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp:396:2: note: in expansion of macro 'OBDUART'

  OBDUART.end();

  ^

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp: In member function 'virtual bool COBD::setBaudRate(long unsigned int)':

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.h:22:17: error: 'Serial1' was not declared in this scope

 #define OBDUART Serial1

                 ^

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp:401:5: note: in expansion of macro 'OBDUART'

     OBDUART.print("ATBR1 ");

     ^

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp: In member function 'virtual void COBDI2C::write(const char*)':

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp:449:35: warning: narrowing conversion of 'millis()' from 'long unsigned int' to 'uint16_t {aka unsigned int}' inside { } [-Wnarrowing]

  COMMAND_BLOCK cmdblock = {millis(), CMD_SEND_AT_COMMAND};

                                   ^

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp: In member function 'bool COBDI2C::sendCommandBlock(byte, uint8_t, byte*, byte)':

C:\Users\TEST\Documents\Arduino\libraries\OBD\OBD.cpp:458:35: warning: narrowing conversion of 'millis()' from 'long unsigned int' to 'uint16_t {aka unsigned int}' inside { } [-Wnarrowing]

  COMMAND_BLOCK cmdblock = {millis(), cmd, data};

                                   ^

exit status 1
Error compiling.

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

Board: "Arduino Nano, ATmega168"``error: 'Serial1' was not declared in this scope
No surprise there

380VRX:
I think that may be my issue, I’m possibly not doing something right as I continuously get this error:

#include ""Arduino.h""

^

RPM_LED:2: error: empty filename in #include

Why the double double quotes?
arduino.h is a standard library and unless you have ‘overridden’ it with a version local tot he sketch, you would include it with

#include <Arduino.h>

This is what I changed in your original sketch because I did not want to install the library as a standard library. The h and cpp files were copied to the sketch directory. The below change is not needed if you do a standard library installation.

#include <Arduino.h>
#include <Wire.h>
#include "OBD.h"      // <-----
COBD obd;