Счетчик проходов через щелевой оптрон.
- Войдите на сайт для отправки комментариев
Чт, 09/08/2018 - 14:40
задача, хочу сделать счетчик проходов через щелевой оптрон, вот что есть пока что)
int sensorPin = A0;
int sensorValue = 0;
int i =0;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(i);
sensorValue = analogRead(sensorPin)*4;
if (sensorValue >3000) {
i=1;
}
else if(sensorValue <3000) {
i=0;
}
}
Отличное начало. Будем внимательно следить за вашим проектом!
не вижу в коде счетчика :) а так неплохо для начала :)
в том то и вопрос, как его сделать? int sensorPin = A0; int sensorValue = 0; int i =0; void setup() { Serial.begin(9600); } void loop() { Serial.println(i); sensorValue = analogRead(sensorPin)*4; if (sensorValue >3000) { i=i+1; } else if(sensorValue <3000) { }считает не точно, и с шимом беда( volatile byte rpmcount; unsigned int rpm; // сохраняем обороты int a=0; volatile unsigned int dist, distantion = 0; const int buttonPin = 2; int buttonState = 0; #include <LiquidCrystal.h> // библиотека экрана LiquidCrystal lcd(12, 11, 10, 8, 7, 6); #include <Button.h> Button but1 (2, 50); // создание объекта для кнопки void setup() { pinMode(buttonPin, INPUT); Serial.begin(9600); lcd.begin(16, 2); // инициализируем дисплей pinMode (9, OUTPUT); // инициализация вывода 9 как "Выход" attachInterrupt(1, rpm_fun, FALLING);// функция прерывания только при смене значения с 1 на 0 rpmVse = 0; rpmcount = 0; rpm = 0; delay(5); but1.flagClick = false; } void loop() { rpmcount = 0; buttonState = digitalRead(buttonPin); if (but1.flagClick == true){ rpm=0 ; lcd.clear(); } int sensorValue = analogRead (A0); analogWrite (9, sensorValue*4); lcd.setCursor(0, 1); lcd.print(rpm); Serial.println(rpm); rpm = rpm+rpmcount; rpmcount = 0; } void rpm_fun() { //обновляем счетчик rpmcount++;// прибавляем единицу к полученому }Может я не понимаю тайного смысла, но зачем счетчик обнулять каждый loop() да еще и дважды? Да и сделайте rpmcount побольше размерность - int, long. Байт переполнится влёт, если быстрый rpm считаете.
const byte interruptPin = 2; // กำหนดว่าขา ของ Counter / Speed Sensor ต่ออยู่กับขา D2 volatile byte ChangeInt = false; volatile uint32_t CountInt = 0; // ประกาศตัวแปร CountInt เป็นชนิด uint เก็บข้อมูลตัวเลขจำนวนเต็ม void setup() { Serial.begin(115200); pinMode(ledPin, OUTPUT); pinMode(interruptPin, INPUT_PULLUP); // กำหนดให้ขาที่ต่อกับ ของเซ็นเซอร์เป็นขาอินพุต // ส่งสัญญาณ LOW (ลอจิก 0) ออกมา เมื่อตรวจพบวัตถุ attachInterrupt(digitalPinToInterrupt(interruptPin), counterObjects, FALLING); } void loop() { if (ChangeInt) { ChangeInt = false; Serial.println(CountInt); } } void counterObjects() { if (digitalRead(interruptPin) == LOW) { // ถ้าขาเชื่อมต่อกับ LOW CountInt++; // เพิ่มค่าในตัวแปร CountInt ขึ้น 1 จำนวน ChangeInt = true; } }извините
const byte interruptPin = 2; // กำหนดว่าขา ของ Counter / Speed Sensor ต่ออยู่กับขา D2 volatile byte ChangeInt = false; volatile uint32_t CountInt = 0; // ประกาศตัวแปร CountInt เป็นชนิด uint เก็บข้อมูลตัวเลขจำนวนเต็ม void setup() { Serial.begin(115200); pinMode(ledPin, OUTPUT); pinMode(interruptPin, INPUT_PULLUP); // กำหนดให้ขาที่ต่อกับ ของเซ็นเซอร์เป็นขาอินพุต // ส่งสัญญาณ LOW (ลอจิก 0) ออกมา เมื่อตรวจพบวัตถุ attachInterrupt(digitalPinToInterrupt(interruptPin), counterObjects, FALLING); } void loop() { if (ChangeInt) { ChangeInt = false; Serial.println(CountInt); } } void counterObjects() { if (digitalRead(interruptPin) == LOW) { // ถ้าขาเชื่อมต่อกับ LOW CountInt++; // เพิ่มค่าในตัวแปร CountInt ขึ้น 1 จำนวน ChangeInt = true; } }извините
Комментарии сильные.))))