Hello,
Some updates ragrding this project
The battery powered sender unit is still working with one charge and a lot of key presses. So for the moment I'm very happy with it. The problem now I have to deal with is to debounce the buttons. I almost don't have experience with interrupts especially pin cange interrupts. I'll be very happy if some one can help me and give some directionts what to do next.
this is the code I'm using for the moment for the sender unit:
// ***************************************************************************************
// Sample RFM69 sketch for Moteino to illustrate sending and receiving, button interrupts
// ***************************************************************************************
// When you press the button on the SENDER Moteino, it will send a short message to the
// RECEIVER Moteino and wait for an ACK (acknowledgement that message was received) from
// the RECEIVER Moteino. If the ACK was received, the SENDER will blink the onboard LED
// a few times. The RECEIVER listens to a specific token, and it alternates the onboard LED
// state from HIGH to LOW or vice versa whenever this token is received.
// ***************************************************************************************
// Hardware setup:
// ***************************************************************************************
// On the sender, hook up a momentary tactile button to D3 like this:
// __-__
// __| |___
// GND ----> BTN ----> D3 (D11 on MoteinoMEGA)
// Load this sketch on the RECEIVER with NODEID=RECEIVER (adjust in config section below)
// Load this sketch on the SENDER with NODEID=SENDER (adjust in config section below)
// RFM69 library and code by Felix Rusu - felix@lowpowerlab.com
// Get libraries at: https://github.com/LowPowerLab/
// Make sure you adjust the settings in the configuration section below !!!
// **********************************************************************************
// Copyright Felix Rusu, LowPowerLab.com
// Library and code by Felix Rusu - felix@lowpowerlab.com
// **********************************************************************************
// License
// **********************************************************************************
// This program is free software; you can redistribute it
// and/or modify it under the terms of the GNU General
// Public License as published by the Free Software
// Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General
// Public License along with this program.
// If not, see <http://www.gnu.org/licenses/>.
//
// Licence can be viewed at
// http://www.gnu.org/licenses/gpl-3.0.txt
//
// Please maintain this license information along with authorship
// and copyright notices in any redistribution of this code
// **********************************************************************************
#include <RFM69.h> //get it here: https://www.github.com/lowpowerlab/rfm69
#include <SPI.h>
#include <LowPower.h> //get library from: https://github.com/lowpowerlab/lowpower
#include <SPIFlash.h>
//*********************************************************************************************
// *********** IMPORTANT SETTINGS - YOU MUST CHANGE/ONFIGURE TO FIT YOUR HARDWARE *************
//*********************************************************************************************
#define NETWORKID 100 //the same on all nodes that talk to each other
#define RECEIVER 1 //unique ID of the gateway/receiver
#define SENDER 2
#define NODEID SENDER //change to "SENDER" if this is the sender node (the one with the button)
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
//#define FREQUENCY RF69_433MHZ
#define FREQUENCY RF69_868MHZ
//#define FREQUENCY RF69_915MHZ
#define ENCRYPTKEY "****************" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HW //uncomment only for RFM69HW! Remove/comment if you have RFM69W!
//*********************************************************************************************
#define SERIAL_BAUD 115200
#define LED 9 // Moteinos have LEDs on D9
RFM69 radio;
SPIFlash flash(8, 0x1F65);
volatile int kur;
ISR (PCINT1_vect) // what happens with pin A0 press
{
kur = 1;
} // end of PCINT1_vect
ISR (PCINT2_vect) // what happens with pin D7 press
{
kur = 2;
} // end of PCINT1_vect
void setup ()
{
digitalWrite (A0, HIGH); // enable pull-up
digitalWrite (7, HIGH); // enable pull-up
Serial.begin(SERIAL_BAUD);
radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW
radio.setHighPower(); //only for RFM69HW!
#endif
radio.encrypt(ENCRYPTKEY);
char buff[50];
sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
Serial.println(buff);
Serial.flush();
pinMode(LED, OUTPUT);
// pin change interrupt
PCMSK1 |= bit (PCINT8); // want pin A0
PCIFR |= bit (PCIF1); // clear any outstanding interrupts
PCICR |= bit (PCIE1); // enable pin change interrupts for A0 to A5
PCMSK2 |= bit (PCINT23); // want pin D7
PCIFR |= bit (PCIF2); // clear any outstanding interrupts
PCICR |= bit (PCIE2); // enable pin change interrupts for D0 to D7
} // end of setup
void loop ()
{
Serial.flush();
radio.sleep();
flash.sleep();
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
if (!digitalRead(7) || !digitalRead(A0)) { //stupid way to make the button works only when pressed, not when released
if (kur == 1) {
radio.sendWithRetry(RECEIVER, "Hi", 2);
Blink(LED, 40, 2);
} else if (kur == 2) {
radio.sendWithRetry(RECEIVER, "Mi", 2);
Blink(LED, 40, 4);
}
}
} // end of loop
void Blink(byte PIN, byte DELAY_MS, byte loops)
{
for (byte i=0; i<loops; i++)
{
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
digitalWrite(PIN,LOW);
delay(DELAY_MS);
}
}
EDIT:
For the moment I came up with a simple solution which for the moment seems to work:
before = millis();
while (!digitalRead(7) || !digitalRead(A0)) {
//Serial.println("akaciq");
after = millis();
}
//Serial.println(after - before);
if (after - before > debounceTime) {
Serial.println("ok");
ok = 1;
} else {
ok = 0;
}
if (ok) {
if (kur == 1) {
radio.sendWithRetry(RECEIVER, "Hi", 2);
Blink(LED, 40, 2);
} else if (kur == 2) {
radio.sendWithRetry(RECEIVER, "Mi", 2);
Blink(LED, 40, 4);
}
}