toggling a relay to bypass a switch

Hi all,

once again, i am guilty of being over ambitious with my plans and my skills don’t come anywhere near my thoughts.
But here goes.

i am trying to control an external light strip (Fuze, Lightstripper to be precise), by building an external circuit with a TIP122 darlington transistor,

the Light stripper has 3 modes (4 if you count “off” as one), that i want to turn on and off depending upon the proximity to an object. (a visual warning system if you will).

now i started to adapt the ping script and added in some perimeters, however this is when i came across the first of my problems, by having the digital pin read as “high” as i get within a danger range this will trigger the first “mode”, if the obstacle was to be taken out of the way the script would read low, yet the lightstripper;s on board circuit would still be in the first mode.

i guess what i am trying to say is that i need a way to toggle the modes depending upon the distance from the obstacle, whilst making them relevant (i.e when you are 20 cm away then mode 1, 10 cm away then mode 2, 5 cm away then mode 3), but should an object get in the way or be removed from the path, the relevant mode is chosen.

i have attached my poor coding for you to have a look at

/* Ping))) Sensor
 
 The circuit:
 	* +V connection of the PING))) attached to +5V
 	* GND connection of the PING))) attached to ground
 	* SIG connection of the PING))) attached to digital pin 7
 * SIG connection of the TIP circuit attached to Pin 8
 
 
 */

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;
int tip122Light = 8; // TIP122 lightstrip curcuit connected to pin 8
int warningZone1 = 10; // range in cm which is considered to be dangerous first cycle of the striplight
int warningZone2 = 7; // range in cm which is considered to be dangerous second cycle of the striplight 
int warningZone3 = 5; // range in cm which is considered to be dangerous third cycle of the striplight

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  pinMode(tip122Light, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);

  Serial.print(cm); // placed to see if the senor is reading correctly
  Serial.print("cm");
  Serial.println();

  if(cm > warningZone1)
  {
    digitalWrite(tip122Light, LOW); // if the distance from the sensor to the obstacal is greater than the distance advised in warningZone1 then leave the light off
  }
  if(cm <= warningZone1);
  { 
    digitalWrite(tip122Light, HIGH); // if the distance from the sensor to the obstacal is equal to or less than the distance advised in warninZOne 1 then turn the light on
  } 
  if(cm <= warningZone2);
  {
    digitalWrite(tip122Light, HIGH); // if the distance to the object decreases to a point that is equal to or less than warningZone2 then pulse high to the pin to hopefully move to the next mode
  }
  if(cm <= warningZone3);
  {
    digitalWrite(tip122Light, HIGH); // if the distance to the object decreases to a point that is equal to or less than warningZone3 then pulse high to the pin to hopefully move to the next mode
  }
  delay(100);
}


long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Before posting code, please autoformat it (Ctrl +T in the IDE)
When posting code, please use code tags (the # button above the smileys)
Your code appears to be missing a final curly brace.

sorry about that, thanks for your guidance.

regards

Martin

maybe if you nest your if statement.

if(cm > warningZone1) {   // only true if anything is within the furthest range
   if(cm > warningZone2) {  // only true if anything is within the midrange
          if(cm > warningZone3) {  // only true if anything is within the closerange
             digitalWrite(redLight, HIGH)
             }
      digitalWrite(yelloLight, HIGH)
      }
 digitalWrite(blueLight, HIGH)
 }

one the object is within the widest zone, the blue light goes on.
as it gets closer and gets within the next zone, the yello light comes on and the blue stays on
in the closest range, all lights come on.


if(cm > warningZone1) {   // only true if anything is within the furthest range
   if(cm > warningZone2) {  // only true if anything is within the midrange
          if(cm > warningZone3) {  // only true if anything is within the closerange
             digitalWrite(redLight, HIGH)
             digitalWrite(yelloLight, LOW)
             digitalWrite(blueLight, LOW)
             }
      digitalWrite(yelloLight, HIGH)
       digitalWrite(redLight, LOW)
       digitalWrite(blueLight, LOW)
      }
 digitalWrite(blueLight, HIGH)
digitalWrite(redLight, LOW)
digitalWrite(yelloLight, LOW)
 }