adjusting distance one button

please Help! need your advice

using the ultrasonic sensor and arduino one
i want to adjust the ubicación of the sensor pressing one button
press one 10cm
press second time 20cm
press third time 30cm

and this value include in the formula:
distance = 10 * (duration / 2) * 0.0344 - ADJUSTMENT;

but i guess i am shewing bigger than i though

my unsuccessful code so far:

#define trigPin 10
#define echoPin 13
int buttonApin = 9;
int buttonBpin = 8;
int adjust = 1;

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(buttonApin, INPUT_PULLUP);  
 pinMode(buttonBpin, INPUT_PULLUP); 
}

void loop() {
 float duration, distance, adjust, distanceok ;
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2);

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

adjust++;

{
 if (digitalRead(buttonApin) == LOW) 

{     
   adjust = adjust + 1;

   if (adjust == 5)
   {
     adjust = 1;
   }
 }

 if (adjust == 1)
 {
   digitalWrite(adjust, 10);
 }
 else if (adjust == 2)
 {
   digitalWrite(adjust, 20);
 }
 else if (adjust == 3)
 {
   digitalWrite(adjust, 30);
 }
 else
 {
   digitalWrite(adjust, 40);
 }

 duration = pulseIn(echoPin, HIGH);
 distance = 10 * (duration / 2) * 0.0344 - adjust;
 distanceok = distance / 1000;

   Serial.print(adjust,1);
   Serial.print(" cm");

   delay(500);

   Serial.print("Distance = ");
   Serial.print(distanceok,1);
   Serial.print(" cm");
 delay(500);

 delay(500);
}
}
digitalWrite(adjust, 10);

digitalWrite takes either HIGH or LOW as the second argument depending on if you want to write the pin to Vcc or to ground. What were you hoping that the 10 or 20 would do here?

Please read the "How to use this forum" post and use code tags when posting code so it is easy to copy.

thanks for answering
i did the code format

my plan is give to ADJUST a value upon every button push adjust=10 or 20 or 30

Sometimes you are using "adjust" as a normal integer variable then later you are trying to treat it as a pin number and doing digitalWrite() to it. That doesn't make any sense.

Instead of all that messing about wouldn't it be easier just to add 10 to "adjust" each time the button is pressed?

Steve

imax:
my plan is give to ADJUST a value upon every button push adjust=10 or 20 or 30

digitalWrite writes voltage to an output pin. You want to change the value of a variable. So why are you using digitalWrite?

I think you should probably take a few hours or even a couple of days and go through some of the basic tutorials on how Arduino and the C++ language works. You're WAY off here.

slipstick:
Sometimes you are using "adjust" as a normal integer variable then later you are trying to treat it as a pin number and doing digitalWrite() to it. That doesn't make any sense.

yes, i was plain wrong.

slipstick:
Instead of all that messing about wouldn't it be easier just to add 10 to "adjust" each time the button is pressed?

Steve

how do i do that?

I really appreciate your help

Instead of all that messing about wouldn't it be easier just to add 10 to "adjust" each time the button is pressed?

imax:
yes, i was plain wrong.

how do i do that?

adjust = adjust + 10;

Or the shorthand:

adjust += 10;

If that confuses you then you really should google "C++ tutorial" and spend a few hours going over the basics. It will make things a thousand times easier once you know the basics of the language.

Well I still don't really understand what exactly you're trying to do but instead of

 if (digitalRead(buttonApin) == LOW) 

{     
   adjust = adjust + 1;

   if (adjust == 5)
   {
     adjust = 1;
   }
 }

 if (adjust == 1)
 {
   digitalWrite(adjust, 10);
 }
 else if (adjust == 2)
 {
   digitalWrite(adjust, 20);
 }
 else if (adjust == 3)
 {
   digitalWrite(adjust, 30);
 }
 else
 {
   digitalWrite(adjust, 40);
 }

can't you simply use

 if (digitalRead(buttonApin) == LOW) 

{     
   adjust = adjust + 10;

   if (adjust >= 50)
   {
     adjust = 10;
   }
 }

Steve

can't you simply use

 if (digitalRead(buttonApin) == LOW) 

{   
  adjust = adjust + 10;

if (adjust >= 50)
  {
    adjust = 10;
  }
}




Steve

Thanks Steve

Every time the user push the button (any time) the ADJUST value change, in other to make on the fly adjustments to the metering: if i set up the device on the front of the car ADJUST = 0, if i move backward the device (on the car) click click click until i get the new position (the front of the car is now 30cm in front of the device so the i now take that change on consideration

well
here an update for those newbie as me:

finally some one told me the switch case and so far seams to be the only way comprensible to me.

I learn basic 40yrs ago and html more than 20yrs ago... so i have no clue what i am doing but for me is as learning a new lenguaje so wish me luck a lot of luck i will need it. And please be patient with me