toggle switch to chose between 1M and 2M distance

Hi
I have ultrasonic device to measure water tank 1M
i want to add toggle switch to make the device chose between 1M and 2M

so i don’t need to open it and change the code every time,
1- i don’t know if i connect the toggle switch right or there is a better way!
2- as a default i want it to measure 1M, if i switch the toggle it will be 2M

i attached file

anybody can help me :roll_eyes:

Untitled Document (2.13 KB)

Can I ask What changes do you make to your code at the moment when going from 1M to 2M?

Why a switch? Why not a simple jumper/solder blob? (I asked this yesterday, but you threw your toys out of the pram and asked for the thread to be deleted)

change this

int highWater = 30;        // These values allow to calculate % of full
int lowWater = 95;        // SRF04 hangs above water (lower distance = more water)

to this

int highWater = 30;        // These values allow to calculate % of full
int lowWater = 200;        // SRF04 hangs above water (lower distance = more water)

Why a switch?
Why not a simple jumper/solder blob?
(I asked this yesterday, but you threw your toys out of the pram and asked for the thread to be deleted)

its a loooong story :roll_eyes:

i want a toggle switch because i put my arduino in closed box
i don’t want to open the device for the jumper :cold_sweat:

Read the switch, set the variable. I really don't see the problem.

i just know how to code for sensors special for ultrasonic and making new library i am not that good in other subjects =( i don't know from where i will start and how the code will be !! i will give you my library XD for better pings for HC-SR04, i think that's a good deal XD

narzan:
i just know how to code for sensors special for ultrasonic and making new library
i am not that good in other subjects =(
i don’t know from where i will start and how the code will be !!
i will give you my library XD for better pings for HC-SR04, i think that’s a good deal XD

try like this, connecting only one of the two positions on the toggle to pin5:

#include <LiquidCrystal.h>

#define ECHOPIN 3                            // Pin to receive echo pulse
#define TRIGPIN 4                            // Pin to send trigger pulse
#define STATUSPIN 13                        // Use for troubleshooting

int highWater = 30;        // These values allow to calculate % of full
int lowWater = 95;        // SRF04 hangs above water (lower distance = more water)

const int togglePin = 5;
int lastSwitchReading;

byte symbol[8] = {         // Custom character for LCD display
  B00000, 
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B00000,
};

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);    // Assign pins

// Utility function for flashing STATUSPIN
void flashLed(int pin, int times, int wait) {
    for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);

    if (i + 1 < times) {
      delay(wait);
    }
  }
}


void setup() 
{
  pinMode(togglePin, INPUT_PULLUP);
  lcd.begin(16,2);
  lcd.print("   AQUA LEVEL ");
  lcd.createChar(0, symbol);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);

  delay(3000);       // Show application name for 3 seconds.

}

void loop() 
{
  int switchState = digitalRead(togglePin);
  if (switchState != lastSwitchReading)
  {
    if (switchState == LOW)
    {
      highWater = 30;
      lowWater = 95;
    }
    else
    {
      highWater = 30;
      lowWater = 200;
    }
  }
  lastSwitchReading = switchState;
  // Measure distance
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.
  
  // Convert measured value to value between 0-16, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 15);
  lcd.clear();
  lcd.print("E      |      F");
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
     lcd.print((char)0);
     scaledValue--;
  }
      
   delay(3000);  // Wait 3 seconds before measuring again. We're in no hurry! :P
}

you only need to add in a little function to display that you changed modes…

You only change lowwater, so set it to either value depending on the state of the switch. It's a single line of code in setup()

Simply connect your toggle switch between GND and pin 5 and change your code as follows

#include <LiquidCrystal.h>

#define ECHOPIN 3                            // Pin to receive echo pulse
#define TRIGPIN 4                            // Pin to send trigger pulse
#define toggleSwitch 5                       // Pin for toggle Switch

#define STATUSPIN 13                        // Use for troubleshooting

int highWater = 30;        // These values allow to calculate % of full
int lowWater = 95;        // SRF04 hangs above water (lower distance = more water)

byte symbol[8] = {         // Custom character for LCD display
  B00000, 
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B00000,
};

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);    // Assign pins

// Utility function for flashing STATUSPIN
void flashLed(int pin, int times, int wait) {
    for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);

    if (i + 1 < times) {
      delay(wait);
    }
  }
}


void setup() {
  
  lcd.begin(16,2);
  lcd.print("   AQUA LEVEL ");
  lcd.createChar(0, symbol);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
  pinMode(toggleSwitch,INPUT_PULLUP);
  delay(3000);       // Show application name for 3 seconds.

}

void loop() {
  // Measure distance
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.
  
  //ADDED FOR TOGGLE SWITCH SUPPORT
  char tankType=digitalRead(toggleSwitch)? '2':'1';
  lowWater = (tankType=='2')? 200:95;        // SRF04 hangs above water (lower distance = more water)
  
  // Convert measured value to value between 0-16, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 15);
  lcd.clear();
//following line was also changed so that the right hand char will indicate your switch setting
  lcd.print("E      |      ");
  lcd.print(tankType);
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
     lcd.print((char)0);
     scaledValue--;
  }
      
   delay(3000);  // Wait 3 seconds before measuring again. We're in no hurry! :P
}

@BulldogLowell oh what a great job :astonished:

void loop() 
{
  int switchState = digitalRead(togglePin);
  if (switchState != lastSwitchReading)
  {
    if (switchState = LOW)
    {
      highWater = 30;
      lowWater = 95;
    }
    else
    {
      highWater = 30;
      lowWater = 200;
    }
  }
  lastSwitchReading = switchState;

"else" i like your way really let me upload it and test it

@KenF

//ADDED FOR TOGGLE SWITCH SUPPORT
  char tankType=digitalRead(toggleSwitch)? '2':'1';
  lowWater = (tankType='2')? 200:95;        // SRF04 hangs above water (lower distance = more water)

this code look so weird for me :fearful: i will try it too thanks BulldogLowell & KenF i will try both of them, and i will back :cold_sweat:

you can also try something like this to display when you have switched from one to two…

#include <LiquidCrystal.h>

#define ECHOPIN 3                            // Pin to receive echo pulse
#define TRIGPIN 4                            // Pin to send trigger pulse
#define STATUSPIN 13                        // Use for troubleshooting

int highWater = 30;        // These values allow to calculate % of full
int lowWater = 95;        // SRF04 hangs above water (lower distance = more water)

const int togglePin = 5;
int lastSwitchReading;

byte symbol[8] = {         // Custom character for LCD display
  B00000, 
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B00000,
};

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);    // Assign pins

// Utility function for flashing STATUSPIN
void flashLed(int pin, int times, int wait) {
    for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);

    if (i + 1 < times) {
      delay(wait);
    }
  }
}


void setup() 
{
  pinMode(togglePin, INPUT_PULLUP);
  lcd.begin(16,2);
  lcd.print("   AQUA LEVEL ");
  lcd.createChar(0, symbol);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);

  delay(3000);       // Show application name for 3 seconds.

}

void loop() 
{
  int switchState = digitalRead(togglePin);
  if (switchState != lastSwitchReading)
  {
    if (switchState == LOW)
    {
      highWater = 30;
      lowWater = 95;
    }
    else
    {
      highWater = 30;
      lowWater = 200;
    }
    changeScale();
  }
  lastSwitchReading = switchState;
  // Measure distance
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.
  
  // Convert measured value to value between 0-16, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 15);
  lcd.clear();
  lcd.print("E      |      F");
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
     lcd.print((char)0);
     scaledValue--;
  }
      
   delay(3000);  // Wait 3 seconds before measuring again. We're in no hurry! :P
}

void changeScale()
{
  lcd.clear();
  lcd.print("New Settings");
  lcd.setCursor(0,1);
  lcd.print("Low Water=");
  lcd.print(lowWater);
  delay(3000);
  lcd.clear();
}
if (switchState = LOW)

Oops

AWOL: if (switchState = LOW)

Oops

Drat!... that's what I get for not testing

@narzan... take heed... should be:

if (switchState == LOW)

I corrected above...

 char tankType=digitalRead(toggleSwitch)? '2':'1';
  lowWater = (tankType='2')? 200:95;

this code look so weird for me smiley-eek-blue i will try it too

The first line effectively means

If (digitalRead(toggleSwitch)==HIGH) tankType='2'; else tankType='1'; (tankType is used further down to update the display)

The second line effectively means

if(tankType=='2') lowWater=200; else lowWater=95;

It's a handy form to get the hang of. Basically whenever you want to select from just two possible values, you can use the form (condition)? valueIfTrue:valueIfFalse

So you could even use things like Serial.println((sizeQuoted>6)? "In your dreams":"Don't worry you're still young");

KenF thanks for all this information ;) i can't wait to back from my work and try both of them XD

nop :fearful: both codes not work always measure of 2M for 1M not work the toggle switch not change anything =( !!

check your switch wiring.

one post of the switch to pin5 the middle to ground, right?

yup and nothing change and the display always show E | 2

OK, thanks for letting us know. Tell us how it goes.

Or, post your code and your schematic.