Go Down

Topic: Code reconfiguration (Read 189 times) previous topic - next topic

Langoni

Dear all,

I am using the following code for AS3935 lightning sensor. The system works very well.
However, I would like to know if would be possible reconstruct the code avoiding the lines 21 and 81.
Both lines contain: void AS3935_ISR();
In another words, I would like to have only two voids, void setup(); and void loop();
Is this possible or not. If it is possible How can I just the original code?

Thanks for any help...

Cesar

Code: [Select]
#include <I2C.h>
  #include <PWFusion_AS3935_I2C.h>

  // interrupt trigger global var       
  volatile int8_t AS3935_ISR_Trig = 0;

  // defines for hardware config
  #define SI_PIN               19
  #define IRQ_PIN              18
  #define AS3935_ADD           0x03     // x03 - standard PWF SEN-39001-R01 config
  #define AS3935_CAPACITANCE   64       // <-- SET THIS VALUE TO THE NUMBER LISTED ON YOUR BOARD

  // defines for general chip settings
  #define AS3935_INDOORS       1
  #define AS3935_OUTDOORS      0
  #define AS3935_DIST_DIS      0
  #define AS3935_DIST_EN       1

  // prototypes
  void AS3935_ISR();
  PWF_AS3935_I2C  lightning0((uint8_t)IRQ_PIN, (uint8_t)SI_PIN, (uint8_t)AS3935_ADD);

  void setup()
  { 
  Serial.begin(9600);
  Serial.println("Playing With Fusion: AS3935 Lightning Sensor, SEN-39001-R01");
  Serial.println("beginning boot procedure...."); 
  // setup for the the I2C library: (enable pullups, set speed to 400kHz)
  I2c.begin();
  I2c.pullup(true);
  I2c.setSpeed(1);
  delay(2); 
  lightning0.AS3935_DefInit();   // set registers to default 
  // now update sensor cal for your application and power up chip
  lightning0.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_OUTDOORS, AS3935_DIST_EN);                 
  // enable interrupt (hook IRQ pin to Arduino Uno/Mega interrupt input: 0 -> pin 2, 1 -> pin 3 )
  attachInterrupt(0, AS3935_ISR, RISING); //Modo original
  //attachInterrupt(digitalPinToInterrupt(IRQ_PIN), AS3935_ISR, RISING); //Modo sugerido
  lightning0.AS3935_PrintAllRegs();
  AS3935_ISR_Trig = 0;           // clear trigger
  }

  void loop()
  {
  // This program only handles an AS3935 lightning sensor. It does nothing until
  // an interrupt is detected on the IRQ pin.
  while(0 == AS3935_ISR_Trig){}
  delay(5);   
  // reset interrupt flag
  AS3935_ISR_Trig = 0;   
  // now get interrupt source
  uint8_t int_src = lightning0.AS3935_GetInterruptSrc(); 
  if(0 == int_src)
  {
    Serial.println("interrupt source result not expected");
  }
  else if(1 == int_src)
  {
    uint8_t lightning_dist_km = lightning0.AS3935_GetLightningDistKm();
    Serial.print("Lightning detected! Distance to strike: ");
    Serial.print(lightning_dist_km);
    Serial.println(" kilometers");
  }
  else if(2 == int_src)
  {
    Serial.println("Disturber detected");
  }
  else if(3 == int_src)
  {
    Serial.println("Noise level too high");
  }
  lightning0.AS3935_PrintAllRegs(); // for debug... 
  }

  // this is irq handler for AS3935 interrupts, has to return void and take no arguments
  // always make code in interrupt handlers fast and short

  void AS3935_ISR()
  {
  AS3935_ISR_Trig = 1;
  }

Geek Emeritus

#1
Mar 22, 2019, 10:26 pm Last Edit: Mar 22, 2019, 10:32 pm by Geek Emeritus
that which you call a void is a function.

my computer bails and fails compiling this at include I2C.h

my own program runs fine with Wire.h

the function call at line 20 occurs before setup. this makes no sense.
All PMs will be deleted unopened due to arrogant argumentative pot stirring Malfoys.
 If you have not read "How to use this forum - please read", in particular: "7. If you are posting code or error messages, use "code" tags":  expect rude responses

groundFungus

#2
Mar 22, 2019, 10:34 pm Last Edit: Mar 22, 2019, 10:36 pm by groundFungus
Code: [Select]
 void AS3935_ISR();
The line numbers don't match when I put the code in my IDE, but I assume that is the line 21 that you speak of.  That is a function prototype.  Arduino IDE, usually, automatically generates function prototypes, but there are cases where the automatic prototype is not right so the prototype must be "manually" provided.  This may be the case.  Comment the line out and if the code compiles, the prototype may not be needed.

As to line 81, that is the function definition for the Interrupt Service Routine (ISR).  ISRs must be functions (code outside of the loop(), setup() or any other function).  So, no, you can't remove that and place it in loop().  The ISR modifies a variable that is used in loop() so the ISR must remain or the loop() code will be broken.

arduarn

Sure, you could do this without interrupts. Just poll the IRQ_PIN regularly in your loop() function with digitalRead(), and when it goes high, wait the specified 2ms, then read the interrupt cause with AS3935_GetInterruptSrc(). The read will automatically clear the interrupt.

By not stating your reasons for wanting to do this, I have the feeling that you have caused some confusion...

gfvalvo

@Langoni, what problem are you trying to solve?
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

Langoni

First of all I need to explain that I am a beginner in arduino programming. I have done many experiments and with very good results. I've even solved some of the more complex problems. However, I'm still trying to learn the fundamentals of programming that often do not make sense to me. This code works correctly but the two functions void AS3935_ISR (); they do not make much sense to me.
I could live with this, but when I try to add this code inside a more complex one code, where I have access buttons to other levels, I do not know what I do with these two functions because I don't understand in fullness.
I just do not understand them and would like to understand ...

Thanks,

Cesar

groundFungus

I already linked to a page for function prototypes.  Here is a page on interrupts and interrupt service routines.  Lots more information with a google search for "arduino interrupts".

Langoni

Thank you all for the suggestions. I'll work with them.

Cesar

Go Up