Analog momentary as latching

Good afternoon!

I am trying to get my onstar mirror in my car to act as a latching button press.
I have written enough code to get it to mostly function and now I am trying to get it the rest of the way perfect as possible.

When I press the button it switches on and off state every time it reads which makes the output 11 unpredictable whether I release in a high or low state unfortunately. Is there any way to get the button press to read more like just one button press? code and circuit as follows:

 This is an onstar mirror output converter
Circuit:
 Connect 5v out of arduino into pin 12 on mirror
 Connect 560 ohm pull down resistor from ground to A0
 Connect A0 arduino to pin 11 on mirror
*/


const int analogPin = A0; 
const int ledPin = 13;   
void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
 
  Serial.begin(9600);


}

void loop() {
  
  int analogValue = analogRead(analogPin);
 
 
 
 
  

  if (analogValue>260 && analogValue<=290) {
    digitalWrite(11, !digitalRead(11));
  }

 
 int sensorValue = digitalRead(11);
 
 

  Serial.println(sensorValue);
  delay(1);  

   if(digitalRead(2) == LOW)
   {digitalWrite(12, HIGH);
   digitalWrite(13, LOW);} 
 

  if(digitalRead(2) == HIGH)
  {digitalWrite(13, HIGH);
  digitalWrite(12, LOW);}  
}



type or paste code here

How are you connecting the Arduino to the onstar mirror?

What is the source of the analog signal?

What is the source of the digital signal?

Sounds like switch bouncing. Do you have reason to think it is not?

a7

  if (analogValue>260 && analogValue<=290) {
    digitalWrite(11, !digitalRead(11));
  }
 int sensorValue = digitalRead(11);

you write and read the same pin. what you want to have if it done?

is a common enough expression to toggle an output bit.

Outputs can be read, the reading reflects the value in the port output register which got there from digitalWrite().

a7

yes, but reading comes one more time and that was for me unusual

I am using the 5v out on the arduino to pin the onstar, onstar has 3 momentary buttons that has a resistor between each. With that signal which is 278 if memory serves me is the integer. I attempted to write a latching code which works if I release the button at the correct moment. The problem is that it switched from low to high and vice versa every time it reads the analog signal instead of each button press causing the "erratic" output.

I cannot post attachments as i just made this account, I'm trying to whip up on tinkercad the circuit in a simple format so that it is digestible.

const int analogPin = A0;

void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  static bool oldAnalogState = false;
  static bool AnalogState = false;
  AnalogState = analogRead(analogPin) > 260 && analogRead(analogPin)<290;
  delay(50);
  bool sensorValue = !digitalRead(11);
  if ( oldAnalogState != AnalogState) {
    oldAnalogState = AnalogState;
    Serial.println(sensorValue);
    digitalWrite(11, sensorValue);
  }
  digitalWrite(13, digitalRead(2));
  digitalWrite(12, !digitalRead(2));
}

If you read this in time, you could try it on the wokwi simulator here

no account login, free. Better?

a7

I just made a discord to share the factory mirror circuit diagram for the mirror, where you see 10v is where I connected to the 5v out on arduino and I have a pull down resistor to stabilize the analog reading.
The discord code is the following:
mTw6jmCG

I am struggling to understand this, can you point me in a direction so that I can read up on how that piece of code works?

does it work?

I have to try tomorrow morning as i have everything in my truck and im in a hotel right now lol

smart people never stop to thinking...

is it clear?

to be honest I haven't, all the hardware is in the truck so i cannot hook it up tonight

the part I'm trying to understand the static bool section

static is attribute to show "this variable valid only in this block" and is initialized with given value, for further working this line will be ignored.
bool is name of type boolean, both allowed in Arduino, store true or false.

that kind of makes sense, i will understand better once I upload the code

that does not quite work, I want the analog of 278 to act as a momentary button press, then based off of that press turn on and off a latching output 11.

void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  bool sensorValue = !digitalRead(11);
  if ( analogRead(A0) > 260 && analogRead(A0) < 290) {
    digitalWrite(11, sensorValue);
    delay(50);
    while (analogRead(A0) > 260 && analogRead(A0) < 290);
  }
  digitalWrite(13, digitalRead(2));
  digitalWrite(12, !digitalRead(2));
}
1 Like

don't you need to check that the button is pressed (LOW ==) and shouldn't you debounce it

  bool analogState = digitalRead(11);
  if (oldAnalogState != analogState) {
    oldAnalogState = analogState;
    delay (20);                               // debounce
    if (LOW == analogState) {
        ...

only Capitalize Constants