Hey all!
I've been trying to make a programme for an IR tachometer with an IR sensor, and I've been having no luck. The main problem I run into is that the results from my programme is very random and I don't have a clue why. here's what I'm trying to do
Check if the sensor is on
if yes, add 1 to counter
if not, do nothing
and repeat it per second, print the counter at the end of the loop
It shouldn't be too hard, but this is my first programme so I have no clue what I'm doing. Also I'm intergating a ESC controller into the programme, so that will also be displayed.
/*
ESC to D9
Sensor to D8
Potentiometer to A0
Screen to SDA/SCL respectively
GND/VCC to breadboard
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
//Display&sensor
#define Backlight_Pin 13
//#define sensor 8
int initial=0;
//int REV=0;
//int prevtime=0;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//ESC
Servo ESC; // create servo object to control the ESC
int potValue; // value from the analog pin
//Calculation
int sensorVal;
int start;
int val = 0;
unsigned int count = 0;
void setup() {
pinMode(8,INPUT);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
ESC.attach(9, 1000, 2000);
pinMode(8,INPUT);
}
void loop() {
//Calculation
start = millis();
while (millis()-start <= 1000){
sensorVal = digitalRead(8);
if (sensorVal == 0){
count ++;
val = count;
}
}
if (millis()-start > 1000){
start = millis();
}
Serial.println(val);
//ESC
potValue = analogRead(A0); // reads the value of the potentiometer (value between 0 and 1023)
potValue = map(potValue, 0, 1023, 0, 180); // scale it to use it with the servo library (value between 0 and 180)
ESC.write(potValue); // Send the signal to the ESC
}
and these are the results that I get from serial print
0
-18721
22502
27485
-945
-27102
9386
14099
-8628
-5658
-32368
6290
-11366
27172
-7108
30951
2265
-27729
18529
-10478
-9145
23157
-7499
-6513
Any help will be appreciated. Thanks!
