1 (edited by saddys 2018-03-15 13:57:05)

Topic: Input / Output Array

Hi!

I want to know if is possible to use app input and output (RemoteXY.button or .led) in an Array.

I try but unsuccesfully. The physics button and phisics LED are OK (switch ON with one press of button and OFF with another press of button and ON/OFF the corresponding led).

Can someone help me please for the remotexy code?

thanks

///////////////////////////////////////////// 
//        RemoteXY include library         // 
///////////////////////////////////////////// 

/* RemoteXY select connection mode and include library */ 
#define REMOTEXY_MODE__ETHERNET_LIB
#include <Ethernet.h>
#include <SPI.h>

#include <RemoteXY.h>

/* RemoteXY connection settings */
#define REMOTEXY_ETHERNET_MAC "DE:AD:BE:EF:EF:ED"
#define REMOTEXY_SERVER_PORT 6377

// RemoteXY configurate   
#pragma pack(push, 1) 
uint8_t RemoteXY_CONF[] = 
  { 255,3,0,3,0,51,0,8,13,0,
  1,0,12,12,12,12,2,31,88,0,
  1,0,31,12,12,12,2,31,88,0,
  1,0,75,14,12,12,2,31,88,0,
  65,4,13,29,9,9,65,4,33,29,
  9,9,65,4,77,28,9,9 }; 
   
// this structure defines all the variables of your control interface  
struct { 

    // input variable
  uint8_t button_1; // =1 if button pressed, else =0 
  uint8_t button_2; // =1 if button pressed, else =0 
  uint8_t button_3; // =1 if button pressed, else =0 

    // output variable
  uint8_t led_1_r; // =0..255 LED Red brightness 
  uint8_t led_2_r; // =0..255 LED Red brightness 
  uint8_t led_3_r; // =0..255 LED Red brightness 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY; 

///////////////////////////////////////////// 
//           END RemoteXY include          // 
///////////////////////////////////////////// 

int buttPin[3] = {27,28,29};   // arrow for real switch button 
int relayPin[3] = {30,31,32};  // arrow for real output relay
int buttRemote[3] = {RemoteXY.button_1,RemoteXY.button_2,RemoteXY.button_3}; //IS OK TO PUT THEM IN AN ARROW?
int ledRemote[3] = {RemoteXY.led_1_r,RemoteXY.led_2_r,RemoteXY.led_3_r};     //IS OK TO PUT THEM IN AN ARROW?
int lastButtState[3];
int buttState[3];
int debounceDelay = 50;
int ledState[3];
int reading[3];
int readingRemote[3];
unsigned long lastDebounceTime[3];


void setup()  
{ 
  RemoteXY_Init ();  
  for (int i = 0; i < 3; i++)
  {
    pinMode(relayPin[i], OUTPUT);
    pinMode(buttPin[i], INPUT_PULLUP); 
  }
}  


void loop()  
{  
  RemoteXY_Handler (); 

     
for (int i = 0; i < 3; i++) // I USE ARRAY BECAUSE I NEED IN THE FUTURE 16 INPUT/RELAYS TO USE
  {
    reading[i] = digitalRead(buttPin[i]);
//    readingRemote[i] = digitalRead(buttRemote[i]);
    if (reading[i] != lastButtState[i]) {
    //  if (reading[i] != lastButtState[i] || readingRemote[i] != lastButtState[i]) { // I NEED SWITCH ON or OFF with a change of phisic button or APP button
      lastDebounceTime[i] = millis(); // for don't use DELAY functions i USE millis
      lastButtState[i] = reading[i];
        
                                        }
    if ((millis() - lastDebounceTime[i]) > debounceDelay) {
       if (buttState[i] != lastButtState[i]) {
           buttState[i] = lastButtState[i];
           if (buttState[i] == HIGH) {
                 ledState[i] = !ledState[i];
                 digitalWrite(relayPin[i], ledState[i]);
                                                
                                     }
                 RemoteXY.led_1_r = (digitalRead(30)==HIGH)?255:0; // with a defined (ledRemote[i]) ARRAY i can't monitoring the led if I put it in for loop, so i use here succesfully
                 RemoteXY.led_2_r = (digitalRead(31)==HIGH)?255:0; // with a defined (ledRemote[i]) ARRAY i can't monitoring the led if I put it in for loop, so i use here succesfully
                 RemoteXY.led_3_r = (digitalRead(32)==HIGH)?255:0; // with a defined (ledRemote[i]) ARRAY i can't monitoring the led if I put it in for loop, so i use here succesfully
                       
                                            }
                                                          }
  }
}

2 (edited by Guillaume 2018-03-15 20:38:17)

Re: Input / Output Array

Welcome wink

You need to store pointers to those variables. For example:

uint8_t * ledRemote[3] = { &RemoteXY.led_1_r, &RemoteXY.led_2_r, &RemoteXY.led_3_r };

3

Re: Input / Output Array

Guillaume wrote:

Welcome ;) You need to store pointers to those variables.

Uh, thanks, tomorrow i'll try... So, i can define in this way

int * ledRemote[3] = {&Remotexy.button_1,&RemoteXY.button_2,&RemoteXY.button_3};
int * buttRemote[3] = {&RemoteXY.led_1_r,&RemoteXY.led_2_r,&RemoteXY.led_3_r };  

And in for (int i) i can use this:

for (int i = 0; i < 3; i++) 
 {
reading[i] = digitalRead(buttPin[i]);
readingRemote[i] = digitalRead(buttRemote[i]);
if (reading[i] != lastButtState[i] || readingRemote[i] != lastButtState[i]) { 

Can i use logical OR in this case?

Ty very much Guillaume!

4 (edited by Guillaume 2018-03-15 20:47:45)

Re: Input / Output Array

Use

digitalRead( *buttRemote[i] );

Because you want to get the value at the pointed address, not the address itself.

And I don't see why you couldn't use logical or smile

I modified my previous post to change the type of the pointer to uint8_t (because those led_r variables are uint8_t so the pointers should be of the same type).

5 (edited by saddys 2018-03-15 20:50:18)

Re: Input / Output Array

You are my favorite genius

Tomorrow I tell you if all ok smile

Thank you so much, very kind

6 (edited by Guillaume 2018-03-15 22:33:00)

Re: Input / Output Array

NP smile

By the way, if (and only if) your struct really look like in your first post, then you don't need to do all that, you can just replace your struct by:

struct { 

    // input variable
  uint8_t buttons[3]; // =1 if button pressed, else =0 

    // output variable
  uint8_t leds[3]; // =0..255 LED Red brightness 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY;

Also something that you might find useful, it's outdated but still relevant : http://forum.remotexy.com/viewtopic.php?id=120

7

Re: Input / Output Array

Thanks!

Tomorrow I will play with your code smile

My struct/code (now) is for understand the mechanism, i'm new in this world!

I wan't to implement at the end:

15 button+15relay for light room and 1 button for "ALL LIGHT OFF"
2 or 3 RGB control for strip rgb led
5 dht sensor

And finally the impossibile thing: 6 window's motor

tongue

For now I want learn and understand simply code... For me, now, the sketch of first post with array code is my current goal!

I started this Monday to go into this world, and my first sketch was a code repeated 20 times wink Today I studied (with forum examples) and tried to use arrays with success and the code has been greatly reduced! I'm very happy!

Slowly I will apply myself to insert the other things written above ... In the limit I will disturb you in exchange for a pizza and a beer (or more beers and pizzas) if you want/can wink  ;p

8

Re: Input / Output Array

I think you don't want to do this:

readingRemote[i] = digitalRead( *buttRemote[i] );

You probaby want to do this instead:

readingRemote[i] = *buttRemote[i];

In fact you may not need this line at all.

Good luck smile

9 (edited by saddys 2018-03-16 14:54:51)

Re: Input / Output Array

Guillaume wrote:

I think you don't want to do this:
You probaby want to do this instead:

readingRemote[i] = *buttRemote[i];

Good luck smile

Wow, all perfect!! smile ty very much

Do not laugh please!!!!

This is my sketch (remember that i'm newbie tongue)

Surely it's badly badly written, but everything works smile

Relays, buttons, all relays off, 3 temperature probes even from the app (I used DHT_noblocking because with the standard one I had annoying delays on buttons/led)

I have this error during compiling but all is ok.. do you know why? it report the last string of RemoteXY configurate (4,6,133,72,0 }; but I have only copied from editor source code)

C:\Users\user\Documents\Arduino\app.ino:51:16: warning: narrowing conversion of '-1' from 'int' to 'const uint8_t {aka const unsigned char}' inside { } [-Wnarrowing]

   4,6,133,72,0 };  

                ^
C:\Users\user\Documents\Arduino\app.ino:51:16: warning: narrowing conversion of '-1' from 'int' to 'const uint8_t {aka const unsigned char}' inside { } [-Wnarrowing]

By the way, this is the code

///////////////////////////////////////////// 
//        RemoteXY include library         // 
///////////////////////////////////////////// 

/* RemoteXY select connection mode and include library */ 
#define REMOTEXY_MODE__ETHERNET_LIB
#include <Ethernet.h>
#include <SPI.h>
#include <RemoteXY.h>
#include <dht_nonblocking.h>
//#include <DHT.h>
//#include <DHT_U.h>
#include <Adafruit_Sensor.h>

/* RemoteXY connection settings */
#define REMOTEXY_ETHERNET_MAC "DE:AD:BE:EF:EF:ED"
#define REMOTEXY_SERVER_PORT 6377

// RemoteXY configurate   
#pragma pack(push, 1) 
uint8_t RemoteXY_CONF[] = 
  { 255,4,0,69,0,22,1,8,13,2,
  130,1,24,0,25,45,0,-1,25,45,
  17,130,1,49,0,25,45,25,0,25,
  44,17,130,1,-1,0,25,45,0,47,
  25,43,17,1,0,7,2,12,12,6,
  3,12,12,2,31,88,0,1,0,30,
  3,12,12,30,3,12,12,2,31,88,
  0,1,0,54,3,12,12,6,50,12,
  12,2,31,88,0,65,2,8,16,9,
  9,8,17,9,9,65,2,31,17,9,
  9,32,17,9,9,65,2,56,17,9,
  9,7,64,9,9,1,0,84,16,12,
  12,34,62,12,12,2,31,88,0,67,
  4,8,32,13,4,7,27,18,5,2,
  26,11,67,4,33,32,13,4,32,27,
  17,5,2,26,11,67,4,59,32,13,
  4,7,76,18,5,2,26,11,67,4,
  8,39,13,4,7,32,18,5,2,26,
  11,67,4,33,39,13,4,32,32,17,
  5,2,26,11,67,4,59,39,13,4,
  7,81,18,5,2,26,11,129,0,1,
  31,6,6,1,27,18,6,133,84,0,
  129,0,1,38,10,6,1,32,6,6,
  133,72,0,129,0,26,31,4,6,26,
  27,4,6,133,84,0,129,0,51,31,
  4,6,1,75,4,6,133,84,0,129,
  0,51,38,4,6,26,32,4,6,133,
  72,0,129,0,26,38,4,6,1,80,
  4,6,133,72,0 };  
   
// this structure defines all the variables of your control interface  
struct { 

    // input variable
  uint8_t button_1; // =1 if button pressed, else =0 
  uint8_t button_2; // =1 if button pressed, else =0 
  uint8_t button_3; // =1 if button pressed, else =0 
  uint8_t button_ALL; // =1 if button pressed, else =0 

    // output variable
  uint8_t led_1_g; // =0..255 LED Red brightness 
  uint8_t led_2_g; // =0..255 LED Red brightness 
  uint8_t led_3_g; // =0..255 LED Red brightness 
  char text_temp1[11];  // string UTF8 end zero 
  char text_temp2[11];  // string UTF8 end zero 
  char text_temp3[11];  // string UTF8 end zero 
  char text_hum1[11];  // string UTF8 end zero 
  char text_hum2[11];  // string UTF8 end zero 
  char text_hum3[11];  // string UTF8 end zero
   
    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY; 

///////////////////////////////////////////// 
//           END RemoteXY include          // 
///////////////////////////////////////////// 

#define DHT_SENSOR_TYPE DHT_TYPE_11
static const int DHT_SENSOR_PIN2 = 2;
DHT_nonblocking dht_sensor1( DHT_SENSOR_PIN2, DHT_SENSOR_TYPE );
static const int DHT_SENSOR_PIN3 = 3;
DHT_nonblocking dht_sensor2( DHT_SENSOR_PIN3, DHT_SENSOR_TYPE );
static const int DHT_SENSOR_PIN4 = 4;
DHT_nonblocking dht_sensor3( DHT_SENSOR_PIN4, DHT_SENSOR_TYPE );

#define buttPinALL 34          // pin pulsante SPEGNI TUTTO
int buttPin[3] = {27,28,29};   // arrow for real switch button 
int relayPin[3] = {30,31,32};  // arrow for real output relay
uint8_t * buttRemoteALL = &RemoteXY.button_ALL;
uint8_t * buttRemote[3] = {&RemoteXY.button_1,&RemoteXY.button_2,&RemoteXY.button_3};
uint8_t * ledRemote[3] = {&RemoteXY.led_1_g, &RemoteXY.led_2_g, &RemoteXY.led_3_g};

int reading[3];
int readingButtALL;
int readingRemote[3];
int readingRemoteALL;

int buttState[3];
int buttStateALL;
int buttRemoteState[3];
int buttRemoteStateALL;

int lastButtState[3];
int lastButtStateALL;
int lastButtRemoteState[3];
int lastButtRemoteStateALL;


int debounceDelay = 50;

int ledState[3];

unsigned long lastDebounceTime[3];
unsigned long lastDebounceTimeALL;


void setup()  
{ 
  RemoteXY_Init ();  
{
  Serial.begin( 115200 );
}

  for (int i = 0; i < 3; i++)
  { pinMode(relayPin[i], OUTPUT);
    pinMode(buttPin[i], INPUT_PULLUP);}
    pinMode(buttPinALL, INPUT_PULLUP);
}  

static bool measure_environment1( float *temperature1, float *humidity1 )
 { static unsigned long measurement_timestamp1 = millis( );
   if( millis( ) - measurement_timestamp1 > 4000ul ) /* Measure once every four seconds. */
   { if( dht_sensor1.measure( temperature1, humidity1 ) == true )
      { measurement_timestamp1 = millis( );
        return( true ); }
      } return( false );}

static bool measure_environment2( float *temperature2, float *humidity2 )
 { static unsigned long measurement_timestamp2 = millis( );
   if( millis( ) - measurement_timestamp2 > 4000ul ) /* Measure once every four seconds. */
   { if( dht_sensor2.measure( temperature2, humidity2 ) == true )
      { measurement_timestamp2 = millis( );
        return( true ); }
      } return( false );}
  
static bool measure_environment3( float *temperature3, float *humidity3 )
 { static unsigned long measurement_timestamp3 = millis( );
  if( millis( ) - measurement_timestamp3 > 4000ul ) /* Measure once every four seconds. */
    { if( dht_sensor3.measure( temperature3, humidity3 ) == true )
      { measurement_timestamp3 = millis( );
        return( true ); }
      } return( false );}

void loop()  
{ RemoteXY_Handler (); 
     
for (int i = 0; i < 3; i++) //lettura pulsanti fisici
    { reading[i] = digitalRead(buttPin[i]);
      if (reading[i] != lastButtState[i]) {
         lastDebounceTime[i] = millis(); 
         lastButtState[i] = reading[i]; }
      if ((millis() - lastDebounceTime[i]) > debounceDelay) {
          if (buttState[i] != lastButtState[i]) {
          buttState[i] = lastButtState[i];
             if (buttState[i] == LOW) {
             ledState[i] = !ledState[i];
             digitalWrite(relayPin[i], ledState[i]); }}}}

for (int i = 0; i < 3; i++) //lettura pulsanti da app remota
    { readingRemote[i] = *buttRemote[i];
      if (readingRemote[i] != lastButtRemoteState[i]) {
         lastDebounceTime[i] = millis(); 
         lastButtRemoteState[i] = readingRemote[i]; }
      if ((millis() - lastDebounceTime[i]) > debounceDelay) {
         if (buttRemoteState[i] != lastButtRemoteState[i]) {
         buttRemoteState[i] = lastButtRemoteState[i];
            if (buttRemoteState[i] == HIGH) {
            ledState[i] = !ledState[i];
            digitalWrite(relayPin[i], ledState[i]);}}}}

      readingButtALL = digitalRead(buttPinALL); // SWITCH OFF da pulsante fisico
      if (readingButtALL != lastButtStateALL) {
         lastDebounceTimeALL = millis(); 
         lastButtStateALL = readingButtALL; }
      if ((millis() - lastDebounceTimeALL) > debounceDelay) {
         if (buttStateALL != lastButtStateALL) {
             buttStateALL = lastButtStateALL;
         if (buttStateALL == LOW) {
            ledState[0] = LOW;
            ledState[1] = LOW;
            ledState[2] = LOW;
            digitalWrite(30, LOW);
            digitalWrite(31, LOW);
            digitalWrite(32, LOW);}}}

      readingRemoteALL = *buttRemoteALL; // SWITCH OFF da pulsante APP
      if (readingRemoteALL != lastButtRemoteStateALL) {
         lastDebounceTimeALL = millis(); 
         lastButtRemoteStateALL = readingRemoteALL; }
      if ((millis() - lastDebounceTimeALL) > debounceDelay) {
         if (buttRemoteStateALL != lastButtRemoteStateALL) {
             buttRemoteStateALL = lastButtRemoteStateALL;
         if (buttRemoteStateALL == HIGH) {
            ledState[0] = LOW;
            ledState[1] = LOW;
            ledState[2] = LOW;
            digitalWrite(30, LOW);
            digitalWrite(31, LOW);
            digitalWrite(32, LOW);}}}           

            
for (int i = 0; i < 3; i++) // accendi/spegni led su app
   { *ledRemote[i] = (digitalRead(relayPin[i])==HIGH)?255:0; }

// lettura delle temperature

{ float temperature1;
  float humidity1;
  if( measure_environment1( &temperature1, &humidity1 ) == true )
  { dtostrf(temperature1, 0, 1, RemoteXY.text_temp1);
    dtostrf(humidity1, 0, 1, RemoteXY.text_hum1); } }

{ float temperature2;
  float humidity2;
  if( measure_environment2( &temperature2, &humidity2 ) == true )
  { dtostrf(temperature2, 0, 1, RemoteXY.text_temp2);
    dtostrf(humidity2, 0, 1, RemoteXY.text_hum2); } }

{ float temperature3;
  float humidity3;
  if( measure_environment3( &temperature3, &humidity3 ) == true )
  { dtostrf(temperature3, 0, 1, RemoteXY.text_temp3);
    dtostrf(humidity3, 0, 1, RemoteXY.text_hum3); } }
}

10

Re: Input / Output Array

Uh!! The problem was simply due to the fact that a graphic panel started just outside the screen tongue

11 (edited by Guillaume 2018-03-16 21:30:09)

Re: Input / Output Array

Good wink

You can solve these warnings by doing this:

uint8_t RemoteXY_CONF[] = 
  { 255,4,0,69,0,22,1,8,13,2,
  130,1,24,0,25,45,0,(uint8_t)-1,25,45,

You could put your sensors in an array too

DHT_nonblocking dhtSensors[] = 
{
  DHT_nonblocking( 2, DHT_TYPE_11 ),
  DHT_nonblocking( 3, DHT_TYPE_11 ),
  DHT_nonblocking( 4, DHT_TYPE_11 ),
};

12

Re: Input / Output Array

Ok, i Will try! smile

Ty!!