й день (я не думал что форум так отреагирует на ентер )
Имеется задача: физически посчитать людей входящих через дверь.
Использую Arduino UNO + HC-SR04 + Lcd keypad shield от DFrobot
Скетч:
#include "DHT.h"
#include <LiquidCrystal.h>
#define echoPin 2 // Echo Pin
#define trigPin 3 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 500; // Maximum range needed
int minimumRange = 50; // Minimum range needed
long duration, distance; // Duration used to calculate distance
int incrementState = 0; //variable that will read the increment button (either HIGH or LOW)
int decrementState = 0; //variable that will read the decrement button (either HIGH or LOW)
int counter = 0; //variable that will store the count
int lastIncrementState = 0;
int lastDecrementState = 0;
int currentState = 0;
int previousState = 0;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
Serial.begin(9600);
lcd.begin(5, 1);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
lcd.print(counter); //print it on serial monitor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
if (distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
lcd.setCursor(0,0);
Serial.println("-1");
digitalWrite(LEDPin, HIGH);
currentState = 1;
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
currentState = 0;
}
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
}
}
previousState = currentState;
//Delay 50ms before next reading.
delay(500);
}
Вобщем при работе происходит следующее:
на экран дублируются числа до самого конца. куда прокопать товарищи?
попробуй перед выводом на экран везде поставить lcd.setCursor(0,0);
это перед строками 55 и 62. пологаю что при каждом выводе на экран курсор просто сдвигаеся.
и убери ln, это переход на следующую строку.
и поставь пару пробелов (Serial.print(" ");)после вывода на экран, если cчет будут идти в обратном порядке, это уберет лишние цифры, к примеру при переходе, с 10 на 9, со 100 на 99 итд.
кстати, почему у тебя "Serial.print", а не "lcd.print"?
й день (я не думал что форум так отреагирует на ентер )
Имеется задача: физически посчитать людей входящих через дверь.
Использую Arduino UNO + HC-SR04 + Lcd keypad shield от DFrobot
Скетч:
#include "DHT.h" #include <LiquidCrystal.h> #define echoPin 2 // Echo Pin #define trigPin 3 // Trigger Pin #define LEDPin 13 // Onboard LED int maximumRange = 500; // Maximum range needed int minimumRange = 50; // Minimum range needed long duration, distance; // Duration used to calculate distance int incrementState = 0; //variable that will read the increment button (either HIGH or LOW) int decrementState = 0; //variable that will read the decrement button (either HIGH or LOW) int counter = 0; //variable that will store the count int lastIncrementState = 0; int lastDecrementState = 0; int currentState = 0; int previousState = 0; LiquidCrystal lcd(8, 9, 4, 5, 6, 7); void setup() { Serial.begin(9600); lcd.begin(5, 1); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDPin, OUTPUT); // Use LED indicator (if required) } void loop() { /* The following trigPin/echoPin cycle is used to determine the distance of the nearest object by bouncing soundwaves off of it. */ lcd.print(counter); //print it on serial monitor digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); //Calculate the distance (in cm) based on the speed of sound. distance = duration/58.2; if (distance <= minimumRange){ /* Send a negative number to computer and Turn LED ON to indicate "out of range" */ lcd.setCursor(0,0); Serial.println("-1"); digitalWrite(LEDPin, HIGH); currentState = 1; } else { /* Send the distance to the computer using Serial protocol, and turn LED OFF to indicate successful reading. */ Serial.println(distance); digitalWrite(LEDPin, LOW); currentState = 0; } if(currentState != previousState){ if(currentState == 1){ counter = counter + 1; Serial.println(counter); } } previousState = currentState; //Delay 50ms before next reading. delay(500); }Вобщем при работе происходит следующее:
на экран дублируются числа до самого конца. куда прокопать товарищи?
попробуй перед выводом на экран везде поставить lcd.setCursor(0,0);
это перед строками 55 и 62. пологаю что при каждом выводе на экран курсор просто сдвигаеся.
и убери ln, это переход на следующую строку.
и поставь пару пробелов (
Serial.print(" ");)после вывода на экран, если cчет будут идти в обратном порядке, это уберет лишние цифры, к примеру при переходе, с 10 на 9, со 100 на 99 итд.кстати, почему у тебя "
Serial.print",а не"lcd.print"?спасибо за совет. дома попробую.
по поводу сериал, сначала выводил данные на порт для мониторинга, потом подключил лсд. видать глаз замылился.