Part 1 should work, unable to test it. position() always gives 0 even when i turn so speed has the same problem

This commit is contained in:
nokkvinetti
2026-09-10 15:21:13 +00:00
parent 04109013f2
commit c0073a5820
5 changed files with 34 additions and 1 deletions
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
@@ -12,6 +12,7 @@ public:
void init();
void update();
int position();
int speed();
private:
Digital_in c1;
@@ -20,6 +21,9 @@ private:
bool previousC1;
volatile int count;
int pastCount;
uint32_t pastTime;
};
#endif
@@ -1,4 +1,5 @@
#include <encoder.h>
#include <Arduino.h>
#include <avr/io.h>
#include <avr/interrupt.h>
@@ -42,6 +43,11 @@ void Encoder::init()
// Enable global interrupts
sei();
// Set initial states for speed
pastCount = count;
pastTime = micros();
}
void Encoder::update()
@@ -78,4 +84,21 @@ int Encoder::position()
SREG = oldSREG;
return currentCount;
}
int Encoder::speed()
{
int currentCount = position();
uint32_t currentTime = micros(); //gives arduino microseconds since program started running
//from spec sheet we get 1400 counts per revolution from driver
// or 700 from red beard??
int rpm = ((currentCount-pastCount)*6e7)/((currentTime-pastTime)*1400); // we turn counts to rotations by dividing by 1400 and microsecond to minutes by multiplying with 60 and 10^6
pastTime = currentTime;
pastCount = currentCount;
return rpm;
}
@@ -14,6 +14,8 @@ int main()
while (1)
{
Serial.println(encoder.position());
//Serial.println(encoder.speed());
delay(100);
}
return 0;