2

raindrop

I am working on a protection circuit with 2 raindrop sensors which would be hooked onto the arduinos 2 interrupt pins and as soon as they detect water they shut some relays off, turn a red led on and exit() the main program.

These sensors I have order are working fine as regular input pins with the following code:

// lowest and highest sensor readings:
const int sensorMin = 0;     // sensor minimum
const int sensorMax = 1024;  // sensor maximum

void setup() {
  // initialize serial communication @ 9600 baud:
  Serial.begin(9600);  
}
void loop() {
  // read the sensor on analog A0:
    int sensorReading = analogRead(A0);
  // map the sensor range (four options):
  // ex: 'long int map(long int, long int, long int, long int, long int)'
    int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // range value:
  switch (range) {
 case 0:    // Sensor getting wet
    Serial.println("Flood");
    break;
 case 1:    // Sensor getting wet
    Serial.println("Rain Warning");
    break;
 case 2:    // Sensor dry - To shut this up delete the " Serial.println("Not Raining"); " below.
    Serial.println("Not Raining");
    break;
  }
  delay(1);  // delay between reads
}

I will use the digital output not the analog but this doesn't change much. For the digital the pin is up at 5V at start and will be put to LOW once water is detected by the sensor.

I using the mega right now for breadbording but micro will be the final board.

Both the mega+micro should have 2,3 as interrupt pins:

Mega, Mega2560, MegaADK 2, 3, 18, 19, 20, 21
Micro, Leonardo, other 32u4-based   0, 1, 2, 3, 7

I have modified the following code and tried all conditions:

LOW to trigger the interrupt whenever the pin is low,
CHANGE to trigger the interrupt whenever the pin changes value
RISING to trigger when the pin goes from low to high,
FALLING for when the pin goes from high to low. 

It doesn't respond to any (the same time as I said the pin works fine as regular input).

Here in the setup I know that I am not using

attachInterrupt(digitalPinToInterrupt(pin), blink, CHANGE);

but

attachInterrupt(interruptPin, myISR, FALLING); 

that is because the IDE doesn't recognize this function. Is there any problem with the code? If I understand interrupts correctly I don't even need to check for that if ( ISRRan ) to break off the while loop, once the interrupt comes in the ardu SHOULD be running my interrupt function and immediately run the protection code then abort.

byte interruptPin = 0;  // your interrupt pin
volatile boolean ISRRan = 0;  // this variable is set in your ISR to trigger a break in the while loop
byte C = 0;  // this is just the counter var for the while loop

void setup() {
 attachInterrupt(interruptPin, myISR, FALLING); 
}

void myISR() {
 ISRRan = 1;
 // do other things in the ISR
}

void loop() {
 while ( C < 10 ) {
   if ( ISRRan ) {
     break;
   }
   //doing something inside the while loop
 C++;
 }
 // do other things outside of the while loop
 ISRRan = 0;  // Reset for whatever reason
}

migrated from electronics.stackexchange.com Apr 11 '16 at 22:45

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • Where your period definition ? Which hardware allow rapid communication ? C++ set a period, mean clock_time x count. You haven't any data when scanning counter on count. Use a lot delay for prepare device + communication. 9600 rate need minimal 0.3 second delay(mean 300 milisecond)(of course can use less delay value but lost a lot data !) – dsgdfg Apr 11 '16 at 14:17
  • You could rework without interrupts. Rain doesn't exactly start and stop within a millisecond. Just test the sensor in your main loop. – Nick Gammon Apr 11 '16 at 23:45
  • arduino.cc/en/Reference/AttachInterrupt has some hints on mapping pins to interrupt numbers, if digitalPinToInterrupt(pin) doesn't work. – Dave X Apr 13 '16 at 17:39
2

I have solved this.

The problem was with the pinout indeed.

attachInterrupt(0, myISR, FALLING);

will use the DIGITAL PIN 2

I was using attachInterrupt 2, that's all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy