Lightning Sensor Unplugs Radio Antennas

I have set up a system using an AS3935 Lighting Sensor to unplug my amateur radio antenna wires if it senses less than 30 kilometers away.

When the AS3935 sets a distance value, an IF statement evaluates the distance (<= 30 km) and changes a boolean value to (TRUE). With a TRUE value, the servo motor turns 90 degrees and unplugs the antenna wires. THIS ALL WORKS.
PLEASE NOTE, I am using this boolean value for functionality that is out of scope for this question and don't want to over-complicate things.

However, I would like to add a push button to run the servo back to 90 degrees (center) to plug the antennas back in when the storm is over. I am having trouble with implementing the code for the button in conjunction with the below code to "open" the servo (unplug antennas). Any and all help is appreciated.

Thanks!

#include "SparkFun_AS3935.h"
#include <Servo.h>

Servo myservo;  // define servo object

// define Lightning Detector settings
#define LIGHTNING_INT 0x08
#define DISTURBER_INT 0x04
#define NOISE_INT 0x01

SparkFun_AS3935 Lightning_Detector;   // initialize Lightning Detector
const int Detection_Pin = 8;  
int Detection = 0;       

bool c = false;  // default bool as safe

void setup() {
  Serial.begin(9600);
  myservo.attach(4);  // attaches the servo on D4
  myservo.write(90);  // set servo to default power-on start position (center)
  delay(1000);        // give servo time to move


void loop() {
  if(digitalRead(Detection_Pin) == HIGH) {
   Detection = Lightning_Detector.readInterruptReg();
     if(Detection == NOISE_INT) {
       Serial.println("Noise Detected.");
       bool = false;
     }
      else if (Detection == DISTURBER_INT) {
        Serial.println("Disturber Detected.");
        bool = false;
     }
      else if(Detection == LIGHTNING_INT){
      int Distance = Lightning_Detector.distanceToStorm();
      Serial.print("Lightning Detected ");
      Serial.print(Distance);
      Serial.println("km away");
      if (Distance <= 30)
         bool = true;
     }


Serial.println(c);  // Print boolean to Serial Monitor. False = 0; True = 1
if (c == 0) {
  Serial.println("All clear.");
}
else (c != 0) {
  Serial.println("Too close!");
  myServo.write(180);  // set servo to 'open' antenna connection
  delay(1000);         // give servo time to move
 }
}

That sounds very much like like "please do the hard work of coding for me".

Show your attempt to implement this and describe what happens when it runs, compared to what you wanted to happen.

Example button code can be found in the Arduino IDE. File>Examples>02.Digital>StateChangeDetection

Please see my code in the original post for what I have written so far.

Here is the code I think I want to implement for the button. I just don't know how or where to integrate it in the above sketch:

/*  buttonState = digitalRead(btn);
  if (buttonState == HIGH) {
      for (pos = 180; pos >=90; pos -= 1) { // Reset servo from 180 degrees to 90 degrees
        myservo.write(pos);
        delay(15); }
  }
   else {} */
}

Or is it buttonState == LOW ?

The bool should be the variable you initially set up and called it "c". You have NOT given this new bool variable a name, why not?

"bool = false;" is a typo.

Both should be 'c = false;' except for the third case, where it should read 'c = true;'

Update: I have added the following code after the "All clear/Too close" section from Post 1. We have a storm incoming tonight, so I'll test and see if the sketch gets hung up somewhere or actually includes the button read in the Loop.

buttonState = digitalRead(btn);
  if (buttonState == HIGH) {
      for (pos = 180; pos >= 90; pos -= 1) { // Reset servo from 180 degrees to 90 degrees (center)
        myservo.write(pos);
        delay(50); }
  }
   else {}

Here is my breadboard diagram of what I am currently trying to get working.

I have the lightning detector running the servo 90 to 180 when it gets a TRUE, and am attempting to use the push button to run the servo from 180 to 90 when I press it.

I hope this clarifies it a little bit.

Hello skyscream111

Post the current sketch to see how we can help.

If you give the forum inaccurate information, you will get inaccurate answers. For example, the servo will not work because there is no common ground between the servo's power supply and the Nano's power supply. Yet you have told us this part does work. So the diagram you posted cannot reflect what you have in front of you.

Your pushbutton may or may not work and we can't tell from your diagram. With these 4-pin buttons, 2 pairs of pins are permanently connected. One pair is connected to the other pair when only the button is pressed. But it's not obvious which pins are paired, from looking at the diagram. So Nano pin 2 could be permanently connected to ground. The trick to avoid that problem is to connect only to a diagonal pair of pins, and leave the other pair unconnected, because it's definitely never the diagonal pairs that are permanently connected.

If you fix the button connections, you need to use INPUT_PULLUP mode on Nano pin 2, or use an external pull-up resistor on the pin, for example 10K. When the button is pressed, the Nano pin will read LOW.

1 Like

Thank you. These particular pushbutton wirings confuse me. I can’t seem to find a datasheet or schematic to show their internal connections. I will update the ‘signal’ wire to run diagonally, and play with HIGH/LOW to see which works.
And yes, I am using Pullup instead of Output in order to utilize the built-in resistor.

You're absolutely correct - I updated the wiring and the schematic based on what you said. This seems to be functioning in bench tests more like I am hoping.

I also added a 220ohm resistor on the servo signal line and it eliminated the "jitters" that sometimes came up in the motor. That makes me feel better, too.

The real smoke test will be for a thunderstorm to pass through to run the whole system process, from AS3935 interrupt signal input to servo sweep then pushbutton to reset to the initial state position.

Ok, it's clear you do not value my advice, I'm out.