Go Down

Topic: Var resetting to 0 after change rather than sticking (Read 97 times) previous topic - next topic

KiwiStag

The code is in relation to a remote control starting script using the boot opening button on my remote locking system (aftermarket).

I can start the car fine - however with the stopcar() function enabled it runs immediately after the startcar() function.

startcar() changes var remoteButton to 2 but after running the function later changes it to 0.

vin checks that the remote button has been pressed (the locking unit pulses 12V for 1 second so is long enough for the voltmeter to pick up state change.

Code: [Select]

// #include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
SoftwareSerial BT(10, 11);
// creates a "virtual" serial port/UART
// connect BT module TX to D10
// connect BT module RX to D11
// connect BT Vcc to 5V, GND to GND

#define RELAY1  41  // IG ACC
#define RELAY2  42  // IG Run
#define RELAY3  43  // IG Start
#define RELAY4  44  // Inside Light
#define RELAY5  45  // Park Lights
#define RELAY6  46  // Horn
#define RELAY7  47
#define RELAY8  48

int remoteVolt = A2;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; //
float R2 = 7500.0; //
int valueV = 0;

int handbrakeVolt = A3;
float voutHB = 0.0;
float vinHB = 0.0;
int valueVH = 0;

const int gearSwitchPin = 5;
const int buzzer = 24;

int remoteButton;

void setup() {


  pinMode(gearSwitchPin, INPUT);

  // set digital pin to control as an output
  //  pinMode(13, OUTPUT);
  // set the data rate for the SoftwareSerial port
  Serial.begin(9600);
  BT.begin(9600);

  BT.println("BT On");
  Serial.println("System Init");

  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  pinMode(RELAY5, OUTPUT);
  pinMode(RELAY6, OUTPUT);
  pinMode(RELAY7, OUTPUT);
  pinMode(RELAY8, OUTPUT);

  digitalWrite(RELAY1, HIGH); // ACC
  digitalWrite(RELAY2, HIGH); // Ignition
  digitalWrite(RELAY3, HIGH); // Start
  digitalWrite(RELAY4, HIGH); // Inside Light
  digitalWrite(RELAY5, HIGH); // Park Lights
  digitalWrite(RELAY6, HIGH); // Horn
  digitalWrite(RELAY7, HIGH);
  digitalWrite(RELAY8, HIGH);

}

void failStart() {
  digitalWrite(RELAY6, LOW);
  delay(150);
  digitalWrite(RELAY6, HIGH);
  delay(150);
  digitalWrite(RELAY6, LOW);
  delay(150);
  digitalWrite(RELAY6, HIGH);
  delay(150);
  digitalWrite(RELAY6, LOW);
  delay(150);
  digitalWrite(RELAY6, HIGH);
  delay(150);
  digitalWrite(RELAY6, LOW);
  delay(150);
  digitalWrite(RELAY6, HIGH);
}

void startCar() {
  Serial.println("Starting preignition warning");
  digitalWrite(RELAY6, LOW);
  digitalWrite(RELAY5, LOW);
  delay(200);
  digitalWrite(RELAY6, HIGH);
  digitalWrite(RELAY5, HIGH);
  delay(400);
  digitalWrite(RELAY6, LOW);
  digitalWrite(RELAY5, LOW);
  delay(200);
  digitalWrite(RELAY6, HIGH);
  // digitalWrite(RELAY5, HIGH);
  delay(750);
  digitalWrite(RELAY6, LOW);
  digitalWrite(RELAY5, LOW);
  delay(200);
  digitalWrite(RELAY6, HIGH);
  digitalWrite(RELAY5, HIGH);
  delay(400);
  digitalWrite(RELAY6, LOW);
  digitalWrite(RELAY5, LOW);
  delay(200);
  digitalWrite(RELAY6, HIGH);
  digitalWrite(RELAY5, HIGH);
  delay(1000);


  Serial.println("Starting ACC Ignition Position.");
  digitalWrite(RELAY1, LOW);
  delay(2700);
  Serial.println("Starting IGN Position.");
  digitalWrite(RELAY2, LOW);
  delay(1000);
  tone(buzzer, 5000);
  delay(500);
  noTone(buzzer);     // Stop sound...

  // Check Handbrake
  valueVH = analogRead(handbrakeVolt);
  voutHB = (valueVH * 5.0) / 1024.0;
  vinHB = voutHB / (R2 / (R1 + R2));

  delay(1500);
  if    (vinHB < 5)
  {
    Serial.println("Starting Car.");
    digitalWrite(RELAY3, LOW);
    delay(1500);
    digitalWrite(RELAY3, HIGH);
    //    delay(300000);
  }

  /*
    digitalWrite(RELAY2, HIGH);
    delay(1000);
    digitalWrite(RELAY1, HIGH);
  */
  if    (vinHB > 5) {
    failStart();
  }
  if    (gearSwitchPin == HIGH) {
    failStart();
  }
}


void stopCar() {
  digitalWrite(RELAY2, HIGH);
  delay(1000);
  digitalWrite(RELAY1, HIGH);
}

void loop() {


  valueV = analogRead(remoteVolt);
  vout = (valueV * 5.0) / 1024.0;
  vin = vout / (R2 / (R1 + R2));

  Serial.print("Remote InputV: ");
  Serial.println(vin, 2);
  Serial.print("Button Push status: ");
  Serial.println(remoteButton);
  delay(800);
  if    ((vin > 5) && (remoteButton != 2)) {
    remoteButton = 2;
    Serial.println(remoteButton);
    startCar();
    delay(2000);
  }


  delay(150);
  if    ((vin > 5) && (remoteButton == 2)) {
    Serial.print("Stop Car Button");
    Serial.println(remoteButton);
    stopCar();
    int    remoteButton = 1;
  }

}



I've tried setting int remoteButton = 0; without success either.

What am I missing that is causing the code to simply move to the shutdown function after remoteStart goes to 0?

arduarn

Not sure if I've understood your problem description, but int remoteButton = 1; was surely meant to be remoteButton = 1;

Quote
What am I missing that is causing the code to simply move to the shutdown function after remoteStart goes to 0?
To summarise your code:
Code: [Select]
  if    ((vin > 5) && (remoteButton != 2)) {
    remoteButton = 2;
    startCar();
  }

  if    ((vin > 5) && (remoteButton == 2)) {
    stopCar();
    remoteButton = 1;
  }

vin remains > 5 for both ifs. The body of the first if sets remoteButton to 2 so that the second if body is immediately executed.

KiwiStag

Sorry - yes, the int remoteButton is invalid and I only put it in to see if it would make a difference (which it didn't).

Code changed:
Code: [Select]

void loop() {

 
  valueV = analogRead(remoteVolt);
  vout = (valueV * 5.0) / 1024.0;
  vin = vout / (R2 / (R1 + R2));
 
  Serial.print("Remote InputV: ");
  Serial.println(vin, 2);
  Serial.print("Button Push status: ");
  Serial.println(remoteButton);
  delay(800);
  if    ((vin > 5) && (remoteButton != 2)) {
   
   
    startCar();
    delay(2000);
    remoteButton = 2;
    Serial.print("Mark as started: ");
    Serial.println(remoteButton);
  }

 
  delay(150);
    if    ((vin > 5) && (remoteButton == 2)) {
    Serial.print("Stop Car Button: ");
    Serial.println(remoteButton);
    stopCar();
    remoteButton = 1;     
    }
 
}



Serial outout:
Code: [Select]
Remote InputV: 0.00
Button Push status: 0
Remote InputV: 9.91
Button Push status: 0
Starting preignition warning
Starting ACC Ignition Position.
Starting IGN Position.
Starting Car.
Mark as started: 2
Stop Car Button2
Remote InputV: 0.02
Button Push status: 1
Remote InputV: 0.00

KiwiStag

I found the issue - was looking at it in totally the wrong light!
Since the two functions run one after another the vin variable doesn't update or reset so is still at the initial reading :)

Code: [Select]

if    ((vin > 5) && (remoteButton != 2)) {
    
    
    startCar();
    delay(2000);
    remoteButton = 2;
    Serial.print("Mark as started: ");
    Serial.println(remoteButton);
    Serial.print("Remote InputV After: ");
    Serial.println(vin, 2);
vin = 0;
  }

Go Up