ahoi
September 12, 2014, 9:40am
1
Im trying to trigger output when distance = value i have set with buttons.
Problems are:
if i go over +10, go back a little, then over +10 value starts changing 11, 21, 31 and so on.
when i set value to 7, 70 or even 700 it triggers when distance is 7cm.
Hope you understand my bad english.
Heres the code:
b1 = digitalRead(button1);
b2 = digitalRead(button2);
if (b1 == 0)
val = (val + 1);
if (b2 == 0)
val = (val - 1);
delay(100);
if (distance == val)(digitalWrite(LED, HIGH));
if (distance != val)(digitalWrite(LED, LOW));
if (distance >= maximumRange || distance <= minimumRange){
lcd.setCursor(0, 0);
lcd.println("**********");
}
else {
lcd.setCursor(0, 0);
lcd.print(distance);
lcd.print("cm ");
lcd.print("Val=");
lcd.print(val);
lcd.setCursor(0, 1);
lcd.print("B1=");
lcd.print(b1);
lcd.print(" B2=");
lcd.print(b2);
//Delay 50ms before next reading.
delay(50);
}
}
neverming, fixed with lcd.clear()
Share your complete code.
In your code, Why you checking button condition for Low. By default button will be low. you should detect button is pressed,
When button is pressed Increment counter
if button 2 is pressed decrements counter
How you reading distance value???
without complete code i cant give feeback. Still lot of problem in your code
Robin2
September 12, 2014, 2:11pm
3
AMPS-N:
Why you checking button condition for Low.
Using the recommended pinMode(buttonPin, INPUT_PULLUP); a LOW indicates that the button is pressed.
...R
ahoi
September 12, 2014, 8:07pm
4
AMPS-N:
Share your complete code.
/*
7 echo
8 trigger
13 väljund LEDiga
9 väljund
2,3 nupud, vajutades peab ühendama maaga.
*/
#include <LiquidCrystal.h>
#define echoPin 7 // echo
#define trigPin 8 // trigger
#define LED 13 // väljund
#define out 9 //väljund
const int button1 = 2; //esimene nupp
const int button2 = 3; //teine nupp
LiquidCrystal lcd(12, 11, 5, 4, A0, A1); //lcd data pinid, järjestus: R5,E,D4,D5,D6,D7
//lcd ühendamise kohta info: http://arduino.cc/en/Tutorial/LiquidCrystal
int maximumRange = 400; //maksimaalne mõõdetav distants
int minimumRange = 0; //minimaalne mõõdetav distants
long duration, distance;
int val = 0; //määrab etteande peale reseti/voolukatkestust, vajadusel võib muuta.
void setup() {
lcd.begin(16, 2);
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(LED, OUTPUT);
pinMode(out, OUTPUT);
}
void loop() {
int b1;
int b2;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/58.2;
b1 = digitalRead(button1);
b2 = digitalRead(button2);
if (b1 == 0)
val = (val + 1); //määrab kui palju lisatakse etteande väärtusele iga vajutusega, vajadusel muuda
if (b2 == 0)
val = (val - 1); //määrab kui palju lisatakse etteande väärtusele iga vajutusega, vajadusel muuda
delay(100); // väike viide, võib kustutada
if (distance == val)(digitalWrite(LED, HIGH));
if (distance == val)(digitalWrite(out, HIGH));
if (distance != val)(digitalWrite(LED, LOW));
if (distance != val)(digitalWrite(out, LOW));
if (distance >= maximumRange || distance <= minimumRange){
lcd.setCursor(0, 0);
lcd.println("limiit");
}
else {
lcd.clear();
lcd.setCursor(0, 0); //(0,0) = lcd esimene rida
lcd.print(distance); //kirjutab lcdle distantsi väärtuse
lcd.print("cm ");
lcd.setCursor(0, 1); //(0,1) = lcd teine rida
lcd.print("Val=");
lcd.print(val); //kirjutab lcdle etteande väärtuse
delay(50); //väike viide enne järgmist lugemist
}
}
ahoi
September 13, 2014, 12:26pm
5
if (distance == val)(digitalWrite(LED, HIGH));]/code]
Is there easy way to add torelance, if distance is +3/-3 of value?
ahoi:
if (distance == val)(digitalWrite(LED, HIGH));]/code]
Is there easy way to add torelance, if distance is +3/-3 of value?
something like:
if (distance <= (val + 3) && distance >= (val - 3))
then your code :
if (distance == val)(digitalWrite(LED, HIGH));
if (distance == val)(digitalWrite(out, HIGH));
if (distance != val)(digitalWrite(LED, LOW));
if (distance != val)(digitalWrite(out, LOW));
if (distance >= maximumRange || distance <= minimumRange)
starts to look more like:
if (distance <= (val + 3) && distance >= (val - 3))
{
(digitalWrite(LED, HIGH));
(digitalWrite(out, HIGH));
}
else
{
(digitalWrite(LED, LOW));
(digitalWrite(out, LOW));
}
if (distance >= maximumRange || distance <= minimumRange)
system
September 13, 2014, 12:50pm
7
something like:
Oh, God, no.
Do you really think in terms of if something is less than 15 inches away and more than 9 inches away, do something? Most people don't. Most people think in terms of if something is greater than 9 inches away and less than 15 inches away, do something.
The code should reflect the way people think. That way, it is far easier to understand.
ahoi
September 13, 2014, 1:01pm
8
BulldogLowell:
Works like i need. thank you.
PaulS:
Oh, God, no.
gosh, thanks but you have me confused with someone else...
PaulS:
Do you really think in terms of if something is less than 15 inches away and more than 9 inches away, do something? Most people don't. Most people think in terms of if something is greater than 9 inches away and less than 15 inches away, do something.
The code should reflect the way people think. That way, it is far easier to understand.
it was just an example,
we are looking at +- 3 of the set point val
mind blowing stuff... yeah
ahoi:
Works like i need. thank you.
system
September 13, 2014, 2:23pm
10
we are looking at +- 3 of the set point val
Still, people grasp the concept of "Is the value between 9 and 15?" better than "Is the value between 15 and 9?".