So yes, another question from your friendly noob here.
I am trying to make a simple project which controls a RC motor I dug up and control the speed with PWM. I can run and control the speed just fine, and the speed I have displayed on the first line works well(displaying 0-225). The problem, however, is the that I just can't get the percentage to display correctly on the bottom line of my LCD screen. All it displays is 0.00% from 0-224, then when it hits 225 (max speed) suddenly it displays 100.00%. Why? This isn't a necessary part of the program, I just want to know why it does this and how to fix it. I will post my code below, I have tried to add comments into it but I'm not yet in the habit, please excuse me if it is hard to understand.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
/*
Sensor on pin A0
SDA to A4
RC on 3
Buttons on 7,8
SCL to A5
*/
int speedVar = 0;
int plus = 0;
int negative = 0;
void setup(){
Serial.begin(9600);
lcd.begin(16,2);
pinMode(7, INPUT); //setting up pins
pinMode(8, INPUT);
lcd.clear();
lcd.print("Speed = ");
lcd.print(speedVar);
lcd.setCursor(0,1);
lcd.print("0%");
delay(50);
}
void loop(){
negative = digitalRead(8); //sets up pins for reading
plus = digitalRead(7);
lcd.clear();
lcd.print("Speed = ");
lcd.print(speedVar);
lcd.setCursor(0,1);
float SV1 = ((speedVar/225)*100);
lcd.print(SV1); //prints percentage
Serial.println(SV1); //checks with serial
lcd.print("%");
delay(50);
analogWrite(3, speedVar);
if(plus == HIGH){ //if positive button is clicked..
if(speedVar == 225){ //check if past speed limit
errorMax();
delay(500);
errorMax(); //Error message for 2 seconds
delay(500);
errorMax();
delay(500);
errorMax();
lcd.clear();
delay(500);
}
else{
speedVar+=1; //add one to speed if under limit
analogWrite(3, speedVar);
}
}
if(negative == HIGH){
if(speedVar == 0){
errorMin();
delay(500);
errorMin();
delay(500);
errorMin();
delay(500);
errorMin();
delay(500);
}
else{
speedVar-=1;
analogWrite(3, speedVar);
}
}
delay(50);
}
void errorMax() //Maximum or minimum functions
{
lcd.clear();
lcd.print("ERROR");
lcd.setCursor(0,1);
lcd.print("MAXIMUM SPEED");
}
void errorMin()
{
lcd.clear();
lcd.print("ERROR");
lcd.setCursor(0,1);
lcd.print("MINIMUM SPEED");
}
Thanks, in advance. Have yourself a good day.