More automotive stuff

The concept raise my headlights, make one wink, then close both headlights. 2 Arduino relays and an Arduino should do it I figure, and a 10k resistor. This will temporarily operate them independent of the original controller, not something for driving around and will not turn the headlights on. When the button isn't pushed the car will operate as normal. Relays will connect the normal circuit in NC position and will switch 12v to the open circuit, (other side of motor,) when commanded by the Arduino. LOW operates the relays.

Still a noob, this is my first attempt at a sketch for this, cobbled together from blink and a couple others. Just wanted to see what you guys think. I have not tested it yet. I'm also thinking there may be easier ways to achieve this. Here she is. It compiles

/*
  Headlight wink

  Raises headlight buckets,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - Relays attached from pins 9 and 10 to ground
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int left_Bucket_Pin =  9;      // the number of the left bucket pin
const int right_Bucket_Pin =  10;      // the number of the right bucket pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


void setup(){
  // raise right bucket, with left, then right bucket goes down and back up both close
  pinMode(right_Bucket_Pin, OUTPUT);
  // initialize the Left bucket pin as an output:   
  pinMode(left_Bucket_Pin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  buttonState = digitalRead(buttonPin);
}   
void loop() {   
   // check if buttonpin is pressed. If it is buttonState is HIGH:
   if (buttonState == HIGH) {
   // lower right bucket:
  pinMode(right_Bucket_Pin, OUTPUT);
  // initialize the Left bucket pin as an output:   
  pinMode(left_Bucket_Pin, OUTPUT);
  // initialize the pushbutton pin as an input:digitalWrite(left_bucket_Pin, LOW);
   digitalWrite(right_Bucket_Pin, LOW);
   delay(1000);
   digitalWrite(right_Bucket_Pin, HIGH);
   delay(1000);
   digitalWrite(right_Bucket_Pin, LOW);
   delay(500);
   digitalWrite(left_Bucket_Pin, HIGH);
   digitalWrite(right_Bucket_Pin, HIGH);
while(1){}
}}

If all the Arduino is doing is to is to raise and lower the headlights then using the delay() function is OK but be aware that if you want to add functionality in the future it may cause problems.

As to the code, what are those stray pinMode()s doing in loop() ?
I suggest that you change the INPUT pinMode() to INPUT_PULLUP to activate the internal pullup resistors and wire the pushbutton to take the pin to GND when pressed. You will also need to change the program logic to detect a button press. That will ensure that the input is always in a known state. Mind you, as you never read the input in loop() in your code it won't work as it is now anyway.

Relays attached from pins 9 and 10 to groundBe careful. Relay boards sold for use with the Arduino are often active LOW, ie they turn on when the input is taken LOW.

A small thing, but it is good practice to use variables of the smallest appropriate type so why int for pin numbers and not byte, and the same for buttonState ?

    while (1) {}Why ?

UKHeliBob:
    while (1) {}Why ?

That would be appropriate if the button being pushed was the Arduino's reset button. The whole program runs once and gets stuck here until the next reset.

I already got rid of the strays. It was from confusion about it not compiling at first. I saw it after the post and removed it. I had to time the action for the wink, that's why the delays. They will still need tweaking once it's built. There will be a Pi with Arduino IDE in the car for making those adjustments and recompiling til I get it right. The LOW trigger I wrote about. Is there another option besides high to turn the relays off or turn the pins off I should say? I thought I needed the "while (1) {}" from another thread on having the loop only run once. So I can just eliminate that completely?

So far as the pin numbers vs byte and the same for buttonState... this was my first sketch, ever, and I assembled this from other sketches to the best of my ability/understanding. I know nothing of this byte thing. I appreciate the feedback and help, this was only an idea last night.

Gotcha MorganS! Now I know the difference and I will take that out.

If the relay module you have requires LOW to activate the relay then HIGH is the best option to de-activate it. The relay may have its own internal pullup, in which case you could switch the pin to input mode but that's and unusual way to do it, relying on the exact behaviour of that specific unit.

So, you guys think it'll work? Arduino side that is. Final version

/*
  Headlight wink

  Raises headlight buckets,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - Relays attached from pins 9 and 10 to ground
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int left_Bucket_Pin =  9;      // the number of the left bucket pin
const int right_Bucket_Pin =  10;      // the number of the right bucket pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


void setup(){
  // raise right bucket, with left, then right bucket goes down and back up both close
  pinMode(right_Bucket_Pin, OUTPUT);
  // initialize the Left bucket pin as an output:   
  pinMode(left_Bucket_Pin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  buttonState = digitalRead(buttonPin);
}   
void loop() {   
   // check if buttonpin is pressed. If it is buttonState is HIGH:
   if (buttonState == HIGH) {
   // lower right bucket:
   // initialize the pushbutton pin as an input:digitalWrite(left_bucket_Pin, LOW);
   digitalWrite(left_Bucket_Pin, LOW);
   digitalWrite(right_Bucket_Pin, LOW);
   delay(1000);
   digitalWrite(right_Bucket_Pin, HIGH);
   delay(1000);
   digitalWrite(right_Bucket_Pin, LOW);
   delay(500);
   digitalWrite(left_Bucket_Pin, HIGH);
   digitalWrite(right_Bucket_Pin, HIGH);

}}

So, you guys think it'll work?

No.

You are not reading the state of buttonPin in loop()

How is the input button wired ?

The button is going to be 5v to pin 2 with a 10k ohm resistor to ground in there, like I saw in someone's tutorial for something else!?! Not sure what I need more for it to read the button on pin 2.

Using a pinMode() of INPUT_PULLUP simplifies the wiring and does not need an external resistor but as long as the input is always in a known state and not floating at an uncertain voltage you will be OK.

Now, what about reading the state of the input in loop() ?

Okay, I've actually got a working circuit in front of me. I had to add the while (1) {} back in because it kept repeating on it's own on power. The whole thing works with the built in reset button. But I want it work with another momentary switch elsewhere. So I just need to figure out the pushbutton thing and It's good.

You are correct the Arduino isn't recognizing anything on pin 2. I am not understanding exactly what I need to do different here. The candles I used for the seance to channel Touring are burning out. You're going to have to dumb it down for me :slight_smile:

Video of it working on reset

So I just need to figure out the pushbutton thing and It's good.

Put the code you have in a function, then in loop() read an input to determine when a button becomes pressed, not when it is pressed. When it becomes pressed call you function.

I have tried many different things at this point searching for some function that will work. I understand that I have to change it from IS pressed to BECOMES pressed, so I'm understanding that part

I've tried serial 9600, if else, input_pullup, each time when it doesn't work I have to pull all of that stuff back out because it autosaved. I turned that off in preferences so I can just close and re-open the file now. So I'm pretty much back to where I started with something that only works on reset and (another problem) cycles through everything when the Arduino powers up. I don't want it to cycle the headlights on startup. The code as it is right now...

/*
  Headlight wink

  Raises headlight buckets,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - Relays attached from pins 9 and 10 to ground
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int left_Bucket_Pin =  9;      // the number of the left bucket pin
const int right_Bucket_Pin =  10;      // the number of the right bucket pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


void setup(){
  // raise right bucket, with left, then right bucket goes down and back up both close
  pinMode(right_Bucket_Pin, OUTPUT);
  // initialize the Left bucket pin as an output:   
  pinMode(left_Bucket_Pin, OUTPUT);
  // initialize the pushbutton pin as an input:
  buttonState = digitalRead(buttonPin);
  pinMode(buttonPin, INPUT);
}   
void loop() {   
   // check if buttonpin is pressed. If it is buttonState is HIGH:
   // initialize the pushbutton pin as an input:digitalWrite(left_bucket_Pin, LOW);
   digitalWrite(left_Bucket_Pin, LOW);
   digitalWrite(right_Bucket_Pin, LOW);
   delay(2000);
   digitalWrite(right_Bucket_Pin, HIGH);
   delay(2000);
   digitalWrite(right_Bucket_Pin, LOW);
   delay(4000);
   digitalWrite(left_Bucket_Pin, HIGH);
   digitalWrite(right_Bucket_Pin, HIGH);
   while (1) {}
   
}

To see how to determine when an input becomes HIGH or LOW look at the StateChangeDetection example in the IDE.

Alright, that was quick. Added what I needed from the suggested example aaaannndd... the button works. Only one more hitch. On power on the relays go to on, (lift the headlights,) until the button is pressed. Is there a way to power on to a ready state? Will be waiting for another reply, this is almost finished :smiley:

Is there a way to power on to a ready state?

Set them to the required startup state in setup() ?

I tried this digitalWrite(x, LOW); pinMode(x, OUTPUT); in the setup and it still starts the pins HIGH

Ha! I'm an idiot :slight_smile: LOW is on and HIGH is off... problem solved. Fully working code...

/*
  Headlight wink

  Raises headlight buckets,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - Relays attached from pins 9 and 10 to ground
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int left_Bucket_Pin =  9;      // the number of the left bucket pin
const int right_Bucket_Pin =  10;      // the number of the right bucket pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int lastButtonState = 0;

void setup(){
  // raise right bucket, with left, then right bucket goes down and back up both close
  digitalWrite(right_Bucket_Pin, HIGH);
  pinMode(right_Bucket_Pin, OUTPUT);
  // initialize the Left bucket pin as an output:   
  digitalWrite(left_Bucket_Pin, HIGH); 
  pinMode(left_Bucket_Pin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  
}   
void loop() {   
   // read the pushbutton input pin:
   buttonState = digitalRead(buttonPin);
   // compare the buttonState to its previous state
   if (buttonState != lastButtonState) {
   // if the state has changed, increment the counter
   if (buttonState == HIGH) {
   digitalWrite(left_Bucket_Pin, LOW);
   digitalWrite(right_Bucket_Pin, LOW);
   delay(2000);
   digitalWrite(right_Bucket_Pin, HIGH);
   delay(2000);
   digitalWrite(right_Bucket_Pin, LOW);
   delay(2000);
   digitalWrite(right_Bucket_Pin, HIGH);
   digitalWrite(left_Bucket_Pin, HIGH);
   } else {
   digitalWrite(left_Bucket_Pin, LOW);
   digitalWrite(right_Bucket_Pin, LOW);
   
   }   }   }

Thank you for all the help. I got it working on both my Arduino UNO R3 and Pro Micro/Leonardo. Both seemto be a little twitchy though, going off on their own here and there, moreso with the Leonardo.

Both seemto be a little twitchy though, going off on their own here and there, moreso with the Leonardo.

Is this in the vehicle or on the bench ? The automotive environment is very electrically noisy.

PGTMR2:
The whole thing works with the built in reset button. But I want it work with another momentary switch elsewhere.

This will do the winky thing for so long as the momentary button is held down. Normally, of course, you'd just hit the button once and let the lights do their thing.

void loop() {
  if(digitalRead(buttonPin) == HIGH /* or LOW, depending on how it's wired */) {
    /* the code to do your winky thing goes here */
  }
}