When the value of the variable "torque" is less than zero(because the value is negative), the LCD and serial will display the value zero instead.
I used this code but it did NOT equate/display the value zero for the variable "torque" in the LCD and serial, if the value of "torque" is less than zero.
if (torque < 0)
{
torque == 0;
}
Serial output. RPM is ok
RPM , torque , voltage from loadcell
0 , -6.49 , 0.05
0 , -6.30 , 0.11
0 , -6.15 , 1.16
0 , -5.94 , 1.52
My target is that when 'torque' is less than(negative) zero it must display its value as zero.
Something like this in the serial.
RPM , torque , voltage from loadcell
0 , 0 , 0.05
0 , 0 , 0.11
0 , 0 , 1.16
0 , 0.2 , 1.52
Why did it not displaying the value zero and what is the right way coding it?
Entire sketch:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 7); //LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)
int backLight = 13;
const int hallPin=2; //RPM:
const unsigned long sampleTime=3000; //RPM:6000(10)
const int maxRPM = 1700; //RPM:
const int numReadings = 25;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int voltage = 0; // the average
int voltageSmooth;
int torque;
int sensorValue1 = analogRead(A0);
void setup() {
pinMode(hallPin,INPUT); //RPM:
Serial.begin(9600);
lcd.begin(20, 4);
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
pinMode(9, OUTPUT); // D9 to LCD Pin 3
analogWrite(9, 50); // set contrast
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
int rpm=getRPM(); //RPM:
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(sensorValue1);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average=voltage:
voltageSmooth = total / numReadings;
float voltage = voltageSmooth * (5.0 / 1023.0);
float torque = (0.143*(voltage*voltage)+0.306*voltage-0.73)*2.2*4.13; //(voltage - 1.6) / 0.985 * 2.2 * 4.13;
if (torque < 0)
{
torque == 0;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("DIGITAL DYNAMOMETER");
lcd.setCursor(0, 1);
lcd.print("TORQUE");
lcd.setCursor(6, 1);
lcd.print(":");
lcd.print(torque, 2);
lcd.print(" lbf.in");
lcd.setCursor(0, 2);
lcd.print("RPM");
lcd.setCursor(6, 2);
lcd.print(":");
lcd.print(rpm,DEC);
lcd.print("rpm");
lcd.setCursor(0, 3);
lcd.print("TEMP");
lcd.setCursor(6, 3);
lcd.print(":");
lcd.print("NA_");
lcd.print("*C");
Serial.print(rpm,DEC);
Serial.print(" , ");
Serial.print(torque, 2);
Serial.print(" , ");
Serial.print(voltage, 2);
Serial.println();
}
int getRPM() //RPM:
{
// sample for sampleTime in milliseconds
int count=0; //RPM:
boolean kflag=HIGH; //RPM:
unsigned long currentTime=0; //RPM:
unsigned long startTime=millis(); //RPM:
while (currentTime<=sampleTime) //RPM:
{
if (digitalRead(hallPin)==HIGH) //RPM:
{
kflag=HIGH; //RPM:
}
if (digitalRead(hallPin)==LOW && kflag==HIGH) //RPM:
{
count++; //RPM:
kflag=LOW; //RPM:
}
currentTime=millis()-startTime; //RPM:
}
int count2rpm = int(60000./float(sampleTime))*count; //RPM:
return count2rpm; //RPM:
}