Hello,
I'm controlling a dc motor speed by a potentiometer
I want to display the potentiometer value as percentage. Here's the code
I'm a beginner so go easy on me .
// Arduino DC motor speed and direction control
#define button 8
#define pot 0
#define pwm1 9
#define pwm2 10
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
int potPin0 = A0;
int potPin1 = A2;
boolean motor_dir = 0;
int motor_speed;
void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
pinMode(potPin0, INPUT);
pinMode(potPin0, INPUT);
}
void loop() {
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("Motor speed: "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potPin0)); // Prints value on Potpin1 to LCD
lcd.setCursor(0,1); // Sets the cursor to col 1 and row 0
lcd.print("E-polytechnique "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potPin1)); // Prints value on Potpin1 to LCD
motor_speed = analogRead(pot) / 4;
if(motor_dir)
analogWrite(pwm1, motor_speed);
else
analogWrite(pwm2, motor_speed);
if(!digitalRead(button)){ // If direction button is pressed
while(!digitalRead(button)); // Wait until direction button released
motor_dir = !motor_dir; // Toggle direction variable
if(motor_dir)
digitalWrite(pwm2, 0);
else
digitalWrite(pwm1, 0);
}
}