Go Down

Topic: add a automatic code to this sketch (Read 303 times) previous topic - next topic

roar45

Sep 10, 2017, 09:52 am Last Edit: Sep 10, 2017, 10:06 am by roar45
Hello

I wonder if someone here can help me to add a automatic code to this sketch.

Sketch is working i use a keypad but only button 5 and 7 is in use.


When i power on arduino,i want automatic hold down the button 5 for 4 seconds and then released,without i push the button manually down.

(data.relay1 must be activated for 4 seconds and then not activated,without i push the button manually down)


It is absolutely very easy to add that to the sketch but I do not know how to do it,sow i can pay 12 dollar through Paypal.  :)

send me a PM

Code: [Select]
#include <Keypad.h>                                     // Keypad
#include <EasyTransfer.h>                               // Rs-422/485
EasyTransfer Transfer;                                  // Rs-422/485
//******************************************************************************
// Keypad
//******************************************************************************
const byte ROWS = 4; // four rows
const byte COLS = 3; // three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6};     // connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

//****************************************************************************
// Setup
//****************************************************************************
boolean enable = true;
boolean enabled = true;
boolean relay_1 = false;
boolean relay_2 = false;
unsigned long lastSend = 0;
unsigned long currentTime;
int sendInterval = 75;
unsigned long previousMillis = 0;
unsigned long interval = 1500;
int ledPin = 9;
int ledState = LOW;

//****************************************************************************
// Rs-422/485 - receiver Setup
//****************************************************************************
struct rovData
{
        int relay1;
 int relay2;

}data;
//***************************************************************************
void setup()
//***************************************************************************
{
 Serial.begin(57600);
 Transfer.begin(details(data), &Serial);
 pinMode(ledPin, OUTPUT);
 keypad.addEventListener(keypadEvent); // Keypad
}
//**************************************************************************
void loop()
//**************************************************************************
{
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval)
    {
      previousMillis = currentMillis;
      if(ledState == LOW)
      {
         ledState = HIGH;
      }
      else
      {
        ledState = LOW;
      }
       digitalWrite(ledPin, ledState);
      }
      
 char key = keypad.getKey();      
}
//*************************************************************************
inline void keypadEvent(KeypadEvent key){
//*************************************************************************
 switch (keypad.getState()){
 case PRESSED:
 switch (key){

      
                               case '5': data.relay1   = 1; break;
                               case '7': data.relay2   = 1; break;
                          
                           }
      break;
 case RELEASED:
 switch (key){
          
                               case '5': data.relay1   = 0; break;
                               case '7': data.relay2   = 0; break;
                        
                               }
 break;
 case HOLD:

 break;
        
 }    
}
//*********************************************************************************
inline void readkey()
//*********************************************************************************
{
    currentTime = millis();
    if(currentTime - lastSend > sendInterval)
    {
      lastSend = currentTime;
      if(enable == true)
      {
        Transfer.sendData();
      }
    }
}

J-M-L

#1
Sep 10, 2017, 06:27 pm Last Edit: Sep 13, 2017, 12:51 pm by J-M-L
looking at the code I can see what happens when you press a key, you just change the value of the data structure and there is no other action.

The only place where this is being sent out through the EasyTransfer library is in your inline void readkey() function but I'm unclear what's calling this function... did you share all the code? are you using a special library for Keypad? could you clarify?

Assuming there is something magic calling readkey() and thus once in a while (every 75ms if that were to be called often enough) the data is sent,  then if what you want to do is simulating what could happen if 5 were to be pressed and released 5 seconds later, you could add this at the end of the setup()

Code: [Select]
// prepare the data to be sent
data.relay1   = true;
data.relay2   = false

//send it
Transfer.sendData();

// wait for 4 seconds
delay(4000);

// prepare the new data to be sent
data.relay1   = false;

//send it
Transfer.sendData();
// wait a bit if needed
delay(100);



if that works I suggest you give the money to the help out the victims of Hurricane Irma or Harvey or to an Organization that Educate kids in needs.

Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

roar45

Hello J-M-L

is not me who has made the sketch,but this sketch is transmitter to other sketch.

I add this to the sketch as you said and is working,thank you  :)

I remove the data.relay2  = false in you code.

I will give 12 dollar to victims of Hurricane Irma or Harvey  :)

Code: [Select]
// prepare the data to be sent
data.relay1   = true;

//send it
Transfer.sendData();

// wait for 5 seconds
delay(5000);

// prepare the new data to be sent
data.relay1   = false;

//send it
Transfer.sendData();
// wait a bit if needed
delay(100);

J-M-L

#3
Sep 11, 2017, 12:09 am Last Edit: Sep 11, 2017, 12:11 am by J-M-L
I add this to the sketch as you said and is working,thank you  :)
...
I will give 12 dollar to victims of Hurricane Irma or Harvey  :)
Two very good news :)

Quote
I remove the data.relay2  = false in your code.
Yes this was un-necessary (as in the setup it's already at that value). that was more for sake of completeness.
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

Go Up