Printing a PWM Value

Hi There,

I am making a robot and using PWM to drive the motors. All its working, however I want to print the value of the PWM (0 to 255) on the LCD. Code Attached. Do I need to add any other variable?

*/

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
 
#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
 
int n = 1;
 
LiquidCrystal_I2C       lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogleftPin = 9; // Analog output pin that the LED is attached to
const int analogrightPin = 10; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
int pwmleft = 0;
int pwmright = 0;

void setup()
{
        //Initialize the LCD
        lcd.begin (20,4,LCD_5x8DOTS);
        
        //Define LCD backlight logic level
        lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); // init the backlight
        
        //Define the following Pins as Inputs
        pinMode(2, INPUT_PULLUP);
        pinMode(3, INPUT_PULLUP);
        pinMode(4, INPUT_PULLUP);
        pinMode(5, INPUT_PULLUP);
        pinMode(6, INPUT_PULLUP);
        pinMode(7, INPUT_PULLUP);
        pinMode(8, INPUT_PULLUP);
        pinMode(11, INPUT_PULLUP);
        pinMode(12, INPUT_PULLUP);
        pinMode(13, INPUT_PULLUP);
           
 }
 
void loop()
{
  
        lcd.setBacklight(HIGH);     // Backlight on
        
        //read the sensors value into a variable
        int S1 = digitalRead(2);
        int S2 = digitalRead(3);
        int S3 = digitalRead(4);
        int S4 = digitalRead(5);
        int S5 = digitalRead(6);
        int S6 = digitalRead(7);
        int S7 = digitalRead(8);
        int S8 = digitalRead(11);
        //int S9 = digitalRead(12);
        //int S10 = digitalRead(13);
 
  
  
        // read the battery voltage:
        sensorValue = analogRead(analogInPin); 
         
  
  
        //Stop the motors once the track is completed
        if ((S5==1) && (S6==1) && (S7==1) && (S8==1) && (S1==1) && (S2==1) && (S3==1) && (S4==1))
             {
              analogWrite(analogleftPin, 0);
              analogWrite(analogrightPin, 0);
              lcd.home ();   
              lcd.print("          **STOP**");
             }
             
         //Stop the motors if no signal is received    
         else if ((S5==0) && (S6==0) && (S7==0) && (S8==0) && (S1==0) && (S2==0) && (S3==0) && (S4==0))
             {
              analogWrite(analogleftPin, 0);
              analogWrite(analogrightPin, 0);
              lcd.home ();   
              lcd.print("          **STOP**");
             }
             
        else
  
                      //If in the track do either:
                    {
                     
                     
                       // Robot is aligned with track. Go Straight at full speed 
                       if ( (S2==1) && (S4==1) && (S6==1) && (S8==1) && (S1==0) && (S3==0) && (S5==0) && (S7==0))
                      
                      {
                       lcd.home ();   
                       lcd.print("          STRAIGHT");
                       analogWrite(analogleftPin, 100);
                       analogWrite(analogrightPin, 100);
                      }
                      
                       //Too far to the LEFT
                       else if ((S5==1) && (S6==1) && (S7==1) && (S8==1))
                      {
                        lcd.home ();   
                        lcd.print("          <- LEFT ");
                        analogWrite(analogleftPin, 100);
                        analogWrite(analogrightPin, 0);
                      }
                      
                        //Too far to the RIGHT
                        else if ((S1==1) && (S2==1) && (S3==1) && (S4==1))
                      {
                        lcd.home ();   
                        lcd.print("          <- RIGHT");
                        analogWrite(analogleftPin, 0);
                        analogWrite(analogrightPin, 100);
                      }
                      
                       
                    }
        
        
  
  /*
  Goto LCd line 1
  Print sensor Values for Diagnostic */
  
  lcd.home ();                     
  lcd.print(""); 
  lcd.print(S1); lcd.print(S2); lcd.print(S3); lcd.print(S4); lcd.print(S5);
  lcd.print(S6); lcd.print(S7); lcd.print(S8); //lcd.print(S9) || lcd.print(S10);
  
  
  //Goto LCD line 2
  lcd.setCursor ( 0, 1 );
  
  //Print battery Voltage, assuming VMAX = 5V
  lcd.print("Main Bat = V " ); lcd.print(sensorValue*0.0049); 

  
  //print out the value of the sensors
  lcd.setCursor ( 0, 2 );        // go to the next line
  lcd.print("LEFT %");      
  //lcd.print(/*outputValue/2.55analogrightPin/outputValue*/(analogleftPin));  
  //lcd.print(/*outputValue/2.55analogrightPin/outputValue*/(analogrightPin));
  lcd.print("  RIGHT %");  
  lcd.print (outputValue, 10 );
  lcd.print (outputValue, 9 );

  delay(2);                    


}

the bit that doesnt work is on the last lines

//print out the value of the sensors
  lcd.setCursor ( 0, 2 );        // go to the next line
  lcd.print("LEFT %");      
  //lcd.print(/*outputValue/2.55analogrightPin/outputValue*/(analogleftPin));  
  //lcd.print(/*outputValue/2.55analogrightPin/outputValue*/(analogrightPin));
  lcd.print("  RIGHT %");  
  lcd.print (outputValue, 10 );
  lcd.print (outputValue, 9 );

the bit that doesnt work is on the last lines

print statement format (same for lcd except for lcd it is lcd.print();

  lcd.print (outputValue, 10 );  (this does not match the syntax described here:

const int analogleftPin = 9; // Analog output pin that the LED is attached to
const int analogrightPin = 10; // Analog output pin that the LED is attached to

  lcd.print (outputValue, 10 );  (You can't do this. You can't print using the PWM pin number. You have to print the
variable. . You know what those are already because you use them here:

analogWrite(analogleftPin, 100);
                       analogWrite(analogrightPin, 100);

  lcd.print (outputValue, 10 );  (this doesn't work because you can't use the pin number .you have to do this:

  lcd.print (outputValue);

You can test this using Serial.print(val);
  lcd.print (outputValue, 10 );  (this does not match the syntax described here:

(You can't do this. You can't print using the PWM pin number. You have to print the
variable. . You know what those are already because you use them here:
Code:

analogWrite(analogleftPin, 100);
analogWrite(analogrightPin, 100);

I Know it is wrong I was just messing arround.

I started by defining the PWM, for example

analogWrite(analogleftPin, 100);
analogWrite(analogrightPin, 100);

Which works fine on the outputs.

And then I was using

 lcd.print (analogrightPin );

Which i hoped It would print the value of the variable analogrightpin, which is the PWM, but for some reason it does not work.

Perhaps I am missing something on the code? I can print directly from an analog input, just having trouble printing the variable I set for the PWM

Which i hoped It would print the value of the variable analogrightpin, which is the PWM, but for some reason it does not work.

The variable analogrightpin holds the number of the pin. It does NOT contain the value written to the pin.

"It does not work" is too lame to merit trying to help you. The code does something. You need to explain what it does. You expect the code to do something (but your expectations are clearly wrong). You haven't been completely clear on your expectations.

Have I not?

This is my issue:
Basically I enable the motors and set pin 9 and 10 as a PWM output. I can set any value from 0 to 255.

analogWrite(analogleftPin, 100);

In this example I set one of them to 100, which is about 45% duty cycle. This works fine and shows on the output

I want to have a variable that can print the value I just send to the PWM pin on the LCD

The variable analogrightpin holds the number of the pin. It does NOT contain the value written to the pin.

That explain my issue. So where is the value written to the pin stored on? Iam guessing i should write it as such

lcd.print (analogrightPin, unknown_variable_where_value_is_stored );

I want to have a variable that can print the value I just send to the PWM pin on the LCD

OK.

int pwmValue = 84;
analogWrite(analogleftPin, pwmValue);
lcd.print(pwmValue);

I was looking at a sketch for serial print

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

Is there a way I can change this line to suit my purpose?

// map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);

Is there a way I can change this line to suit my purpose?

Yes. Use a text editor.

What is your purpose?

Same as above, just trying to avoid having to define the PWm value on each instance. I have like 30 different combinations for both left and right PWM outputs

Why not use array ?