DC Motor control, trying to display procentage of power(rpm) on LCD

Hello.

I am a certified electrician and wanted to learn more about controlling signal for various purposes, and mostly to code.

My DC motor is connected to its own 9V circuit, I use a potensiometer for controlling the current to the motor.

Now this works all dandy on its own.

However, when I upload my code WITH these four last lines of code;

  • lcd.print(voltage);*

  • lcd.print (" V");*

  • lcd.print(prosent);*

  • lcd.print(" %");*

    the motor simply does not rotate when given current through the potensiometer.

However, the LCD screen is not displaying anything. I have set it up on its own circuit with a simple sketch to check the screen was functioning properly.

I am very new to the coding aspect of this, any help is greatly appriciated.

#include <LiquidCrystal.h>

// Motor Control Integers
int pwmPin = 6;
int pot = A0;
int c1 = 0;
int c2 = 0;

// LCD Integers
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int prosentin = A3;
int prosent = 0;
const int voltagein = A5; 


void setup() {
  // Motor Control
  pinMode(pwmPin, OUTPUT); 
  pinMode(pot, INPUT);

  // LCD Screen
  Serial.begin(9600);
  lcd.begin(16, 2);

  pinMode(voltagein, INPUT);
  pinMode(prosentin, INPUT);
}


void loop() {
  // Motor Control
  c2= analogRead(pot); 
  c1= 1024-c2;
  digitalWrite(pwmPin, HIGH); 
  delayMicroseconds(c1);   
  digitalWrite(pwmPin, LOW);  
  delayMicroseconds(c2);

  // LCD Screen
  lcd.setCursor(0, 1);
  int voltageVal = analogRead(voltagein);
  float voltage = (voltageVal/1024.0) * 5.0;

  int prosentVal = analogRead(prosentin);
  prosent = map(prosentVal, 0, 1024, 0, 255);

  lcd.print(voltage);
  lcd.print (" V");

  lcd.print(prosent);
  lcd.print(" %");
}


Schematic? Pics?

  • AnalogRead is on a 0-1024 scale,
  • AnalogWrite is on a 0-255 scale
  • Percent is a 0-100% scale

so maybe:

prosent = map(prosentVal, 0, 1024, 0, 100);

Also, the time ON here:

might be small compared to the time off while the LCD is being updated.

I might move the LCD stuff into a rate-protected protected routine to minimize it's impact on the control with these untested snippets:


void loop(void){
   ...
   report();

void report(void){
 static unsigned long lastReportMs = 0;
 if (millis() - lastReportMs < 250){
     return;
  } else {
    last = millis();
   // LCD Screen
    lcd.setCursor(0, 1);
    int voltageVal = analogRead(voltagein);
    float voltage = (voltageVal/1024.0) * 5.0;

    int prosentVal = analogRead(prosentin);
    prosent = map(prosentVal, 0, 1024, 0, 255);

    lcd.print(voltage);
    lcd.print (" V");

    lcd.print(prosent);
    lcd.print(" %");
  }
}

Thank you a reply. Here is the updated code, I pretty much copy pasted for simplicity and incorporated your thougth on the percentage scale.

However, there were some fualt messages that followed. I could lurk some of the simple ones out but the rest I do not know a way around.

#include <LiquidCrystal.h>

// Motor Control Integers
int pwmPin = 6;
int pot = A0;
int c1 = 0;
int c2 = 0;

// LCD Integers
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int prosentin = A3;
int prosent = 0;
const int voltagein = A5; 


void setup() {
  // Motor Control
  pinMode(pwmPin, OUTPUT); 
  pinMode(pot, INPUT);

  // LCD Screen
  Serial.begin(9600);
  lcd.begin(16, 2);

  pinMode(voltagein, INPUT);
  pinMode(prosentin, INPUT);
}


void loop() {
  // Motor Control
  c2= analogRead(pot); 
  c1= 1024-c2;
  digitalWrite(pwmPin, HIGH); 
  delayMicroseconds(c1);   
  digitalWrite(pwmPin, LOW);  
  delayMicroseconds(c2);
}

void loop(void) {
   ...
   report();


void report(void){
 static unsigned long lastReportMs = 0;
 if (millis() - lastReportMs < 250){
     return;
  } else {
    last = millis();
   // LCD Screen
    lcd.setCursor(0, 1);
    int voltageVal = analogRead(voltagein);
    float voltage = (voltageVal/1024.0) * 5.0;

    int prosentVal = analogRead(prosentin);
    prosent = map(prosentVal, 0, 1024, 0, 100);

    lcd.print(voltage);
    lcd.print (" V");

    lcd.print(prosent);
    lcd.print(" %");
  }
}

Here are the fualt codes:

error: redefinition of 'void loop()'
void loop(void) {

note: 'void loop()' previously defined here
void loop() {

error: expected primary-expression before '...' token
...

error: a function-definition is not allowed here before '{' token
void report(void){

error: expected '}' at end of input

Multiple libraries were found for "LiquidCrystal.h"
Used: C:\Users\jobbn\Documents\Arduino\libraries\LiquidCrystal
Not used: C:\Users\jobbn\AppData\Local\Arduino15\libraries\LiquidCrystal
exit status 1

Compilation error: redefinition of 'void loop()'

Sorry, I meant that the new report() function should be called within loop(){}.

This compiles:

#include <LiquidCrystal.h>

// Motor Control Integers
int pwmPin = 6;
int pot = A0;
int c1 = 0;
int c2 = 0;

// LCD Integers
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int prosentin = A3;
int prosent = 0;
const int voltagein = A5;


void setup() {
  // Motor Control
  pinMode(pwmPin, OUTPUT);
  pinMode(pot, INPUT);

  // LCD Screen
  Serial.begin(9600);
  lcd.begin(16, 2);

  pinMode(voltagein, INPUT);
  pinMode(prosentin, INPUT);
}


void loop() {
  // Motor Control
  c2 = analogRead(pot);
  c1 = 1024 - c2;
  digitalWrite(pwmPin, HIGH);
  delayMicroseconds(c1);
  digitalWrite(pwmPin, LOW);
  delayMicroseconds(c2);

  report();
}


void report(void) {
  static unsigned long lastReportMs = 0;
  if (millis() - lastReportMs < 250) {
    return;
  } else {
    lastReportMs = millis();
    // LCD Screen
    lcd.setCursor(0, 1);
    int voltageVal = analogRead(voltagein);
    float voltage = (voltageVal / 1024.0) * 5.0;

    int prosentVal = analogRead(prosentin);
    prosent = map(prosentVal, 0, 1024, 0, 100);

    lcd.print(voltage);
    lcd.print (" V");

    lcd.print(prosent);
    lcd.print(" %");
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.