Hey guys,
I'm a beginner and I'm working on a project where I have to transmit data from a tachometer sketch from an MKR 1300. Before dealing with the LoRa transmission, I decided to test out a hall effect tachometer sketch made by Pawel Hernik (link to his Arduino tachometer tutorial
After slightly modifying the sketch so that I can observe from the serial monitor rather than an OLED, I tested out the program on an Arduino Nano and it works perfectly. I just got my Arduino MKR 1300 yesterday, successfully uploaded the same sketch to the board, but the serial monitor shows no changes (everything stays at 0). I've also made sure that the A3144 sensor output connects to digital pin 2, and a pull-up resistor connected to the same pin.
I also have not installed any library for the MKR 1300. As far as I know, the MKRWAN library is only needed to connect to the network, so a simple tachometer sketch should work fine. Should I be using a different pin? I have also included my code below just in case.
I'd appreciate any help from you guys
Thanks in advance!
// Fidget Spinner counter and tachometer
// 27.08.2017 Pawel A. Hernik
// source code for the video:
// https://youtu.be/42qNfPOYlR8
/*
Parts:
- Hall sensor 3144
- Neodymium magnet for fidget spinner
- OLED SSD3106
- Arduino Pro Mini/Nano
3144 pinout from front:
1 - VCC
2 - GND
3 - DATA (0 when close to magnet)
*/
const int hallPin = 2; // pin 2 = int 0
volatile unsigned long cntTime=0;
volatile unsigned long cnt=0;
float spd;
float dis;
unsigned int up;
void doCount()
{
if(digitalRead(hallPin) == LOW)
{
cnt++;
cntTime = millis();
up++;
}
}
// --------------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
pinMode(hallPin,INPUT_PULLUP);
digitalWrite(hallPin,HIGH);
attachInterrupt(digitalPinToInterrupt(hallPin), doCount, FALLING); // hall pin on interrupt 0 = pin 2
digitalWrite(hallPin,HIGH);
pinMode(hallPin,INPUT_PULLUP);
cntTime = millis();
}
// --------------------------------------------------------------------------
volatile unsigned long rpm=0,maxrpm=0;
int dispRotTime=0, rotTime=0;
unsigned long measureTime=0,curTime,startTime=0;
int dispCnt=0,measureCnt=0;
const int resetTime = 2000;
const int minRotNum = 1; // 1 - calc after every rotation
void loop()
{
curTime = millis();
if(curTime-cntTime>resetTime) { // reset when less than 30RPM (dt>2s)
cnt = measureCnt = 0;
rpm = 0;
}
if(cnt==1) startTime = cntTime;
if(cnt-measureCnt>=minRotNum) {
rpm = 60000L*(cnt-measureCnt)/(cntTime-measureTime); //Serial.println(String("Cnt=")+cnt+" dcnt="+(cnt-measureCnt)+" dtime="+(cntTime-measureTime));
measureCnt = cnt;
measureTime = cntTime;
}
rotTime = (cntTime-startTime)/1000; // time in seconds
if(cnt>1 || !dispRotTime) { // keep previous time on the OLED until new rotation starts
dispRotTime = rotTime;
dispCnt = cnt;
}
//Calculate distance
dis = up*0.66*3.1415926; //number of pulses multiplied by circumference
//Calculatekm/h
spd = 0.1885*rpm*0.66;
if(rpm>maxrpm) maxrpm=rpm;
Serial.print("\tRotations: ");
Serial.print(dispCnt);
Serial.print("\tRPM: ");
Serial.print(rpm);
Serial.print("\tSpeed (km/h): ");
Serial.print(spd);
Serial.print("\tDistance (m): ");
Serial.print(dis);
Serial.print("\tTime Elapsed: ");
Serial.println(dispRotTime);
}
// --------------------------------------------------------------------------