Nitrous Controller - Please Help

Please go to the end.

Arm = digitalRead (2, LOW);
Horn = digitalRead (3,HIGH);

For one thing, digitalRead() only takes one argument.

if (Arm = HIGH & gear >2 & winSwitch = HIGH)                //Activate relay 1 when armed, in proper gear, and in window//
{

For another thing, "=" is an assignment operator. You want "==" to compare two values.

Thing three: 'arm' and 'Arm' are different variables.

4th thing: You have code (particularly pinMode() calls) outside of any function. That is not legal syntax.

Try turning up the warning level and fixing bugs until all the messages are gone:

sketch_mar12a:11: error: expected constructor, destructor, or type conversion before '(' token
 pinMode (gear, INPUT);        // Analog voltage from gear position sensor//
         ^
sketch_mar12a:12: error: expected constructor, destructor, or type conversion before '(' token
 pinMode (Arm, INPUT);         //Arming Switch//
         ^
sketch_mar12a:13: error: expected constructor, destructor, or type conversion before '(' token
 pinMode (Horn, INPUT);        //Horn button//
         ^
sketch_mar12a:14: error: expected constructor, destructor, or type conversion before '(' token
 pinMode (winSwitch, INPUT);   //Window switch will send 12v when nitrous may be used//
         ^
sketch_mar12a:15: error: expected constructor, destructor, or type conversion before '(' token
 pinMode (rePower1, OUTPUT);   //Nitrous stage 1 relay//
         ^
sketch_mar12a:16: error: expected constructor, destructor, or type conversion before '(' token
 pinMode (rePower2, OUTPUT);   //Nitrous stage 2 relay//
         ^
sketch_mar12a:20: error: expected constructor, destructor, or type conversion before '(' token
 pinMode (hrelay, OUTPUT);     //Horn relay//
         ^
/Users/john/Documents/Arduino/sketch_mar12a/sketch_mar12a.ino: In function 'void setup()':
sketch_mar12a:25: error: too many arguments to function 'int analogRead(uint8_t)'
 gear = analogRead (A0, 0);
                         ^
In file included from sketch/sketch_mar12a.ino.cpp:1:0:
/Applications/Arduino1.8.0.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h:136:5: note: declared here
 int analogRead(uint8_t);
     ^
sketch_mar12a:26: error: 'Arm' was not declared in this scope
 Arm = digitalRead (2, LOW);
 ^
sketch_mar12a:26: error: too many arguments to function 'int digitalRead(uint8_t)'
 Arm = digitalRead (2, LOW);
                          ^
In file included from sketch/sketch_mar12a.ino.cpp:1:0:
/Applications/Arduino1.8.0.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h:135:5: note: declared here
 int digitalRead(uint8_t);
     ^
sketch_mar12a:27: error: 'Horn' was not declared in this scope
 Horn = digitalRead (3,HIGH);
 ^
sketch_mar12a:27: error: too many arguments to function 'int digitalRead(uint8_t)'
 Horn = digitalRead (3,HIGH);
                           ^
In file included from sketch/sketch_mar12a.ino.cpp:1:0:
/Applications/Arduino1.8.0.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h:135:5: note: declared here
 int digitalRead(uint8_t);
     ^
sketch_mar12a:28: error: void value not ignored as it ought to be
 rePower1=digitalWrite (4,LOW);
         ^
sketch_mar12a:29: error: void value not ignored as it ought to be
 rePower2=digitalWrite (5,LOW);
         ^
sketch_mar12a:30: error: 'hrealy' was not declared in this scope
 hrealy=digitalWrite (6,LOW);
 ^
sketch_mar12a:31: error: too many arguments to function 'int digitalRead(uint8_t)'
 winSwitch=digitalRead (7,LOW);
                             ^
In file included from sketch/sketch_mar12a.ino.cpp:1:0:
/Applications/Arduino1.8.0.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h:135:5: note: declared here
 int digitalRead(uint8_t);
     ^
/Users/john/Documents/Arduino/sketch_mar12a/sketch_mar12a.ino: In function 'void loop()':
sketch_mar12a:41: error: 'Arm' was not declared in this scope
 if (Arm = HIGH & gear >2 & winSwitch = HIGH)                //Activate relay 1 when armed, in proper gear, and in window//
     ^
/Users/john/Documents/Arduino/sketch_mar12a/sketch_mar12a.ino:41:23: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
 if (Arm = HIGH & gear >2 & winSwitch = HIGH)                //Activate relay 1 when armed, in proper gear, and in window//
                       ^
sketch_mar12a:41: error: lvalue required as left operand of assignment
 if (Arm = HIGH & gear >2 & winSwitch = HIGH)                //Activate relay 1 when armed, in proper gear, and in window//
                                      ^
sketch_mar12a:44: error: expected ';' before '}' token
   }
   ^
sketch_mar12a:45: error: 'Arm' was not declared in this scope
 if (Arm = HIGH & gear >2 & Horn = LOW & winSwitch = HIGH)   //Activate relays 1 and 2 when all the above are true and horn is pushed.// 
     ^
/Users/john/Documents/Arduino/sketch_mar12a/sketch_mar12a.ino:45:23: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
 if (Arm = HIGH & gear >2 & Horn = LOW & winSwitch = HIGH)   //Activate relays 1 and 2 when all the above are true and horn is pushed.// 
                       ^
sketch_mar12a:45: error: 'Horn' was not declared in this scope
 if (Arm = HIGH & gear >2 & Horn = LOW & winSwitch = HIGH)   //Activate relays 1 and 2 when all the above are true and horn is pushed.// 
                            ^
sketch_mar12a:45: error: lvalue required as left operand of assignment
 if (Arm = HIGH & gear >2 & Horn = LOW & winSwitch = HIGH)   //Activate relays 1 and 2 when all the above are true and horn is pushed.// 
                                                   ^
sketch_mar12a:48: error: expected ';' before 'digitalWrite'
   digitalWrite (rePower2, HIGH)
   ^
sketch_mar12a:54: error: expected ';' before '}' token
   }
   ^
exit status 1
expected constructor, destructor, or type conversion before '(' token

The actual error messages would provide a lot of useful information to us.

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

ebweiss2:
I see no bad syntax.

The compiler sees a LOT of bad syntax. Once you have gotten the compiler to verify your code with “Compiler warnings:” turned up to “All” (see Preferences) then you will have fixed your syntax errors. If the code then doesn’t do what you want you have to find and correct the semantic errors. The compiler isn’t much help with that. :slight_smile:

Man, that was all messed up. Guess my memory of coding was worse than I thought. Here is a revised version that managed to compile. I’ll see if it works soon. Thanks for the help. If you see anything else please let me know.

int arm= 2;
int horn= 3;
int rePower1= 4;
int rePower2= 5;
int winSwitch= 7;
int gear= A0;
int hrelay= 6;


void setup() {

pinMode (gear, INPUT);        // Analog voltage from gear position sensor//
pinMode (arm, INPUT);         //Arming Switch//
pinMode (horn, INPUT);        //Horn button//
pinMode (winSwitch, INPUT);   //Window switch will send 12v when nitrous may be used//
pinMode (rePower1, OUTPUT);   //Nitrous stage 1 relay//
pinMode (rePower2, OUTPUT);   //Nitrous stage 2 relay//
pinMode (hrelay, OUTPUT);     //Horn relay//

  
  Serial.begin(9600);

}




void loop() {

analogRead (gear);
digitalRead (arm);
digitalRead (horn);
digitalRead (winSwitch);
                                                              // read the input on analog pin 0//
  int gear = analogRead(A0);
                                                            // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V)//
  float voltage = gear * (5.0 / 1023.0);

if (arm == HIGH & gear >2 & winSwitch == HIGH);                //Activate relay 1 when armed, in proper gear, and in window//
{
  digitalWrite (rePower1, HIGH);
  }
if (arm == HIGH & gear >2 & horn == LOW & winSwitch == HIGH)   //Activate relays 1 and 2 when all the above are true and horn is pushed.// 
{
  digitalWrite (rePower1, HIGH); 
  digitalWrite (rePower2, HIGH);
    }

else if (horn == LOW);                                           //When above conditions are not met, horn button activates horn relay//
{
  digitalWrite (hrelay, HIGH);
  }
    
  // print out the value you read:
  Serial.println(voltage);
}

You should probably be using '&&' (logical OR) instead of '&' (bitwise OR).

  analogRead (gear);
  digitalRead (arm);
  digitalRead (horn);
  digitalRead (winSwitch);

When you call a function to get a return value, it is customary to store the result in a variable so you can use it.

I see you calculate a voltage and don't use it for anything. I suspect you wanted to use it to calculate the gear number.

OK, I’ve done a bit of work on this. No error messages but not working. Anyone know why? Just so you know what I’m doing.

These conditions must be met for the first solenoid to fire.

  1. System armed - pin 2
  2. Voltage from gear position sensor over 3V (not first gear).
  3. Signal from external window switch.

For the second solenoid to fire all the above must be true, plus the horn button must be depressed.

Should I release the horn, the 2nd solenoid should stop. Should any of the above conditions become untrue, both solenoids should be cut and the horn button should operate the horn relay instead of solenoid B.

Here is the code so far.

int armIN = 2;
int hornIN = 3;
int solA = 4;
int solB = 5;
int winSwitch = 7;
int gearIN = A0;
int hrelay = 6;

int armval = LOW;
int hornINval = LOW;
int winSwitchval = LOW;
int gearval = 0;
int solAval = LOW;
int solBval =LOW;
int hrelayval =LOW;



void setup() {

pinMode (gearIN, INPUT);        // Analog voltage from gear position sensor//
pinMode (armIN, INPUT);         //Arming Switch//
pinMode (hornIN, INPUT);        //Horn button//
pinMode (winSwitch, INPUT);     //Window switch will send 12v when nitrous may be used//
pinMode (solA, OUTPUT);         //Nitrous stage 1 relay//
pinMode (solB, OUTPUT);         //Nitrous stage 2 relay//
pinMode (hrelay, OUTPUT);       //Horn relay//

  Serial.begin(9600);

}

void loop() {


                                                              // read the input on analog pin 0//
  gearval = analogRead(gearIN);
  armval = digitalRead(armIN);
  hornINval = digitalRead (hornIN);
  winSwitchval = digitalRead (winSwitch);
 
                                                            // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V)//
  float voltage = gearval * (5.0 / 1023.0);

if ((armval == HIGH) && (voltage >3))               //Activate relay 1 when armed, in proper gear, and in window//
{
  while (winSwitch == HIGH)
  {
  digitalWrite (solA, HIGH);
  }
}
  solA = LOW;
   
if ((armval == HIGH) && (voltage >3))   //Activate relays 1 and 2 when all the above are true and horn is pushed.// 
{
  while ((winSwitch == HIGH) && (hornINval == HIGH))
  {
  digitalWrite (solA, HIGH); 
  digitalWrite (solB, HIGH);
  }
  solA = LOW;
  solB = LOW;
    }

else if ((armval == LOW) || (winSwitchval == LOW) || (voltage <3) && ( hornINval == HIGH))                                         //When above conditions are not met, horn button activates horn relay//
{
  digitalWrite (hrelay, HIGH);
    
    }
    
  // print out the value you read:
  Serial.println(voltage);
  delay (500);
}

You enter a while loop if winSwitch is HIGH, but winSwitch will never change value in your while loop, so your code is trapped there.
You better use if else to fire your solenoids.

if (armval == HIGH && voltage >3 && winSwitch == HIGH) {
    digitalWrite (solA, HIGH);
    }
  else {
    digitalWrite (solA, LOW);  // you had forgot digitalWrite here
    }

Let your second if statement only control solB as all conditions are the same except HornINval, and if solB is activated, solA has already been activated a few lines up.

Changed things a bit. I added delays for diagnostics as I'm using an LED.

When all conditions are met, pin 6 outputs 5V for the 500ms. The other pins output nothing.

I got rid of while loops and nested an if loop.

int armIN = 2;
int hornIN = 3;
int solA = 4;
int solB = 5;
int winSwitch = 7;
int gearIN = A0;
int hrelay = 6;

int armval = LOW;
int hornINval = LOW;
int winSwitchval = LOW;
int gearval = 0;
int solAval = LOW;
int solBval =LOW;
int hrelayval =LOW;



void setup() {

pinMode (gearIN, INPUT);        // Analog voltage from gear position sensor//
pinMode (armIN, INPUT);         //Arming Switch//
pinMode (hornIN, INPUT);        //Horn button//
pinMode (winSwitch, INPUT);     //Window switch will send 12v when nitrous may be used//
pinMode (solA, OUTPUT);         //Nitrous stage 1 relay//
pinMode (solB, OUTPUT);         //Nitrous stage 2 relay//
pinMode (hrelay, OUTPUT);       //Horn relay//

 Serial.begin(9600);

}

void loop() {
                                                            // read the input on analog pin 0//
 gearval = analogRead(gearIN);
 armval = digitalRead(armIN);
 hornINval = digitalRead (hornIN);
 winSwitchval = digitalRead (winSwitch);

                                                           // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V)//
 float voltage = gearval * (5.0 / 1023.0);

if (armval == HIGH && voltage >3 && winSwitch == HIGH)               //Activate relay 1 when armed, in proper gear, and in window//
{
 
 digitalWrite (solA, HIGH);
delay (500);

if (hornINval == HIGH)                                                                   //Activate relay 2 when all the above are true and horn is pushed.// 
  {
 digitalWrite (solB, HIGH);
 delay (500);
 }
}  

else                                                                //When above conditions are not met, horn button activates horn relay//
  {
  digitalWrite (solA, LOW);
  digitalWrite (solB, LOW);  
  }

if (hornINval == HIGH)
{ 
 
 digitalWrite (hrelay, HIGH);
 delay (500);
}
digitalWrite (hrelay, LOW);
 // print out the value you read:
 Serial.println(voltage);
 delay (500);
}

Gabriel_swe:
You enter a while loop if winSwitch is HIGH, but winSwitch will never change value in your while loop, so your code is trapped there.
You better use if else to fire your solenoids.

My thinking is that when the external window switch stops sending signal, it will fall out of the while loop. Is that wrong?

OK, the horn relay always works regardless of conditions - even when it shouldn’t. Nothing else fires. I eliminated the gear signal to simplify.

int armIN = 2;
int hornIN = 3;
int solA = 4;
int solB = 5;
int winSwitch = 7;
int gearIN = A0;
int hrelay = 6;

int armval = LOW;
int hornINval = LOW;
int winSwitchval = LOW;
int gearval = 0;
int solAval = LOW;
int solBval =LOW;
int hrelayval =LOW;



void setup() {

pinMode (gearIN, INPUT);        // Analog voltage from gear position sensor//
pinMode (armIN, INPUT);         //Arming Switch//
pinMode (hornIN, INPUT);        //Horn button//
pinMode (winSwitch, INPUT);     //Window switch will send 12v when nitrous may be used//
pinMode (solA, OUTPUT);         //Nitrous stage 1 relay//
pinMode (solB, OUTPUT);         //Nitrous stage 2 relay//
pinMode (hrelay, OUTPUT);       //Horn relay//

  

}

void loop() {
                                                             // read the input on analog pin 0//
  gearval = analogRead(gearIN);
  armval = digitalRead(armIN);
  hornINval = digitalRead (hornIN);
  winSwitchval = digitalRead (winSwitch);
 
                                                            // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V)//
  float voltage = gearval * (5.0 / 1023.0);

while (armval == HIGH && winSwitchval == HIGH)               //Activate relay 1 when armed, in proper gear, and in window//
{
 gearval = analogRead(gearIN);
  armval = digitalRead(armIN);
  winSwitchval = digitalRead (winSwitch);
  digitalWrite (solA, HIGH);
  
}
digitalWrite (solA, LOW);

while (armval == HIGH && winSwitchval == HIGH && hornINval == HIGH)                                                                 //Activate relay 2 when all the above are true and horn is pushed.// 
   {
  gearval = analogRead(gearIN);
  armval = digitalRead(armIN);
  hornINval = digitalRead (hornIN);
  winSwitchval = digitalRead (winSwitch);
  digitalWrite (solB, HIGH);
  digitalWrite (solA, HIGH);
  
  }
  digitalWrite (solA, LOW);
  digitalWrite (solB, LOW);
  gearval = analogRead(gearIN);
  armval = digitalRead(armIN);
  hornINval = digitalRead (hornIN);
  winSwitchval = digitalRead (winSwitch); 
 

while ((hornINval == HIGH) && (armval == LOW || voltage <3 || winSwitch == LOW))
 
{  
  digitalWrite (hrelay, HIGH);
  hornINval = digitalRead (hornIN);
}

digitalWrite (hrelay, LOW);
 
}

You do a lot of reading of the same inputs.
Anything in loop() will be repeated thousands of times a second. Why not just read your inputs once at the top of loop, then check the conditions with "if" instead of "while".

evanmars:
You do a lot of reading of the same inputs.
Anything in loop() will be repeated thousands of times a second. Why not just read your inputs once at the top of loop, then check the conditions with "if" instead of "while".

I tried using if and it didn't work. I need for example a solenoid to stay open as long as a button is pressed and then close when it's released. If I'm not mistaken, an if loop will toggle it open until other code toggles it back off. I don't want that.

I would say you were on the right track in post#10, but in the last you are back with while. While loop is blocking everything else. While is good if you want to halt your program flow until a condition is met, but with multiple conditionss doing different things, I rarely find while suitable.

Now you are updating your inputs inside while loop, and that is good, you have a way out of the while loop, BUT - a big but comes ahead.

You enter your first while if armval and winSwitchval are both high. SolA activate. So far all good, but as long those conditions are true, you are stuck. HornINval does nothing. As soon as armval or winSwitchval becomes low, you are free from the while loop and solA deactivate.

As one of those two are low, you will never enter the second while and solB never activate.

Even if you simultaneous have all 3 conditions met to fire solA and solB, first while will be true and block you from activate solB. A solution to that is to switch place of your two first while loops.

If you want solA active and a few seconds later activate solB without first deactivate A, you should work more on while free code in post#10.

Try this.
edit: Added a line to also turn off solB if condition for solA is no longer met.

int armIN = 2;
int hornIN = 3;
int solA = 4;
int solB = 5;
int winSwitch = 7;
int gearIN = A0;
int hrelay = 6;
int armval = LOW;
int hornINval = LOW;
int winSwitchval = LOW;
int gearval = 0;
int solAval = LOW;
int solBval = LOW;
int hrelayval = LOW;

void setup() {
  pinMode (gearIN, INPUT);        // Analog voltage from gear position sensor//
  pinMode (armIN, INPUT);         //Arming Switch//
  pinMode (hornIN, INPUT);        //Horn button//
  pinMode (winSwitch, INPUT);     //Window switch will send 12v when nitrous may be used//
  pinMode (solA, OUTPUT);         //Nitrous stage 1 relay//
  pinMode (solB, OUTPUT);         //Nitrous stage 2 relay//
  pinMode (hrelay, OUTPUT);       //Horn relay//
  Serial.begin(9600);
}

void loop() {
  // read the input on analog pin 0//
  gearval = analogRead(gearIN);
  armval = digitalRead(armIN);
  hornINval = digitalRead (hornIN);
  winSwitchval = digitalRead (winSwitch);

  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V)//
  float voltage = gearval * (5.0 / 1023.0);

  if (armval == HIGH && voltage > 3 && winSwitch == HIGH) {    //Activate relay 1 when armed, in proper gear, and in window//
    digitalWrite (solA, HIGH);
    if (hornINval == HIGH) {      //Activate relay 2 when all the above are true and horn is pushed.//
      digitalWrite (solB, HIGH);
    } else {
      digitalWrite (solB, LOW);
    }
  } else { 
digitalWrite (solB, LOW);    //No solB without solA allowed.
digitalWrite (solA, LOW);
  }

  if (hornINval == HIGH && (armval == LOW || voltage < 3 || winSwitch == LOW)) {
    digitalWrite (hrelay, HIGH);
  } else {
    digitalWrite (hrelay, LOW);
  }
  // print out the value you read:
  Serial.println(voltage);
  delay (500);
}

Hi,
WHY DID YOU ERASE YOUR ORIGINAL POSTING?

If you want to contribute to this forum as it helps you, leaving all your posts untouched, will help any other user with your type of problem.
Now this entire thread is useless. We have no idea what your problem was or any basic information.

Tom.... >:( >:( >:( >:( >:(

OK, got it working. Yay! Seems my loop syntax was bad and evidently using "or" and "and" in the same loop causes problems. Anyway, feel free to use this code. I will integrate the window switch some day - for now I already have one so why bother.

    /* 

    2 stage nitrous controller by Eric Weiss
    Must be armed and out of first gear.
    Opens solenoid 1 when signal is received from external window switch. (RPM + TPS window).  
    Solenoid 2 is open when horn button is pushed.  
    Solenoids will only open when armed, not in 1st gear (via gear position signal) and in window.
    Otherwise, horn button defaults back to horn operation. 
*/

int buttonPin = 11;
int ledPin = 13;
int armSwitch = 10;
int solA = 9;
int solB = 8;
int horn = 7;
int gearPin = A0;
int winSwitch = 6;

void setup() {

  pinMode (buttonPin, INPUT);
  pinMode (ledPin, OUTPUT);
  pinMode (armSwitch, INPUT);
  pinMode (gearPin, INPUT);
  pinMode (solA, OUTPUT);
  pinMode (solB, OUTPUT);
  pinMode (horn, OUTPUT);
  pinMode (winSwitch, INPUT);
 
}

void loop() {
 
  if (digitalRead (winSwitch) == HIGH && digitalRead (buttonPin) == HIGH && digitalRead (armSwitch) == HIGH && analogRead (gearPin) >100)
{
  digitalWrite (ledPin, HIGH);
  digitalWrite (solA, HIGH);
  digitalWrite (solB, HIGH);
}
     
  else
{
  digitalWrite (ledPin, LOW);
  digitalWrite (solA, LOW);
  digitalWrite (solB, LOW);
  digitalWrite (winSwitch, LOW);
  digitalWrite (armSwitch, LOW);
  digitalWrite (buttonPin, LOW);
}  

     if (digitalRead (winSwitch) == HIGH && digitalRead (armSwitch) == HIGH && analogRead (gearPin) >100)
    {
      digitalWrite (ledPin, HIGH);
      digitalWrite (solA, HIGH);
      digitalWrite (solB, LOW);
    }

    else
    {
  digitalWrite (ledPin, LOW);
  digitalWrite (solA, LOW);
  digitalWrite (solB, LOW);
  digitalWrite (winSwitch, LOW);
  digitalWrite (armSwitch, LOW);
  digitalWrite (buttonPin, LOW);
}  



  if (digitalRead (winSwitch) == LOW || digitalRead (armSwitch) == LOW)
{
      if (digitalRead (buttonPin) == HIGH)
      {
            digitalWrite (horn, HIGH);
      }
      
        else
        {
          digitalWrite (horn, LOW);
        }
}
      
   digitalWrite (horn, LOW);
}