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 );