Last active
September 18, 2022 12:50
-
-
Save martinius96/d1befbb807ff76562e634fa7c9cf00c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Autor implementácie: Martin Chlebovec (martinius96) | |
//Starting refresh rate: 1Hz | |
//Podpor tvorbu: https://paypal.me/chlebovec | |
#include <avr\wdt.h> | |
#include <FastX9CXXX.h> | |
// pinout | |
#define X9_CS 3 | |
#define X9_INC 4 | |
#define X9_UD 5 | |
#define ROBOTDYN_Pin 2 // ROBOTDYN modul pripojeny k D2 (podpora preprusenia) | |
volatile unsigned long tipCount; // pocet pulzov, inkrementovane v interrupte | |
volatile unsigned long ContactTime;// timer v interrupte - casovy debounce | |
FastX9C103 x9; //objekt x9 pre triedu FastX9C103 | |
unsigned long timer = 0; | |
unsigned long interval = 1000; | |
void setup() { | |
// zahájení komunikace po sériové lince | |
Serial.begin(9600); | |
pinMode(ROBOTDYN_Pin, INPUT); | |
attachInterrupt(digitalPinToInterrupt(ROBOTDYN_Pin), rgisr, FALLING); //prerusenie na zostupnu hranu - LOW | |
sei();// Enable Interrupts | |
x9.Setup(X9_CS, X9_UD, X9_INC); | |
wdt_enable(WDTO_8S); //watchdog | |
} | |
void loop() { | |
wdt_reset(); //feed watchdog | |
if (Serial.available()) | |
{ | |
char val = Serial.read(); | |
if (val == '+') | |
interval += 100; | |
else if (val == '-') | |
interval -= 100; | |
} | |
if ((millis() - timer) >= interval) { | |
Serial.print("Interval: "); | |
Serial.println(interval); | |
timer = millis(); | |
cli();//Disable interrupts | |
x9.JumpToStep(tipCount); | |
Serial.print("Pocet impulzov: "); | |
Serial.println(tipCount); //1 pulz = 100 ohm | |
tipCount = 0; //VYNULOVANIE PULZOV | |
sei();//Enables interrupts | |
Serial.print("Priblizna hodnota nastaveneho odporu: "); | |
Serial.print(x9.GetEstimatedResistance(), DEC); | |
Serial.println(" Ohm"); | |
} | |
} | |
void rgisr() { | |
//AK JE MEDZI PULZAMI ASPON 15ms | |
if ((millis() - ContactTime) > 15 ) { // debounce of sensor signal | |
tipCount++; //PRIPOCITAJ +1 | |
ContactTime = millis(); //uloz cas pulzu | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment