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
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.
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
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 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.