Coding error with electro pneumatic circuit

Hi, im a biology student with zero arduino experience attempting to copy this program over to my arduino nano: http://softroboticstoolkit.com/low-cost-ep-circuit/board-implementation/software

Here is code:

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

if ( switch_pin == HIGH) {
digitalWrite(inletValvePin,HIGH); // turn pressure valve HIGH
digitalWrite(pumpPin,HIGH); // make sure pump is on
digitalWrite(exhaustValvePin,LOW); // turn exhaust valve LOW
digitalWrite(ledPin,HIGH); // LED ON
}

if ( switch_pin == LOW) {
digitalWrite(inletValvePin,LOW); // turn pressure valve HIGH
digitalWrite(pumpPin,LOW); // pump is off
digitalWrite(exhaustValvePin,HIGH); // turn exhaust valve HIGH
digitalWrite(ledPin,LOW); // LED off
}

sensorValue = analogRead(analogInPin);

// digital value of pressure sensor voltage
voltage_mv = (sensorValue * reference_voltage_mv) / ADCFULLSCALE;

// pressure sensor voltage in mV
voltage_v = voltage_mv / 1000;

output_pressure = ( ( (voltage_v - (0.10 * (reference_voltage_mv/1000) )) * (Pmax - Pmin) ) / (0.8 * (reference_voltage_mv/1000) ) ) + Pmin;

if (sensorValue <= desiredValue) {
analogWrite(pumpPin,255); // Pump. (0 is off) and (255 is on)
digitalWrite(inletValvePin,HIGH); // open the air valve
digitalWrite(exhaustValvePin,LOW); // close the exhaust valve
}

if (sensorValue > desiredValue) {
while (analogRead(A0) > (desiredValue - tolerance) ) {
analogWrite(pumpPin,190); // reduce speed of pump
digitalWrite(inletValvePin,HIGH); // open the air valve
digitalWrite(exhaustValvePin,LOW); // close the exhaust valve
}
}

When i try to compile, i’m getting error message: exit status 1 ‘switch_pin’ was not declared in this scope, for the second if statement line. Any idea what I’m doing incorrectly? Thanks in advance for the help.

Can you please edit your post to use code tags please. See How to use this forum.

And to answer the question, the compiler is right. switch_pin is never defines. What is "switch_pin" supposed to be? And how is the compiler supposed to know? Out of thin air?

Well you probably need to spend 1h doing the first 3 or 4 Arduino basic tutorials...

Your variable is not declared, the pin mode is not set and pin value not read...

You do not have a declaration for switch_pin.

What pin did you connect the switch to?

For example, if your switch is connected to pin #3, you need:

const byte switch_pin = 3 ;

someplace ABOVE void setup() { .

Presumably, your switch is connected to pin 3 and to ground. In that case, within the setup() function, you need:

pinMode(switch_pin, INPUT_PULLUP) ;

Okay thanks for all the feedback, really out of my depth here. This is the schematic of my components:

Here’s my updated code:

const byte switch_pin = 27 ;
const byte inletValvePin = 9 ;
const byte exhaustValvePin = 10 ;
const byte pumpPin = 8 ;
const byte sensorValue = 19 ;
const byte analogInPin = 27 ;

void setup() {
  // put your setup code here, to run once:
pinMode(switch_pin, INPUT_PULLUP) ;
}

void loop() {
  // put your main code here, to run repeatedly:

if ( switch_pin == HIGH) {
digitalWrite(inletValvePin,HIGH); // turn pressure valve HIGH
digitalWrite(pumpPin,HIGH); // make sure pump is on 
digitalWrite(exhaustValvePin,LOW); // turn exhaust valve LOW 

}

if ( switch_pin == LOW) { 
digitalWrite(inletValvePin,LOW); // turn pressure valve HIGH
digitalWrite(pumpPin,LOW); // pump is off
digitalWrite(exhaustValvePin,HIGH); // turn exhaust valve HIGH 

}

sensorValue = analogRead(analogInPin); 
                            
// digital value of pressure sensor voltage
voltage_mv =  (sensorValue * reference_voltage_mv) / ADCFULLSCALE;
 
// pressure sensor voltage in mV
voltage_v = voltage_mv / 1000; 
                                     
output_pressure = ( ( (voltage_v - (0.10 * (reference_voltage_mv/1000) )) * (Pmax - Pmin) ) / (0.8 * (reference_voltage_mv/1000) ) ) + Pmin;

if (sensorValue <= desiredValue) {
analogWrite(pumpPin,255); // Pump. (0 is off) and (255 is on)
digitalWrite(inletValvePin,HIGH); // open the air valve
digitalWrite(exhaustValvePin,LOW); // close the exhaust valve
}

if (sensorValue > desiredValue) {
while (analogRead(A0) > (desiredValue - tolerance) ) {
analogWrite(pumpPin,190); // reduce speed of pump
digitalWrite(inletValvePin,HIGH); // open the air valve
digitalWrite(exhaustValvePin,LOW); // close the exhaust valve
}
}

Confused with the Sensor Value and AnalogIn pin. Currently getting an error saying exit status 1
assignment of read-only variable 'sensorValue" Is analoginpin the pin used to power pressure sensor (VCC)? or VOUT, which seems like sensor value to me?

You declared sensorValue as "const"

const byte sensorValue = 19 ;

That's because if you want to read a value into a variable then it should not be const (which is short for constant).

And, because analogRead() returns a unsigned 10-bit value (0-1023) it will not fit into a byte. a unsigned int would be the right type.

And why do you assign the value 19 to it? What do you think it means?

 if ( switch_pin == HIGH) {

That probably doesn't do what you want. It does not check the state of that pin to be high. It just checks the value assigned to switch_pin to be equal to HIGH (which is defined as 1). So it checks if 27 is equal to 1 which is of course false... Try

 if ( digitalRead(switch_pin) == HIGH) {
if ( switch_pin == LOW) {
[code]
Same here but because the state of the pin can only be HIGH or LOW it's per definition it's inverse. So replace it with
[code]else{
output_pressure = ( ( (voltage_v - (0.10 * (reference_voltage_mv / 1000) )) * (Pmax - Pmin) ) / (0.8 * (reference_voltage_mv / 1000) ) ) + Pmin;

output_presure, reference_voltage_mv, Pmax, Pmin are all not defined... Do you try to calculate the desire value?

 voltage_v = voltage_mv / 1000;

And instead of calculating the measured voltage and the desired voltage, just work with the ADC value as is and find the ADC value that would correspond to the desired voltage.

And both in the check of the switch as in the check for the sensor value you try to use the same outputs

    analogWrite(pumpPin, 255); // Pump. (0 is off) and (255 is on)
    digitalWrite(inletValvePin, HIGH); // open the air valve
    digitalWrite(exhaustValvePin, LOW); // close the exhaust valve

But which one is it? Which should control the state of those outputs? Now they will alternate very very quickly between the two different states assigned by the switch and the sensor.

And the IDE really really tries to help you to make the code more readable by for example indenting the code. Don't try to counteract. Press CTRL+T in the IDE and see how it tries to help. Looks better, doesn't it?

Okay managed to get this whole code to compile, but it’s still full of errors. Thanks so much Septillion! I believe you are right, when I give power to my circuit the pump just clicks on and off regardless of switch. My goal is to be able to flip the switch and have pump reach a desired value of PSI then maintain that level via FSM, then once i flip the switch off it will deflate down to 0 PSI. How could i go about coding to find ADC without having to worry about measured V? Thank you so much!

const int switch_pin = 27 ;
const int inletValvePin = 9 ;
const int exhaustValvePin = 10 ;
const int pumpPin = 8 ;
const int analogInPin = A0 ;
const int ADCFULLSCALE = 1023 ;
const int Pmax = 30 ;      //PSI
const int Pmin = 0 ;       //PSI   
const int desiredValue = 15 ;   //PSI
const int tolerance = 1 ; //PSI

//variables that will change

int sensorValue = 0 ;
int voltage_mv = 0 ;
int reference_voltage_mv = 0 ;
int voltage_v = 0 ;
int output_pressure = 0 ;



void setup() {
  // put your setup code here, to run once:
  pinMode(switch_pin, INPUT_PULLUP) ;




}

void loop() {
  // put your main code here, to run repeatedly:

// switch 
  if ( digitalRead(switch_pin == HIGH)) {
    digitalWrite(inletValvePin, HIGH); // turn pressure valve HIGH
    digitalWrite(pumpPin, HIGH); // make sure pump is on
    digitalWrite(exhaustValvePin, LOW); // turn exhaust valve LOW

  }

  else{
    digitalWrite(inletValvePin, LOW); // turn pressure valve HIGH
    digitalWrite(pumpPin, LOW); // pump is off
    digitalWrite(exhaustValvePin, HIGH); // turn exhaust valve HIGH
  }
    
   //pressure sensor calibration
   
    sensorValue = analogRead(analogInPin);

    // digital value of pressure sensor voltage
    voltage_mv =  (sensorValue * reference_voltage_mv) / ADCFULLSCALE;

    // pressure sensor voltage in mV
    voltage_v = voltage_mv / 1000;

    output_pressure = ( ( (voltage_v - (0.10 * (reference_voltage_mv / 1000) )) * (Pmax - Pmin) ) / (0.8 * (reference_voltage_mv / 1000) ) ) + Pmin;

   
    //FSM for pressure control
    
    if (sensorValue <= desiredValue) {
      analogWrite(pumpPin, 255); // Pump. (0 is off) and (255 is on)
      digitalWrite(inletValvePin, HIGH); // open the air valve
      digitalWrite(exhaustValvePin, LOW); // close the exhaust valve
    }

    if (sensorValue > desiredValue) {
      while (analogRead(A0) > (desiredValue - tolerance) ) {
        analogWrite(pumpPin, 190); // reduce speed of pump
        digitalWrite(inletValvePin, HIGH); // open the air valve
        digitalWrite(exhaustValvePin, LOW); // close the exhaust valve

}
}}
if ( digitalRead(switch_pin == HIGH))

Nope. Go look at what he wrote again. Coding is a very exacting thing. The compiler will not ever try to figure out what you meant or let something go because it is close enough. It always has to be exact.

A link to the schematic would normally look like this: http://softroboticstoolkit.com/low-cost-ep-circuit/assembly/schematic-wiring

That schematic has some strange things in it. Among other things, I notice that there is a peculiar way to deal with back-EMF (it probably does not do the job well), and that the Arduino outputs should have resistors in series with the bases of the output transistors.

Good Luck to you, especially if you are following these instructions.

Lol yes that is exactly what i'm trying to go off of vaj, ill look into more resistors. Okay delta, should i set up switch pin as an int = 0, like so?

int switch_pin = 0 ;

No. you should look at that line I quoted from your code and then look at the line in reply #6 (third code box) where you copied it from and see if you can spot the difference.

The only reason to make switch_pin 0 would be if you had the switch connected to pin 0. Pin 0 is one of the hardware serial pins. Those are best left alone until you know what you're doing.

According to the schematic at http://softroboticstoolkit.com/low-cost-ep-circuit/assembly/schematic-wiring, your switch is connected to pin #2, so you need:

const byte switch_pin = 2 ;

someplace ABOVE void setup() { .

According to the schematic, your switch is connected to pin 2 and a resistor to ground at one end, and to 5 volts at the other end. In that case, within the setup() function, you need:

pinMode(switch_pin, INPUT) ;

Okay, pin D2 is that pin 2? or pin 5? according to this:|500x283

Those fly back diodes need to be connected in parallel with each solenoid, also with the motor. The way they are shown in the diagram will do nothing.

@IBOYLES, spotted the difference?

And if talked about pin 2 in Arduino we mean digital pin 2 / D2. The other numbering in that image is just confusing and rarely used. There is a reason they labeled the pins ;)

And I agree with Dan95 and vaj4088. Use base resistors. Probably 1k is fine but it does depend on the motors/solenoids you connect. And those diodes need to be across the motor/solenoid (with the cathode (the side with the line) to the 6V line). Now they are useless.

And if you want to start/stop the process with the switch, don't turn on off the pumps with it... The switch determines if you want to read the ADC (the presure) and act on that or not. ;)