WHAT AM I DOING WRONG

Trying to create a input that will trigger my truck to start. im stuck at the moment because when i send pin 2 to high nothing is happening. here is my code

//Inputs
int BTn_on = 2; //Button "ON" 



//Outputs
int ign_ctrl = 8;                                               //Ignition Control Relay controlled by pin 8
int starter_ctrl = 9;                                             //Starter Control Relay controlled by pin 9
                                    

//DEFINITIONS
void setup()
{
  pinMode(BTn_on, INPUT); 
 
  pinMode(ign_ctrl, OUTPUT);
  pinMode(starter_ctrl, OUTPUT);
 
 
  Serial.begin(9600);                                     //Initialize the serial port with a baud rate of 9600
}




//Check function: Test for proper character set from BT
void loop()                           
{

  if (BTn_on == HIGH)                                          //Check if an BT=HIGH has been sent
  {
    Serial.println(BTn_on);
    on();                                                  
  }
  else
  {
    loop();                                                    //Return to beginning of program
  }
}




//Begin function: Turn on accessory and ignition
void on()

{

  digitalWrite(ign_ctrl, LOW);
  delay(500);
  digitalWrite(ign_ctrl, HIGH);         //Turn ignition ON
  delay(500); // 0.5 second delay
  starter();
  
  
  
}

void starter()
{

  digitalWrite(starter_ctrl, HIGH);  //Starter ON
  delay(1000);
  digitalWrite(starter_ctrl, LOW);   //Starter OFF
  loop();
  

}

you appear to be calling loop() recursively, e.g. remove the line

    loop();                 //Return to beginning of program

your program probably runs out of stack space

Like this? Its still not working

//Inputs
int BTn_on = 3; //Button "ON" 



//Outputs
int ign_ctrl = 8;                                               //Ignition Control Relay controlled by pin 8
int starter_ctrl = 9;                                             //Starter Control Relay controlled by pin 9
                                    

//DEFINITIONS
void setup()
{
  pinMode(BTn_on, INPUT); 
 
  pinMode(ign_ctrl, OUTPUT);
  pinMode(starter_ctrl, OUTPUT);
 
 
  Serial.begin(9600);                                     //Initialize the serial port with a baud rate of 9600
}




//Check function: Test for proper character set from BT
void loop()                           
{

  if (BTn_on == HIGH)                                          //Check if an BT=HIGH has been sent
  {
    Serial.println(BTn_on);
    on();                                                  
  }
  
}




//Begin function: Turn on accessory and ignition
void on()

{

  digitalWrite(ign_ctrl, LOW);
  delay(500);
  digitalWrite(ign_ctrl, HIGH);         //Turn ignition ON
  delay(500); // 0.5 second delay
  starter();
  
  
  
}

void starter()
{

  digitalWrite(starter_ctrl, HIGH);  //Starter ON
  delay(1000);
  digitalWrite(starter_ctrl, LOW);   //Starter OFF
  loop();
  

}

does the serial console display anything when you press the button?
can you put an oscilloscope on the outputs to view changes of state

No the serial doesn't return anything. this is my very first project with arduino. whats is oscilloscope?

how is the button connected?
do you have a multimeter to check the input voltage to the Arduino pin?
try change the print so it displays text

    Serial.println("BTn_on");

an oscilloscope allows you to view the signals on the outputs
try attaching LEDs to the outputs or check with a multimeter - that should show if anything is happening

Hi,
You have declared you input pin, but you don't digitalRead it to see if the button is pushed.

Tom... :slight_smile:

TomGeorge:
You have declared you input pin, but you don't digitalRead it to see if the button is pushed.

missed that one! too used to working on PIC microcontrollers where you can address pins directly

Ok i have it on digitalread now. it seamed to function now i cant get it to anything again. Sorry im new to this my button should be going from ground to input pin correct?

void loop()                           
{

Serial.println("Waiting for Button Press");
delay(500);

  if (digitalRead(BTn_on) == HIGH)                                         //Check if an BT=HIGH has been sent
  {
    Serial.println(BTn_on);
    on();                                                  
  }
  
}

have a look at

do you have a multimeter to check the input signal?

YAH it's working now. Thanks for all the help guys. no i just have to figure out a way to write a variable after the truck starts to let the loop know that ignition pin is on and to shut off the truck.

Hi,
How have you got Pin2 wired, if you are pulling it to 5V with the button, you need a 10K resistor from gnd to Pin2 to make sure it goes LOW when the button is not pressed.

How are you driving your relays, not directly from the Arduino pin, as it cannot supply enough current?

What model Arduino are you using?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Try this code that I have edited, I've made the pin numbers more descriptive.

//Inputs
int BTn_Pin = 3; //Button "ON"
bool BTn_on;
//Outputs
int ign_ctrlPin = 8;                 //Ignition Control Relay controlled by pin 8
int starter_ctrlPin = 9;             //Starter Control Relay controlled by pin 9


//DEFINITIONS
void setup()
{
  Serial.begin(9600);                                     //Initialize the serial port with a baud rate of 9600
  pinMode(BTn_Pin, INPUT);
  pinMode(ign_ctrlPin, OUTPUT);
  pinMode(starter_ctrlPin, OUTPUT);

  digitalWrite(ign_ctrlPin, LOW);   //Ignition OFF
  digitalWrite(starter_ctrlPin, LOW);   //Starter OFF
}

//Check function: Test for proper character set from BT
void loop()
{
  BTn_on = digitalRead(BTn_Pin);  // Read the Button input pin
  if (BTn_on == HIGH)              //Check if an BTn_on =HIGH has been sent
  {
    Serial.println(BTn_on);
    on();
  }
}

//Begin function: Turn on accessory and ignition
void on()
{

  digitalWrite(ign_ctrlPin, LOW);
  delay(500);
  digitalWrite(ign_ctrlPin, HIGH);         //Turn ignition ON
  delay(500); // 0.5 second delay
  starter();
}

void starter()
{
  digitalWrite(starter_ctrlPin, HIGH);  //Starter ON
  delay(1000);
  digitalWrite(starter_ctrlPin, LOW);   //Starter OFF

}

Thanks.. Tom.. :slight_smile:

NEW CODE

I need the button to do something else in value of running is = 1 then it will preform start command if the same button is pressed again it will preform the off command. Currently not working on my code. im trying here guys but i keep messing thing up please help. attached is my wiring diagram i know its cheesy but i threw it together quick.

//Inputs
int BTn_on = 2; //Button "ON" 



//Outputs
int ign_ctrl = 8;                                               //Ignition Control Relay controlled by pin 8
int starter_ctrl = 9;                                             //Starter Control Relay controlled by pin 9
                                    
//Inputs
int BTn_on = 2; //Button "ON" 
int BTn_off = 3;



//Outputs
int ign_ctrl = 8;                                               //Ignition Control Relay controlled by pin 8
int starter_ctrl = 9;                                             //Starter Control Relay controlled by pin 9
                                    


//Engine running yes or no?
int running;


//DEFINITIONS
void setup()
{
  pinMode(BTn_on, INPUT); 
  pinMode(BTn_on, INPUT_PULLUP);
   pinMode(BTn_off, INPUT); 
  pinMode(BTn_off, INPUT_PULLUP);
 
  pinMode(ign_ctrl, OUTPUT);
  pinMode(starter_ctrl, OUTPUT);
 
 
  Serial.begin(9600);                                     //Initialize the serial port with a baud rate of 9600
}




//Check function: Test for proper character set from BT
void loop()                           
{

//Serial.println("Waiting for Button Press");
//delay(1500);


if (running = 1)

{
 if (digitalRead(BTn_on) == LOW)                                         //Check if an BT=HIGH has been sent
  {
    Serial.println(BTn_on);
    on();                                                  
  }
}


if (running = 0);

{
 if (digitalRead(BTn_on) == LOW)                                         //Check if an BT=HIGH has been sent
  {
    Serial.println(BTn_on);
    off();                                                  
  }
}

  
}




//Begin function: Turn on accessory and ignition
void on()

{

  digitalWrite(ign_ctrl, LOW);
  Serial.println(ign_ctrl);
  delay(1000);
  digitalWrite(ign_ctrl, HIGH);  //Turn ignition ON
  running = 0;
  Serial.println(ign_ctrl);                  
  delay(1000); // 0.5 second delay
  starter();
  
  
  
}




void off()

{

  digitalWrite(ign_ctrl, LOW);
  Serial.println(ign_ctrl);
  delay(1000);
  digitalWrite(ign_ctrl, LOW);       //Turn ignition Off
  Serial.println(ign_ctrl);
  running = 1;                  
  delay(3000); // 0.5 second delay
  loop();
  
  
  
}




void starter()
{

  digitalWrite(starter_ctrl, HIGH);  //Starter ON
  Serial.println(starter_ctrl);
  delay(1100);
  Serial.println(starter_ctrl);
  digitalWrite(starter_ctrl, LOW);   //Starter OFF
  delay(3000);
  loop();

}

What a mess.
Command names like 'on()' and 'running' are used that are already used by the IDE (turning red).
Things are double double and a lot of white space that makes things hard to follow.

Start by telling us what the program should do (short form), before you (or us) have another go at writing code.
I'll start with the declarations (with meaningful names) and the setup().
Leo..

const byte buttonONpin = 2;
const byte buttonOFFpin = 3;
const byte ignitionControlPin = 8;
const byte starterControlPin = 9;
boolean engineRunning;


void setup() {
  Serial.begin(9600);
  pinMode(buttonONpin, INPUT_PULLUP); // button connected to pin and ground
  pinMode(buttonOFFpin, INPUT_PULLUP);
  pinMode(ignitionControlPin, OUTPUT);
  pinMode(starterControlPin, OUTPUT);
}

Ok i cleaned this up a bit. My goal is to have a variable "running" set to what happened last. the Void loop will check to see if the variable is equal to 1 or 0. if 1 then when input 2 btn is pressed it will jump to void on() then to void starter() then back to void loop an wait for button to be pressed again. If variable "running" is equal to 0 then jump to void off() then to Void loop() and wait for the button to be pressed again. its working but sometimes it jumps to start the engine when the engine has already been started.

//Inputs
int BTn_on = 2;                      //Button "ON" 

//Outputs
int ign_ctrl = 8;                    //ACC/Ignition Control Relay controlled by pin 8
int starter_ctrl = 9;                //Starter Control Relay controlled by pin 9
                                    
//Engine running yes or no?
int running1 = 1;                         //Variable 'running1'

//DEFINITIONS
void setup()
{
  pinMode(BTn_on, INPUT); 
  pinMode(BTn_on, INPUT_PULLUP);
  pinMode(ign_ctrl, OUTPUT);
  pinMode(starter_ctrl, OUTPUT);
  Serial.begin(9600);                    //Initialize the serial port with a baud rate of 9600
}

//Check function: Test for proper character set from BTn
void loop()                           
{

if (running1 = 1)                          //Check if Varible 'running1' is equal to 1
{
 if (digitalRead(BTn_on) == LOW)          //Check if Button has been pressed
  {
    Serial.println(BTn_on);               // Print BTn_on 
    on();                                 // Jump to Void on()                 
  }
}
if (running1 = 0);                         //Check if Varible 'running1' is equal to 0
{
 if (digitalRead(BTn_on) == LOW)          //Check if Button has been pressed                                  
  {
    Serial.println(BTn_on);               // Print BTn_on
    off();                                // Jump to Void off()                   
  }
}

  
}

//Begin function: Turn on ACC Then Jump to void Starter ()
void on()
{
  Serial.println("Void ON");          //Print Current Void
  digitalWrite(ign_ctrl, LOW);        //Turn ACC OFF
  Serial.println(ign_ctrl);           //Print ign_ctrl
  delay(1000);                        // 1 second delay
  digitalWrite(ign_ctrl, HIGH);       //Turn ACC ON
  running1 = 0;                        //Set Variable running1 to 0
  Serial.println(running1);           //Print running1           
  delay(1000);                        // 1 second delay
  starter();                          // Jump to void starter()
  
}

//Begin function: Turn off ACC Then return to Void loop()
void off()

{
  Serial.println("Void OFF");        //Print Current Void
  digitalWrite(ign_ctrl, LOW);       //Turn ACC OFF
  Serial.println(ign_ctrl);          //Print ign_ctrl
  delay(1000);                       // 1 second delay
  digitalWrite(ign_ctrl, LOW);       //Turn ACC OFF
  running1 = 1;                      //Set Variable running1 to 1
  Serial.println(running1);          //Print running1
  delay(3000);                       // 3 second delay
  loop();                            // Jump to void loop()
  
}

//Begin function: Turn Starter on for 1.1 seconds then return to void loop()
void starter()
{
  Serial.println("Void Starter");    //Print Current Void
  digitalWrite(starter_ctrl, HIGH);  //Starter ON
  Serial.println(starter_ctrl);      //Print starter_ctrl
  delay(1100);                       // 1.1 second delay
  Serial.println(starter_ctrl);      //Print starter_ctrl
  digitalWrite(starter_ctrl, LOW);   //Starter OFF
  delay(3000);                       // 3 second delay
  loop();                            // Jump to void loop()

}

Didn't you read my post.

on(); and void on() warns me to NOT use that word (turns red). Not in your IDE ?

Leo..

Hi,
Can I suggest as @wawa has that you start with this setup.
It logically names your pin assignments, like it does in my posting #11.

It may feel as if you are coding slowly, but it is the only way to learn.
We are trying to teach you to code logically so your code is easy to read and easy to debug any problems.

@wawa setup code.

const byte buttonONpin = 2;
const byte buttonOFFpin = 3;
const byte ignitionControlPin = 8;
const byte starterControlPin = 9;
boolean engineRunning;

void setup() {
  Serial.begin(9600);
  pinMode(buttonONpin, INPUT_PULLUP); // button connected to pin and ground
  pinMode(buttonOFFpin, INPUT_PULLUP);
  pinMode(ignitionControlPin, OUTPUT);
  pinMode(starterControlPin, OUTPUT);
}

Please read posts and TRY what is suggested.

Change void no() to something like void Ignition_On()
What IDE and operating system are you using?
Thanks.. Tom.. :slight_smile:

Okay not sure why the enginerunning variable is not resetting to zero after void on is ran.

//Inputs
int BTn_on = 2;                      //Button "ON" 

//Outputs
int ign_ctrl = 11;                    //ACC/Ignition Control Relay controlled by pin 8
int starter_ctrl = 7;                //Starter Control Relay controlled by pin 9
                                    
//Engine running yes or no?
int running1 = 1;                         //Variable 'running1'


[color=red]unsigned long Engineruntime = millis;

const long interval = 1000;
[/color]


//DEFINITIONS
void setup()
{
  pinMode(BTn_on, INPUT); 
  pinMode(BTn_on, INPUT_PULLUP);
  pinMode(ign_ctrl, OUTPUT);
  pinMode(starter_ctrl, OUTPUT);
  Serial.begin(9600);                    //Initialize the serial port with a baud rate of 9600
}

//Check function: Test for proper character set from BTn
void loop()                           
{


Serial.println(Engineruntime);

if (digitalRead(BTn_on) == LOW)
  {
  if (running1 == 1)                  //If Variable = 1 Jump to void on()
     on();
  else if (running1 == 0)             //If Variable = 1 Jump to void off()
    off();
  }

[color=red]if (Engineruntime >= 100000) {       // Turns the engine off after 
    off();[/color]
}
}

//Begin function: Turn on ACC Then Jump to void Starter ()
void on()
{
  Serial.println("Void ON");          //Print Current Void
  digitalWrite(ign_ctrl, LOW);        //Turn ACC OFF
  Serial.println(ign_ctrl);           //Print ign_ctrl
  delay(1000);                        // 1 second delay
  digitalWrite(ign_ctrl, HIGH);       //Turn ACC ON
  running1 = 0;                        //Set Variable running1 to 0
  Serial.println(running1);           //Print running1           
  delay(2000);                        // 1 second delay
[color=red]  Engineruntime = 0;                  //Sets the Variable back to 0[/color]
  
  starter();                          // Jump to void starter()
  
}

//Begin function: Turn off ACC Then return to Void loop()
void off()

{
  Serial.println("Void OFF");        //Print Current Void
  digitalWrite(ign_ctrl, LOW);       //Turn ACC OFF
  Serial.println(ign_ctrl);          //Print ign_ctrl
  delay(1000);                       // 1 second delay
  digitalWrite(ign_ctrl, LOW);       //Turn ACC OFF
  running1 = 1;                      //Set Variable running1 to 1
  Serial.println(running1);          //Print running1
  delay(1000);                       // 3 second delay
  loop();                            // Jump to void loop()
  
}

//Begin function: Turn Starter on for 1.1 seconds then return to void loop()
void starter()
{
  Serial.println("Void Starter");    //Print Current Void
  digitalWrite(starter_ctrl, HIGH);  //Starter ON
  Serial.println(starter_ctrl);      //Print starter_ctrl
  delay(1100);                       // 1.1 second delay
  Serial.println(starter_ctrl);      //Print starter_ctrl
  digitalWrite(starter_ctrl, LOW);   //Starter OFF
  delay(1000);                       // 3 second delay
  running1 = 0;
  loop();                            // Jump to void loop()

}

@mgiles6229, please do not cross-post. Threads merged.

ok i wont do that anymore