The code below shows my work to date. As a newbie I have worked hard on this. I have a Radio Control receiver attached to the arduino and it works 7 relays on demand. All well and good. Now I want to put in a routine, and the only way to trigger this from the RC is to make a double click response.
I have added the “ClickButton.h" and the sample code which is all I want to so far. I have attached the Led to measure success or not.
https://code.google.com/p/clickbutton/wiki/Usage
The major difference here is that I am not using a click button, but an algorithm based on ‘double clicking’ the transmitter. I am not getting any joy from this at all. I am sure this is where the issue lies.. Is there an obvious problem?
#include <PinChangeInt.h>
#include "ClickButton.h"
#include <Servo.h>
#define BALLAST_IN_PIN 2
#define KNOB_IN_PIN 3
#define AUX_IN_PIN 4
#define HYDRO_IN_PIN 5
#define HYDRO_OUT_PIN 13
int PISTON_DIVE_PIN = 6;
int PISTON_SURFACE_PIN = 7;
int PUMP_DIVE_PIN = 8;
int PUMP_SURFACE_PIN = 9;
int TRIM_TO_BOW_PIN = 10;
int TRIM_TO_STERN_PIN = 11;
int WATER_GUN_PIN = 12;
int buttonPin1;
// the LED
const int ledPin = A2;
int ledState = 0;
// the Button
//int buttonPin1;
ClickButton button1(buttonPin1, LOW, CLICKBTN_PULLUP);
// Arbitrary LED function
int LEDfunction = 0;
// individual timing gaps for the following functions.
//first switches
const unsigned long PISTON_DIVE_PINinterval = 1000;
const unsigned long PISTON_SURFACE_PINinterval = 1000;
const unsigned long PUMP_DIVE_PINinterval = 1000;
const unsigned long PUMP_SURFACE_PINinterval = 1000;
const unsigned long TRIM_TO_BOW_PINinterval = 1000;
const unsigned long TRIM_TO_STERN_PINinterval = 1000;
const unsigned long WATER_GUN_PINinterval = 1000;
unsigned long PISTON_DIVE_PINtimer;
unsigned long PISTON_SURFACE_PINtimer;
unsigned long PUMP_DIVE_PINtimer;
unsigned long PUMP_SURFACE_PINtimer;
unsigned long TRIM_TO_BOW_PINtimer;
unsigned long TRIM_TO_STERN_PINtimer;
unsigned long WATER_GUN_PINtimer;
Servo servoBallast;
Servo servoKnob;
Servo servoAux;
Servo servoHydro;
// These bit flags are set in bUpdateFlagsShared to indicate which
// channels have new signals
#define BALLAST_FLAG 1
#define KNOB_FLAG 2
#define AUX_FLAG 4
#define HYDRO_FLAG 5
//double click routine
#define diveclick1
#define diveclick2
#define diveroutine
// holds the update flags defined above
volatile uint8_t bUpdateFlagsShared;
volatile uint16_t unBallastInShared;
volatile uint16_t unKnobInShared;
volatile uint16_t unAuxInShared;
volatile uint16_t unHydroInShared;
uint32_t ulBallastStart;
uint32_t ulKnobStart;
uint32_t ulAuxStart;
uint32_t ulHydroStart;
void setup()
{
Serial.begin(9600);
servoHydro.attach(HYDRO_OUT_PIN);
{
button1.debounceTime = 20; // Debounce timer in ms
button1.multiclickTime = 250; // Time limit for multi clicks
button1.longClickTime = 1000; // time until "held-down clicks" register
}
pinMode(PISTON_DIVE_PIN, OUTPUT);
pinMode(PISTON_SURFACE_PIN, OUTPUT);
pinMode(PUMP_DIVE_PIN, OUTPUT);
pinMode(PUMP_SURFACE_PIN, OUTPUT);
pinMode(TRIM_TO_BOW_PIN, OUTPUT);
pinMode(TRIM_TO_STERN_PIN, OUTPUT);
pinMode(WATER_GUN_PIN, OUTPUT);
pinMode(ledPin, OUTPUT);
// timer for functions
PISTON_DIVE_PINtimer = millis ();
PISTON_SURFACE_PINtimer = millis ();
PUMP_DIVE_PINtimer = millis ();
PUMP_SURFACE_PINtimer = millis ();
TRIM_TO_BOW_PINtimer = millis ();
TRIM_TO_STERN_PINtimer = millis ();
WATER_GUN_PINtimer = millis ();
(PISTON_DIVE_PIN, HIGH);
(PISTON_SURFACE_PIN, HIGH);
(PUMP_DIVE_PIN, HIGH);
(PUMP_SURFACE_PIN, HIGH);
(TRIM_TO_BOW_PIN, HIGH);
(TRIM_TO_STERN_PIN, HIGH);
(WATER_GUN_PIN, HIGH);
(buttonPin1, HIGH);
PCintPort::attachInterrupt(BALLAST_IN_PIN, calcBallast,CHANGE);
PCintPort::attachInterrupt(KNOB_IN_PIN, calcKnob,CHANGE);
PCintPort::attachInterrupt(AUX_IN_PIN, calcAux,CHANGE);
PCintPort::attachInterrupt(HYDRO_IN_PIN, calcHydro,CHANGE);
}
void loop()
{
static uint16_t unBallastIn;
static uint16_t unKnobIn;
static uint16_t unAuxIn;
static uint16_t unHydroIn;
// local copy of update flags
static uint8_t bUpdateFlags;
//Serial.println(unBallastIn
//Serial.println(unKnobIn);
//Serial.println(unAuxIn);
//Serial.println(unHydroIn);
Serial.println(buttonPin1);
//click parameters which need work
if (unBallastIn > 1800)
{
digitalWrite(buttonPin1, LOW);
}
else if (unBallastIn < 1800)
{
digitalWrite(buttonPin1, HIGH);
}
{
// Update button state
button1.Update();
// Save click codes in LEDfunction, as click codes are reset at next Update()
if (button1.clicks != 0) LEDfunction = button1.clicks;
// Simply toggle LED on single clicks
// (Cant use LEDfunction like the others here,
// as it would toggle on and off all the time)
if(button1.clicks == 1) ledState = !ledState;
// blink faster if double clicked
if(LEDfunction == 2) ledState = (millis()/500)%2;
// blink even faster if triple clicked
if(LEDfunction == 3) ledState = (millis()/200)%2;
// slow blink (must hold down button. 1 second long blinks)
if(LEDfunction == -1) ledState = (millis()/1000)%2;
// slower blink (must hold down button. 2 second loong blinks)
if(LEDfunction == -2) ledState = (millis()/2000)%2;
// even slower blink (must hold down button. 3 second looong blinks)
if(LEDfunction == -3) ledState = (millis()/3000)%2;
// update the LED
digitalWrite(ledPin,ledState);
}
//PISTON PARAMETERS for realy action
if (unBallastIn > 1800)
{
if ( (millis () - PISTON_DIVE_PINtimer) >= PISTON_DIVE_PINinterval)
digitalWrite(PISTON_DIVE_PIN, LOW);
}
else if (unBallastIn < 1800)
{
digitalWrite(PISTON_DIVE_PIN, HIGH);
PISTON_DIVE_PINtimer = millis ();
}
if (unBallastIn < 1300)
{
if ( (millis () - PISTON_SURFACE_PINtimer) >= PISTON_SURFACE_PINinterval)
digitalWrite(PISTON_SURFACE_PIN, LOW);
}
else if (unBallastIn > 1300)
{
digitalWrite(PISTON_SURFACE_PIN, HIGH);
PISTON_SURFACE_PINtimer = millis ();
}
//etc, cut the rest out