help on basic programmation

hi, i am making a new arduino project that will replace my car window switch, the utility of that is to make an auto roll-up when the car are locked/armed.
i have a question about the timing, i want the arduino to activate the roll-up relay to close the window, but for a specified time and disable the relay after this time. here is what i have write:

//WINDOW RELAY
        const int LT_WINDOW_UP_RELAY = 1;
        const int LT_WINDOW_DOWN_RELAY = 2;

//SINGLE WINDOW ROLL-UP TIME
        int SINGLE_WINDOW_ROLL_UP_TIME = 10; //SEC

//LEFT WINDOW ROLL-UP FONCTION
        void LT_WINDOW_ROLL_UP()
        {
          digitalWrite(LT_WINDOW_UP_RELAY, LOW); //START ROLL-UP
          unsigned long LT_WINDOW_ROLL_UP_TIMER= millis(); //INITIALISE TIMER
          if (millis() < (LT_WINDOW_ROLL_UP_TIMER + (SINGLE_WINDOW_ROLL_UP_TIME *= 1000)) { LT_WINDOW_ROLL_UP() }
          else { digitalWrite(LT_WINDOW_UP_RELAY, HIGH); }
        }

i’m pretty sure that code will work… but what i’m not… as the program will repaeat the roll-up fonction until the requested time are elapsed, i suppose that they will not do anything else… so the program stop until the window are not completely close ??

thank you :stuck_out_tongue:

Do you want the function to return only after the roll-up is complete?

//LEFT WINDOW ROLL-UP FUNCTION
void LT_WINDOW_ROLL_UP()
        {
        digitalWrite(LT_WINDOW_UP_RELAY, LOW); //START ROLL-UP
        delay(SINGLE_WINDOW_ROLL_UP_TIME * 1000UL);
        digitalWrite(LT_WINDOW_UP_RELAY, HIGH); }
        }

Or do you want to do other things at the same time the window is going up? If so, you want something like this:

 //LEFT WINDOW ROLL-UP FUNCTION
void LT_WINDOW_ROLL_UP() {
    static unsigned long left_up_start_time = 0;
    if (!LeftWindowAtTop) {
        if (left_up_start_time == 0) {
            digitalWrite(LT_WINDOW_UP_RELAY, LOW); //START ROLL-UP
            left_up_start_time = millis();
        }
        if (millis() - left_up_start_time > SINGLE_WINDOW_ROLL_UP_TIME * 1000UL) {
            LeftWindowAtTop = true;
            left_window_start_time = 0;
            digitalWrite(LT_WINDOW_UP_RELAY, HIGH); 
         }
    }
}

You would call it repeatedly from loop() and when the global boolean LeftWindowAtTop goes to TRUE you know the time has expired. Between calls you can do other things like roll up the right window.

As an exercise, this might be interesting; as a permanent installation, it's dangerous. Sooner or later, some creature's hand, arm or head will be sticking through the window when the sequence starts, with possibly disastrous results. That might be you, a child, or a pet.

if (something_in_window) {
  if ( (something == child) || (something == my_dog) ) {
    digitalWrite(LT_WINDOW_UP_RELAY, HIGH);  // Stop motor
  }
  else {
    if (something == adult_human) {
      shout("Look out!");  // Trigger audible alarm
    }
  }
}

Please reconsider whether you want to implement this.

I assume "real" window roller-uppers have some form of feedback (hall sensor to measure motor speed, ammeter to measure motor load, etc) to detect an obstacle?

tylernt:
I assume "real" window roller-uppers have some form of feedback (hall sensor to measure motor speed, ammeter to measure motor load, etc) to detect an obstacle?

In my experience, "real" window roller-uppers require manual operation. The manufacturers can't risk crushing a child's windpipe with an automatic roller-upper so they require you hold the button. They also try to make the buttons so it is hard for a child to lean or stand on in a way that makes a window go up.

johnwasser:
In my experience, "real" window roller-uppers require manual operation. The manufacturers can't risk crushing a child's windpipe with an automatic roller-upper so they require you hold the button. They also try to make the buttons so it is hard for a child to lean or stand on in a way that makes a window go up.

Hm, I think you're right, that explains why the switches sometimes seem to be stupidly designed to go the opposite way I expect. Makes sense.

Still, I thought I remembered a Volkswagen factory sales brochure from the late 90s that said it could roll the windows up when you arm the alarm. Can't find it now, though, all I can find are aftermarket add-ons so maybe I'm misremembering.

tylernt:
I assume "real" window roller-uppers have some form of feedback ... to detect an obstacle?

I've never seen an electric car window that doesn't require the operator to maintain a fairly substantial pressure on a switch for the whole time the window is closing. I've seen lots of cars that will roll a window open with just the touch of a button, but none that will roll a window closed. Presumably, that feature is omitted because there's not an economically feasible way to make sure that the window doesn't grab something on the way up.

I'm surprised to see that alarm companies are actually selling gizmos for this purpose. I can't find anything about safety features, though, other than an Amazon listing for an apparently unbranded device that says the word, "safety" - a bit unconvincingly, I might add. Can you give a link to an installation manual for an aftermarket gizmo that does this?

Most of these devices seem to sell for less than an Arduino Uno. I don't believe that they incorporate any missing safety features.

Geographical info: I'm referring to cars sold for use in the US. Outside the US, somebody else does the driving - I have no experience with driver-side controls of cars made for other markets.

tmd3:
Can you give a link to an installation manual for an aftermarket gizmo that does this?

I rather suspect they don't. Don't see how they could without a sensor on the motor, and that's pretty unlikely to be there in a way that your average Joe can easily / will bother to hook up.

tmd3:
I've never seen an electric car window that doesn't require the operator to maintain a fairly substantial pressure on a switch for the whole time the window is closing.

I have a window lifter system that raises both windows when the alarm is switched on, no manual intervention required (or possible) after hitting the keyfob button to arm the alarm. I don't think this is particularly unusual.

I haven't seen such on unmodified cars in the US, though it seems that this feature is available as an aftermarket modification. I can't find a manufacturer that discloses its location and isn't in China.

Is your system supplied by the vehicle manufacturer, or is it an aftermarket add-on?

That's a slightly awkward question to answer, because it's a TVR and the boundary between production and aftermarket is slightly blurred for this manufacturer. The module was fitted by a dealer, which arguably makes it aftermarket fitment, but it was supplied by the same company that supplied the OEM alarm system and I believe it could have been fitted as OEM equipment if that had been specified. A quick google indicates that these devices are still widely available for household names such as Clifford, Meta, Toad in the UK and I don't know of any regulation or policy which would prevent them from getting UK type approval or complying with construction and use regs in the UK.

I have no idea whether there's a rule against it in the US. But, we take a certain national pride in the litigiousness of our citizens; I suspect that concern for legal exposure keeps manufacturers from installing that feature in a new car bound for this market.

I wouldn't install this. It appears, though, that plenty of other would, and have, and I'm not seeing any news stories about kids garroted by car windows. I've heard that our distant ancestors rolled up their windows by turning little cranks by hand, and they were usually able to secure their cars. I'll continue to hold the buttons until the windows are all the way up.

Update: Yesterday I drove a car whose window closed with one touch of a momentary switch. It was a Toyota Corolla, a rental car, and it looked like it was made for the US market. It had an automatic reverse feature that seemed to be quite sensitive. When the window encountered resistance while it was closing, it reversed direction and opened fully. I was able to trigger it with pressure from just my index finger.

I have all but claimed in this thread that this feature isn't available in the US. I am wrong. This feature is indeed available on cars in the US. It has a safety feature to keep it from closing against mechanical resistance, apparently sensitive enough to keep it from accidentally throttling a child or dog whose neck is in the way.

[Edit: Fixed grammar]

tmd3:
I was able to trigger it with pressure from just my index finger.

In the wintertime when the window is frosted/iced over, I wonder if it will false-detect on the resistance and refuse to roll up. Might make for some cold drives...

...but maybe you can hold the switch engaged to override the safety feature.