Please help in my project on blind stick

Hello friend I’m using arduino nano i want add a 3 mode switch in my blind stick to control the distance in mode 1 distance is 30 mode to distance is 50 in mode 3 distance is 100
How to apply this type of function in my code or hardware.
Code is below:

const int trigPin = 3;
const int echoPin = 2;
const int buzzer = 5;
const int motorPin = 6;

long duration;
int distance;
int safetyDistance;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
}

void loop() {

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 30){
digitalWrite(buzzer, HIGH);
digitalWrite(motorPin, HIGH);
}
else{
digitalWrite(buzzer, LOW);
digitalWrite(motorPin, LOW);
}

Serial.print("Distance: ");
Serial.println(distance);
}

Please post your code using code tags. It is all explained in the sticky post at the top of the forum. It helps people help you. After reading that post, you can edit your post and fix it.

The easiest method would be to wire up a push button. Every time you press it, you change mode and when it gets to high, it wraps back around to the beginning. (1 -> 2 -> 3 -> 1 -> 2...)

Look at the state change detection example in the IDE (File->examples->02.digital->StateChangeDetection) to learn how to do it properly.

1 Like

    if (safetyDistance <= 30)
        ...
    else if (safetyDistance <= 50)
        ...
    else
        ...

post code using </>

I want to use distance according to switch mode

Not sure what you want help with. Are talking about a hardware switch? What type of switch?

Yes A hardware sliding switch

consider

const int trigPin = 3;
const int echoPin = 2;
const int buzzer = 5;
const int motorPin = 6;
long duration;
int distance;
int safetyDistance;

const int pinSlideSw = 7;
int       threshold;


void setup() {
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(buzzer, OUTPUT);
    pinMode(motorPin, OUTPUT);
    Serial.begin(9600);
}

void loop() {
    if (digitalRead (pinSlideSw))
        threshold = 100;
    else
        threshold = 30;

    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);
    distance= duration*0.034/2;
    safetyDistance = distance;

    if (safetyDistance <= threshold){
        digitalWrite(buzzer, HIGH);
        digitalWrite(motorPin, HIGH);
    }
    else{
        digitalWrite(buzzer, LOW);
        digitalWrite(motorPin, LOW);
    }
    Serial.print("Distance: ");
    Serial.println(distance);
}

1 Like

It not required to write pinSlideSw in void setup in pinmode?

i/o is INPUT by default

How to use if 2 pin is use i try which is below, is this is right?
I want a condition if 7or 8 both are high then threshold 100

const int pinSlideSw = 7
const int pinSlideSw1 = 8

void loop() {
if (digitalRead (pinSlideSw))
(digitalRead (pinSlideSw1))
threshold = 100;

i think you want to add a 2nd switch and set thresshold if both slides switch are set

    if (digitalRead (pinSlideSw) && digitalRead (pinSlideSw1))
        threshold = 100;
    else
        threshold = 30;
1 Like

thanks sir you helped me a lot.
@gcjr

@gcjr dear sir I want your help

Now I’m using single slide switch which have 1 common and 4 states.

I use pull up circuit with this switch but it not working properly when I touch pin 7 to 9 any pin it works with my body resistance and when I connect it to switch only one threshold is working.

Please help me how to resolve this problem.
My code which is i use

const int trigPin = 3;
const int echoPin = 2;
const int buzzer = 5;
const int motorPin = 6;
long duration;
int distance;
int safetyDistance;

const int pinSlideSw = 7;
const int pinSlideSw1 = 8;
const int pinSlideSw2 = 9;
const int pinSlideSw3 = 10;
int threshold;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
if (digitalRead (pinSlideSw))
threshold = 10;
else if (digitalRead (pinSlideSw1))
threshold = 50;
else if (digitalRead (pinSlideSw2))
threshold = 110;
else if (digitalRead (pinSlideSw3))
threshold = 170;
else
threshold = 0;

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
safetyDistance = distance;

if (safetyDistance <= threshold) {
digitalWrite(buzzer, HIGH);
digitalWrite(motorPin, HIGH);
}
else {
digitalWrite(buzzer, LOW);
digitalWrite(motorPin, LOW);
}
Serial.print("Distance: ");
Serial.println(distance);
}

configure each input pin as INPUT_PULLUP

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.