Hello, I have a question basically I have an issue programming the code with three different mass measurements. First I am trying to measure mass from 0 grams to 1000 grams.
And I am trying separate three parts the first part is x < 300 grams, the second part is x > 300 grams && x< 600 grams, and the third part is else is above 600 grams. Can u help me how to code this?
const int f0 = A0;
const int f1 = A1;
const int f2 = A2;
const int f3 = A3;
const int red = 9;
const int green = 10;
const int blue = 11;
float s0 = 0; // value read from the pot
float s1 = 0;
float s2 = 0;
float s3 = 0;
float pred = 0;
int outputValue = 0; // value output to the PWM (analog out)
int rep = 10;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}
void loop() {
float sum0 = 0;
float sum1 = 0;
float sum2 = 0;
float sum3 = 0;
s0=analogRead(f0);
s1=analogRead(f1);
s2=analogRead(f2);
s3=analogRead(f3);
float sum = (s0+s1+s2+s3)*5.0/1023.0;
if (sum<19.0){
delay(750);
for (int i = 0; i < rep; i++) {
sum0 += analogRead(f0);
sum1 += analogRead(f1);
sum2 += analogRead(f2);
sum3 += analogRead(f3);
}
sum0 *= (5.0 / 1023.0);
sum1 *= (5.0 / 1023.0);
sum2 *= (5.0 / 1023.0);
sum3 *= (5.0 / 1023.0);
s0 = sum0 / rep;
s1 = sum1 / rep;
s2 = sum2 / rep;
s3 = sum3 / rep;
sum = s0+s1+s2+s3;
if (sum < 3.1){
pred = -95.994241 + 333.23423/sum;
}
else if (sum > 3.1 && sum < 3.6){
pred = -923.262974 + 17232.1374/sum;
}
else {
pred = -11127.81 + 33575.004/sum;
}
Serial.print("s0 = ");
Serial.println(s0);
Serial.print("s1 = ");
Serial.println(s1);
Serial.print("s2 = ");
Serial.println(s2);
Serial.print("s3 = ");
Serial.println(s3);
Serial.print("volts: ");
Serial.println(sum/4);
Serial.print("grams: ");
Serial.println(pred);
Serial.println("_____________________");
Serial.println(sum);
delay(700);
}
if (pred < 200){
digitalWrite(red, HIGH);
digitalWrite(blue, LOW);
digitalWrite(green, LOW);
}
else if (pred < 400 && pred >= 200){
digitalWrite(red, LOW);
digitalWrite(blue, LOW);
digitalWrite(green, HIGH);
}
else {
digitalWrite(red, LOW);
digitalWrite(blue, HIGH);
digitalWrite(green, LOW);
}
}