Author Topic: Cornering lights/turning assistant and fog lights-again (but different problem)  (Read 7472 times)

Hants

  • Newbie
  • *
  • Posts: 14
I am struggling with adding another functionality - both lamps ON. Has anybody got an idea how to add this feature without destroying code that has been written by Dr_P?

Code: [Select]
/* Light turning assistant for cars (cornering lights)
   Credits to user Dr_P at http://mjlorton.com forum
   
*/


int fogL = 8;                               // 55W Relay for L fog lamp
int fogR = 7;                               // 55W Relay for R fog lamp
int indL = 4;                               // Circuit between multiswitch and indicator relay
int indR = 2;                               // Circuit between multiswitch and indicator relay




void setup()
{
 pinMode(fogL, OUTPUT);
 pinMode(fogR, OUTPUT);
 pinMode(indL, INPUT);
 pinMode(indR, INPUT);
}

void loop()

{

      {
     int i=100;                                    // counter initialized with "done" value
     if(digitalRead(indR))                 
            {
            i=0;                                   //if indicators were reversed, the cycle needs to be reset to full 5s
            while(digitalRead(indR) && i<100)
                  {
                  i++;
                  digitalWrite(fogR, HIGH);
                  digitalWrite(fogL, LOW);
                  delay(50);
                  }
            }
      if(digitalRead(indL))                        //same as right side
            {
            i=0;
            while(digitalRead(indL) && i<100)
                  {
                  i++;
                  digitalWrite(fogR, LOW);
                  digitalWrite(fogL, HIGH);
                  delay(50);
                  }
            }
      if(!digitalRead(indR) && !digitalRead(indL))      // no indicators currently on
            if(i==100)                                  //neither indicators were on OR the 5s cycle is over
                  {
                  digitalWrite(fogR, LOW);
                  digitalWrite(fogL, LOW);
                  }
            else                                        //one or both indicators were on for less than 5s
                  while(!digitalRead(indR) && !digitalRead(indL) && i<100)      //but not any more
                        {
                        i++;                           //finish the 5s cycle in the last foglight configuration
                        delay(50);
                        }
      }
}

Mr Eastwood

  • Sr. Member
  • ****
  • Posts: 274
Hi,  is this what you mean ?


Code: [Select]
int fogL = 8;                               // 55W Relay for L fog lamp
int fogR = 7;                               // 55W Relay for R fog lamp
int indL = 4;                               // Circuit between multiswitch and indicator relay
int indR = 2;                               // Circuit between multiswitch and indicator relay
int swBoth = ?????;


void setup()
{
 pinMode(fogL, OUTPUT);
 pinMode(fogR, OUTPUT);
 pinMode(indL, INPUT);
 pinMode(indR, INPUT);
 pinMode(swBoth, INPUT);
}

void loop()

{

      {
     int i=100;                                    // counter initialized with "done" value
     if(digitalRead(indR))                 
            {
            i=0;                                   //if indicators were reversed, the cycle needs to be reset to full 5s
            while(digitalRead(indR) && i<100)
                  {
                  i++;
  if(!swBoth) {
digitalWrite(fogR, HIGH);
digitalWrite(fogL, LOW);
  }
  else {
digitalWrite(fogR, HIGH);
digitalWrite(fogL, HIGH);
  }
                  delay(50);
                  }
            }
      if(digitalRead(indL))                        //same as right side
            {
            i=0;
            while(digitalRead(indL) && i<100)
                  {
                  i++;
  if(!swBoth) {
digitalWrite(fogR, LOW);
digitalWrite(fogL, HIGH);
  }
  else {
digitalWrite(fogR, HIGH);
digitalWrite(fogL, HIGH);
  }
                  delay(50);
                  }
            }
      if(!digitalRead(indR) && !digitalRead(indL))      // no indicators currently on
            if(i==100)                                  //neither indicators were on OR the 5s cycle is over
                  {
                  digitalWrite(fogR, LOW);
                  digitalWrite(fogL, LOW);
                  }
            else                                        //one or both indicators were on for less than 5s
                  while(!digitalRead(indR) && !digitalRead(indL) && i<100)      //but not any more
                        {
                        i++;                           //finish the 5s cycle in the last foglight configuration
                        delay(50);
                        }
      }
}
Hey! Frisbee! Far out!

Hants

  • Newbie
  • *
  • Posts: 14
Sadly.. Nope.

Now both lamps are going ON for 5 seconds when I switch 'indL' or 'indR.'

'swBoth' doesn't do anything

 :'(


Thanks for trying!

dr_p

  • Jr. Member
  • **
  • Posts: 64
In addition to what you had initially, you want a push-button to light both fog lights? And I suppoze it has priority over everything else?

Mr Eastwood

  • Sr. Member
  • ****
  • Posts: 274
Sadly.. Nope.
Now both lamps are going ON for 5 seconds when I switch 'indL' or 'indR.'
'swBoth' doesn't do anything

doh! sorry i missed the digital read!   hopefully now when swBoth input is switched "high" both fog lights will come on when you use the LH RH indicator; and when "low" it will work as normal;  if not, I'm going to throw all my microchip pics into the bin!! ;-)

replace ...
Code: [Select]
if(!swBoth) {

with..
Code: [Select]
if( !digitalRead(swBoth) ) {
Hey! Frisbee! Far out!

Mr Eastwood

  • Sr. Member
  • ****
  • Posts: 274
ok, the swBoth (high) as an over-ride both lights version,  maybe? :-)

Code: [Select]
int fogL = 8;                               // 55W Relay for L fog lamp
int fogR = 7;                               // 55W Relay for R fog lamp
int indL = 4;                               // Circuit between multiswitch and indicator relay
int indR = 2;                               // Circuit between multiswitch and indicator relay
int swBoth = ?????;


void setup()  {
 pinMode(fogL, OUTPUT);
 pinMode(fogR, OUTPUT);
 pinMode(indL, INPUT);
 pinMode(indR, INPUT);
 pinMode(swBoth, INPUT);
}

void loop() {
 
     int i=100; // counter initialized with "done" value

     if(digitalRead(indR)) {
            i=0; //if indicators were reversed, the cycle needs to be reset to full 5s
            while(digitalRead(indR) && i<100) {
                  i++;
  if(! digitalRead(swBoth) ) {
digitalWrite(fogR, HIGH);
digitalWrite(fogL, LOW);
  }
  else {
digitalWrite(fogR, HIGH);
digitalWrite(fogL, HIGH);
  }
                  delay(50);
}
}

if(digitalRead(indL)) { //same as right side
            i=0;
            while(digitalRead(indL) && i<100) {
                  i++;
  if(! digitalRead(swBoth) ) {
digitalWrite(fogR, LOW);
digitalWrite(fogL, HIGH);
  }
  else {
digitalWrite(fogR, HIGH);
digitalWrite(fogL, HIGH);
  }
                  delay(50);
            }
}

    if(!digitalRead(indR) && !digitalRead(indL)) { // no indicators currently on
   
  if(! digitalRead(swBoth) ) {

if(i==100) { //neither indicators were on OR the 5s cycle is over
if(! digitalRead(swBoth) ) {
digitalWrite(fogR, LOW);
digitalWrite(fogL, LOW);
}
else {
digitalWrite(fogR, HIGH);
digitalWrite(fogL, HIGH);
}
}
else  { //one or both indicators were on for less than 5s
while(!digitalRead(indR) && !digitalRead(indL) && i<100 && !digitalRead(swBoth)  ) { //but not any more
i++;                           //finish the 5s cycle in the last foglight configuration
delay(50);
                        }
}
  }
  else {
digitalWrite(fogR, HIGH);
digitalWrite(fogL, HIGH);
  }
}
}
Hey! Frisbee! Far out!

Hants

  • Newbie
  • *
  • Posts: 14
In addition to what you had initially, you want a push-button to light both fog lights? And I suppoze it has priority over everything else?

Yes :-)


Jucole - yes! it is working now!!!


I am shocked how large, long and complicated the new code is now, but if it has to be like that, than it is OK.

THANK YOU, GUYS


After few changes - final code looks like that (reversed connections on relays!):

Code: [Select]
/* Light turning assistant for cars (cornering lights)
   Credits to users Dr_P and jucole at http://mjlorton.com forum
   
   Version designed for use with relays equipped with photocoupler (JD-VCC pin)
   (Pinout bank 1: [[JD-VCC, VCC]], GND;                        Pinout bank 2: GND, IN1, IN2, VCC)
   More details about reversed connection: http://arduino.cc/forum/index.php?topic=79745.0
 
   
*/

int fogL = 8;                               // 55W Relay for L fog lamp
int fogR = 7;                               // 55W Relay for R fog lamp
int indL = 4;                               // Circuit between multiswitch and indicator relay
int indR = 2;                               // Circuit between multiswitch and indicator relay
int swBoth = 3;                           // Separate switch for both fog lights


void setup()  {
 pinMode(fogL, OUTPUT);
 pinMode(fogR, OUTPUT);
 pinMode(indL, INPUT);
 pinMode(indR, INPUT);
 pinMode(swBoth, INPUT);
}

void loop() {
   
     int i=100; // counter initialized with "done" value

     if(digitalRead(indR)) {
            i=0; //if indicators were reversed, the cycle needs to be reset to full 5s
            while(digitalRead(indR) && i<100) {
                  i++;
   if(! digitalRead(swBoth) ) {
digitalWrite(fogR, LOW);
digitalWrite(fogL, HIGH);
   }
   else {
digitalWrite(fogR, LOW);
digitalWrite(fogL, LOW);
   }
                  delay(50);
}
}

if(digitalRead(indL)) { //same as right side
            i=0;
            while(digitalRead(indL) && i<100) {
                  i++;
   if(! digitalRead(swBoth) ) {
digitalWrite(fogR, HIGH);
digitalWrite(fogL, LOW);
   }
   else {
digitalWrite(fogR, LOW);
digitalWrite(fogL, LOW);
   }
                  delay(50);
            }
}

     if(!digitalRead(indR) && !digitalRead(indL)) { // no indicators currently on
     
  if(! digitalRead(swBoth) ) {

if(i==100) { //neither indicators were on OR the 5s cycle is over
if(! digitalRead(swBoth) ) {
digitalWrite(fogR, HIGH);
digitalWrite(fogL, HIGH);
}
else {
digitalWrite(fogR, LOW);
digitalWrite(fogL, LOW);
}
}
else  { //one or both indicators were on for less than 5s
while(!digitalRead(indR) && !digitalRead(indL) && i<100 && !digitalRead(swBoth)  ) { //but not any more
i++;                           //finish the 5s cycle in the last foglight configuration
delay(50);
                        }
}
   }
   else {
digitalWrite(fogR, LOW);
digitalWrite(fogL, LOW);
   }
}
}

« Last Edit: May 15, 2013, 08:07:02 PM by Hants »

Mr Eastwood

  • Sr. Member
  • ****
  • Posts: 274
If you want a shorter version this one will work too.

Code: [Select]
void turnOffFogs() {
digitalWrite(fogL, LOW);
digitalWrite(fogR, LOW);
}

void loop() {
if(digitalRead(swBoth)) {
digitalWrite(fogL, HIGH);
digitalWrite(fogR, HIGH);
mode = 3;
timer=0;
}
else {
if(digitalRead(indL)) {
digitalWrite(fogL, HIGH);
digitalWrite(fogR, LOW);
mode = 1;
timer=0;
}
if(digitalRead(indR)) {
digitalWrite(fogL, LOW);
digitalWrite(fogR, HIGH);
mode = 2;
timer=0;
}
}
if( (mode == 1 || mode == 2) && !digitalRead(indL) && !digitalRead(indR) && !digitalRead(swBoth) ) {
delay(50);
timer++;
if(timer==100) {
turnOffFogs();
mode = 0;
}
}
if( mode == 3 && !digitalRead(swBoth)) {
turnOffFogs();
mode = 0;
}    
}
Hey! Frisbee! Far out!

Mr Eastwood

  • Sr. Member
  • ****
  • Posts: 274
If anyone wants the same code but suitable for the microchip 16F84A @2Mhz this will get you started.

Code: [Select]
#include <pic.h>

#define fogL RA0
#define fogR RA1
#define indL RB0
#define indR RB1
#define swBoth RB2

void delay() {
int i;
for(i=0; i<800;i++) {
asm("nop");
}
}

void turnOffFogs() {
fogL = 0;
fogR = 0;
}

void main() {
int mode;
int timer;
TRISA = 0;
TRISB = 255;
fogL = 0;
fogR = 0;
while(1) {
if(swBoth) {
fogL = 1;
fogR = 1;
mode = 3;
timer=0;
}
else {
if(indL) {
fogL = 1;
fogR = 0;
mode = 1;
timer=0;
}
if(indR) {
fogL = 0;
fogR = 1;
mode = 2;
timer=0;
}
}
if( (mode == 1 || mode == 2) && !indL && !indR && !swBoth ) {
delay();
timer++;
if(timer==100) {
turnOffFogs();
mode = 0;
}
}
if( mode == 3 && !swBoth) {
turnOffFogs();
mode = 0;
}
}
}
Hey! Frisbee! Far out!

dr_p

  • Jr. Member
  • **
  • Posts: 64
maybe now it's a good time to add switch debouncing, but this time do it yourself ;)

Hants

  • Newbie
  • *
  • Posts: 14
Dr_p  It not necessary as every switch is very properely grounded :-) and actually - during very long tests, I havent experienced any problems with switch bouncing

Anyway - code is interesting and is a good learning ground to me. And I am impressed with your knowledge.


I'll post a youtube link after few days to show you how it works in practice.