I am making a device which can multiply the analog input signal with respect to the selected gain from the digital inputs. Here I have used two digital inputs(from which I can have four selections) but I am planing to add two inputs more(then I can have 16 selections). I made the code with if else statements is there any shorter way to do that? or else the code is getting huge.
here is the code:
#define sensor A0
#define resetPin 3
#define measurePin 2
#define k1 3
#define k2 4
float A = 0;
float J = 0;
int resetstatus = 0;
int measurestatus = 0;
void setup() {
Serial.begin(9600);
pinMode(sensor,INPUT);
pinMode(resetPin,INPUT_PULLUP);
pinMode(measurePin,INPUT_PULLUP);
pinMode(k1,INPUT_PULLUP);
pinMode(k2,INPUT_PULLUP);
Serial.println("INITIALAIZING");
delay(3000);
Serial.println("Ready to begin");
delay(200);
}
void loop()
{
resetstatus = digitalRead(resetPin);
measurestatus = digitalRead(measurePin);
if(resetstatus == LOW)
{
reset();
delay(200);
}
if(measurestatus == LOW)
{
if(digitalRead(k1 == LOW) && digitalRead(k2 == LOW))
{
J = 1.0;
measure();
delay(50);
}
else if(digitalRead(k1 == LOW) && digitalRead(k2 == HIGH))
{
J = 2.0;
measure();
delay(50);
}
else if(digitalRead(k1 == HIGH) && digitalRead(k2 == LOW))
{
J = 3.0;
measure();
delay(50);
}
if(digitalRead(k1 == HIGH) && digitalRead(k2 == HIGH))
{
J = 4.0;
measure();
delay(50);
}
}
}
void measure()
{
A = analogRead(sensor);
float sumA = A * J;
Serial.println(sumA);
}
void reset()
{
Serial.println("reset");
}
I am having a problem with the output from this code (I am using a potentiometer to feed the arduino an analog value)
I dont know why the output is showing 993 and 3972 at each cycle. can someone explain?
