LM393 Code Works on Mega but never on Giga R1

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 ; } 

Hi, @kaanrevan
I am not familiar with the Giga, but have you setup your hardware for the 3V3 i/o logic of the Giga?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

2 Likes

Hi @kaanrevan. The Mbed OS crash is caused by calling Serial.println while you have interrupts disabled. Here is a minimal demonstration sketch:

void setup() {
  Serial.begin(9600);
}
void loop() {
  noInterrupts();
  Serial.println("Hello, world!");
  interrupts();
  delay(1000);
}

(the crash only occurs when the port is open in Serial Monitor, etc.)

I guess you disabled interrupts for the sake of atomicity during your RPM calculation? If so, you can fix the problem by simply move the Serial.println call to after the interrupts call:

      interrupts(); 
      Serial.println(velocity,2);
1 Like

lm393
Circuit as shown...

I tried this but the values are shown as 0.

Hi,

Do you have a DMM? (Digital MultiMeter)

Does the output of the sensor change when you operate it?

Tip; Press [ CTRL ] [ T ] to auto format your code, to make it easier to read.

Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Thanks. I tried again and solved problem.

You are welcome. I'm glad it is working now.

Regards,
Per

1 Like