Hi there,
I’m trying to create a code that will run different sensors and calculations, if different conditions are met, and write the calculated total onto an LCD screen. So far, all of the sensors run, and the LCD writes, and everything’s great…up until I try to add some rules so that I can have a minimum and maximum value (I also added a couple rules to try and keep the final ‘total’ fairly unpredictable). I’m not sure where I’ve gone wrong, but once I add these rules, when the hall sensor (pin 2) is triggered the LCD screen ALWAYS writes ‘3.00’, no matter the pressure I put on the pressure sensor (analog pin A1).
I think I’ve accidentally defined something permanently? Any help would be greatly appreciated!! Thank you!
The original, pre-addition code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
#define trigpin 5
#define echopin 6
int start;
const int hallPin = 2;
int hallState = 0;
const int minTotal = 0;
const int analogInPin = A1;
int sensorValue = 0;
int val = 0;
int duration, distance;
float total;
float totalH;
float totalH1;
float totalH2;
float totalT;
float a;
float b;
unsigned long time;
const int maxTime = 32000;
const int minTime = 0;
const int starttime = 0;
unsigned long timenow;
int duration2;
void setup() {
Serial.begin (9600);
pinMode(hallPin, INPUT);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
lcd.begin(16, 2);
lcd.print("Self-Monitor");
delay(1000);
lcd.clear();
}
int getTotalHallTime ()
{
hallState = digitalRead(hallPin);
if (hallState == LOW) {
timenow=(millis()-starttime); //get current time
}
else
{
timenow=1000;
}
totalT=(timenow/1000);
return totalT;
}
int getTotal ()
{
total = (totalH+totalT);
return total;
}
int getTrigpin ()
{
long duration, distance;
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = (duration / 2) / 29.1;
delay (250);
if (distance < 15) {
int total = getTotalMat ();
}
}
int getTotalMat() {
pinMode(1, INPUT_PULLUP);
sensorValue = analogRead(analogInPin);
Serial.print("sensor = " );
Serial.println(sensorValue);
a = 5.00;
b = 11.00;
if (sensorValue <= 6) {
total = (a / 2);
}
if (sensorValue > 6 && sensorValue <= 10) {
total = (b + 3.40);
}
if (sensorValue > 10){
total = (b*2.90);
}
return total;
}
void loop(){
hallState = digitalRead(hallPin);
Serial.print (hallState);
totalH1 = 0.35;
totalH2 = 0.89;
long duration, distance;
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = (duration / 2) / 29.1;
delay (250);
if (hallState == LOW) {
totalH = totalH1;
timenow=(millis()-starttime);
totalT=(timenow/3000);
pinMode(1, INPUT_PULLUP);
sensorValue = analogRead(analogInPin);
Serial.print("sensor = " );
Serial.println(sensorValue);
total = (totalH+(totalT/15)-(sensorValue/5));
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Item Value:");
lcd.setCursor(7,1);
lcd.print(total, 2);
}
if (hallState == HIGH && distance < 15) {
totalH = totalH2;
pinMode(1, INPUT_PULLUP);
sensorValue = analogRead(analogInPin);
Serial.print("sensor = " );
Serial.println(sensorValue);
a = 5.00;
b = 11.00;
if (sensorValue <= 6) {
total = (a / 2);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Item Value:");
lcd.setCursor(7,1);
lcd.print(total, 2);
}
if (sensorValue > 6 && sensorValue <= 10) {
total = ((b + 3.40)/totalH2);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Item Value:");
lcd.setCursor(7,1);
lcd.print(total, 2);
}
if (sensorValue > 10){
total = (b*2.90);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Item Value:");
lcd.setCursor(7,1);
lcd.print(total, 2);
}
}}
And the additional rules that made it stop working:
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
#define trigpin 5
#define echopin 6
int start;
const int hallPin = 2;
int hallState = 0;
const int minTotal = 0;
const int analogInPin = A1;
int sensorValue = 0;
int val = 0;
int duration, distance;
float total;
float totalH;
float totalH1;
float totalH2;
float totalT;
float totalS;
float a;
float b;
unsigned long time;
const int maxTime = 32000;
const int minTime = 0;
const int starttime = 0;
unsigned long timenow;
int duration2;
void setup() {
Serial.begin (9600);
pinMode(hallPin, INPUT);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
lcd.begin(16, 2);
lcd.print("Self-Monitor");
delay(1000);
lcd.clear();
}
int getTotalHallTime ()
{
hallState = digitalRead(hallPin);
if (hallState == LOW) {
timenow=(millis()-starttime); //get current time
}
else
{
timenow=1000;
}
totalT=(timenow/1000);
return totalT;
}
int getTotal ()
{
total = (totalH+totalT);
return total;
}
int getTrigpin ()
{
long duration, distance;
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = (duration / 2) / 29.1;
delay (250);
if (distance < 15) {
int total = getTotalMat ();
}
}
int getTotalMat() {
pinMode(1, INPUT_PULLUP);
sensorValue = analogRead(analogInPin);
Serial.print("sensor = " );
Serial.println(sensorValue);
a = 5.00;
b = 11.00;
if (sensorValue <= 6) {
total = (a / 2);
}
if (sensorValue > 6 && sensorValue <= 10) {
total = (b + 3.40);
}
if (sensorValue > 10){
total = (b*2.90);
}
return total;
}
void loop(){
hallState = digitalRead(hallPin);
Serial.print (hallState);
totalH1 = 0.35;
totalH2 = 0.89;
long duration, distance;
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = (duration / 2) / 29.1;
delay (250);
if (hallState == LOW) {
totalH = totalH1;
timenow=(millis()-starttime);
totalT=(timenow/3000);
pinMode(1, INPUT_PULLUP);
sensorValue = analogRead(analogInPin);
Serial.print("sensor = " );
Serial.println(sensorValue);
totalS = (totalH+(totalT/15)-(sensorValue/5));
if (totalS < -0.5) {
total = ((-totalS)-(sensorValue/10));
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Item Value:");
lcd.setCursor(7,1);
lcd.print(total, 2);
}
if (totalS < 0 && totalS > -0.5) {
total = (-totalS);
}
if (totalS = 0) {
total = (totalH+(sensorValue/5));
}
if (totalS > 0 && totalS <= 3.00) {
total = (totalS);
}
if (totalS < 3.00) {
total = (3.00-totalS);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Item Value:");
lcd.setCursor(7,1);
lcd.print(total, 2);
}
if (hallState == HIGH && distance < 15) {
totalH = totalH2;
pinMode(1, INPUT_PULLUP);
sensorValue = analogRead(analogInPin);
Serial.print("sensor = " );
Serial.println(sensorValue);
a = 5.00;
b = 11.00;
if (sensorValue <= 6) {
total = (a / 2);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Item Value:");
lcd.setCursor(7,1);
lcd.print(total, 2);
}
if (sensorValue > 6 && sensorValue <= 10) {
total = ((b + 3.40)/totalH2);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Item Value:");
lcd.setCursor(7,1);
lcd.print(total, 2);
}
if (sensorValue > 10){
total = (b*2.90);
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Item Value:");
lcd.setCursor(7,1);
lcd.print(total, 2);
}
}}