I'm trying to move codes in a project from mega to Giga. I detect the speed with LM393. The codes work correctly on mega but not on GIGA. It is compiled and loaded in GIGA, but due to a logic error, the red LEDs give a warning and it does not work. Afterwards, I cannot enter the serial screen because the connection is disconnected. I reload the empty code using the reset method twice and my connection is restored. I think the codes are not compatible with GIGA. I would be happy if you support me in this matter.
int encoder_pin = 2;
unsigned int rpm = 0;
float velocity = 0; //[Km/h]
volatile byte pulses = 0;
unsigned long timeold = 0;
unsigned int pulsesperturn = 10;
const int wheel_diameter = 598;
static volatile unsigned long debounce = 0;
void setup() {
Serial.begin(9600);
pinMode(encoder_pin, INPUT);
attachInterrupt(0, counter, RISING);
pulses = 0;
rpm = 0;
timeold = 0;
}
void loop() {
if (millis() - timeold >= 1000){
noInterrupts();
rpm = (60 * 1000 / pulsesperturn )/ (millis() - timeold)* pulses;
velocity = rpm * 3.1416 * wheel_diameter * 60 / 1000000; // [Km/h]
timeold = millis();
Serial.println(velocity,2);
pulses = 0;
interrupts();
}
}
void counter(){
if( digitalRead (encoder_pin) && (micros()-debounce > 500) && digitalRead (encoder_pin) ) {
debounce = micros();
pulses++;}
else ; }