a little programming help please.



  • I can write some pretty good *nix shell scripts and windows scripts. but don't know any C programming.
    I'm trying to write a for loop but can't seem to get it to work, the issue is the variables i want to use.
    something like this
    var1 = 123
    var2 = 456
    var3 = 789

    setup
    loop
    for X blah blah
    myaction(varX)

    where varX is = to the var 1 2 or 3 above.

    is something like that possible with arduino code or am i barking up the wrong tree?

    thanks



  • Sounds like you might want to use an array. Something like :

     int myVars[] = { 123,456,789 };
     
     for (int i = 0; i < (sizeof(myVars)/sizeof(int)) ; i++) {
         Serial.println(myVars[i]);
     }
    

    should work, though depending on exactly what you want to accomplish, and what controller you are using, etc. there might be some better approach.



  • thanks for looking at this for me.
    I gave your suggestion a try, i'm still having issues

    char* outlet[]={"0","1111111","2222222","3333333"};
    where S = to one of the elements in the array
    Serial.println(outlet[(S)]); <-- this works
    mySwitch.send(outlet[(S)], 24); <-- this errors

    error: invalid conversion from 'char*' to 'long unsigned int' [-fpermissive]

    why does it print, but not put that same value in the next command?
    I want it to work like this
    myswitch.send (VALUE of the element in the arra,24);
    how do i get that value in there?


  • Hero Member

    @mvader

    You are using a "setter" for a float value (or pointer) see API

    // Setters for payload
    MyMessage& set(void* payload, uint8_t length);
    MyMessage& set(const char* value);
    MyMessage& set(uint8_t value);
    MyMessage& set(float value, uint8_t decimals);
    MyMessage& set(unsigned long value);
    MyMessage& set(long value);
    MyMessage& set(unsigned int value);
    MyMessage& set(int value);
    


  • so the value of S is a number it's the sensor ID # that my vera has assigned to my outlets
    when i click "ON" in vera, it will send (if say outlet 3) S will then equal 3
    i then want to send out the code that is assigned to outlet 3
    i was using an array to store them, but i would be fine defining outlet1= outlet2= etc
    either way when i get to the function as the bottom
    i want the code to show outlet(S) S being the outlet, but outlet(S) should = 1111641 and that is what code i want sent out
    i can't seem to figure out how to make that happen
    in a shell script i can just do outlet$S and then that = outlet3 (the value of)
    can you do that in arduino/C ?

    #include <MySensor.h>
    #include <SPI.h>  
    #include <RCSwitch.h>
    
    #define RF433_CHILD_ID 0
    #define NUMBER_OF_OUTLETS 4 
    #define char* outlet[]={"0","1119683","1111641","1114582"};   
    
    MySensor gw;
    RCSwitch mySwitch = RCSwitch();
    
    void setup() 
    {
    mySwitch.enableTransmit(2); 
    Serial.begin(115200); 
    gw.begin(incomingMessage, AUTO, true);
    gw.sendSketchInfo("RF433C", "1.0");
    
    for(int i=0; i<NUMBER_OF_OUTLETS;i++)
    {
    gw.present(i+1, S_LIGHT);
    }
    
    }
    
    void loop()
    {
      gw.process();
    }
    
    void incomingMessage(const MyMessage &message)
    {
      if (message.type==V_LIGHT) {
      int incomingLightState =  message.getBool(); 
      int S = message.sensor;
    
     if (incomingLightState==1)
     {
     Serial.println(outlet[(S)]);
     mySwitch.send(outlet[(S)], 24);
     } 
      }
    }
    
    


  • Maybe you could post your entire sketch here?



  • @soward said:

    Maybe you could post your entire sketch here?

    that is my entire sketch (above your post) lol
    i have not been able to get any further


  • Hero Member

    @mvader
    I don't know what you are trying to accomplish but RCSwtich.send expects either a character array (= implicit pointer) or an "unsigned long" Code with a length value. I does not have anything to do with MySensors. It is probably easier for you to browse through the RCSwitch examples before combining it with the MySensors API (which has similar characteristics..

    Below the RCSwitch definitions.

    void send(unsigned long Code, unsigned int length);
    void send(char* Code);
    


  • @AWI
    i'm confused why you say it doesn't have anything to do with mysensors.
    i have a working sketch that controls 433 outlets on my - mysensors network
    the issue i have is i'm trying to shorten the sketch.
    the way it is written now, it defines an on and off code for each outlet
    if you have many outlets that's allot of "IF" statements.
    in the shell script world i could easily shorten that by using a variable and for loop.
    as admitted i'm a scripter and know little about c/arduino programming
    here is a modified working sketch

    /*  RF433Mhz Transmitter node:
        This sketch allows On-Off-Keying (OOK) control of four 433Mhz relay outlets
        which utilize the common PT2262 encoding chip for signal transmission. The sketch could
        be easily expanded to include additional outlets.  The sensor node consists of a nano 
        connected to a NRF24L01 and a 433Mhz transmitter connected to pin 3, 5V and Gnd.  The 
        transmitter can run off of 3.3V as well. 
        
        The sketch is based on the MySensors project (http://www.mysensors.org). 
        Submitted by Dwalt.
    */ 
    
    //  Include related libraries
    #include <MySensor.h>
    #include <SPI.h>  
    #include <RCSwitch.h>
    
    //  Define Constants
    #define RF433_CHILD_ID 0
    #define NUMBER_OF_OUTLETS 4 // Each outlet will have 2 OOK codes
    //#define SEND_DATA 2
    
    #define CODE_1On 1119683
    #define CODE_1Off 1119692
    #define CODE_2On 6287580
    #define CODE_2Off 6287572
    #define CODE_3On 6287578
    #define CODE_3Off 6287570
    #define CODE_4On 6287577
    #define CODE_4Off 6287569
    
    MySensor gw;
    
    RCSwitch mySwitch = RCSwitch();
    
    void setup() 
    {
      
    
    mySwitch.enableTransmit(2); 
      
     Serial.begin(115200); // Not sure why this was included
    
     //  The node is mains powered, so why not make it a repeater.
     gw.begin(incomingMessage, AUTO, true);
     
     // Send the sketch version information to gateway
     gw.sendSketchInfo("RF433", "1.1");
     
     // Register outlets to gw (they will be created as child devices)
     for(int i=0; i<NUMBER_OF_OUTLETS;i++) {
       gw.present(i+1, S_LIGHT);
    
     }
        
    }
    
    void loop() {
      gw.process();
    }
      void incomingMessage(const MyMessage &message) {
      
      if (message.type==V_LIGHT) {
      int incomingLightState =  message.getBool(); 
      int incomingOutlet = message.sensor;
      
      Serial.print("Outlet #: ");
      Serial.println(message.sensor);
      Serial.print("Command: ");
      Serial.println(message.getBool());
     
     if (incomingOutlet==1) {
     if (incomingLightState==1) {
        // Turn on  socket 1
        Serial.println("Turn on Socket 1");
     mySwitch.send(CODE_1On, 24); // These codes are unique to each outlet
     delay(50); 
     }
     if (incomingLightState==0)  {
        // Turn off socket 1
     Serial.println("Turn off Socket 1");
    mySwitch.send(CODE_1Off, 24);
    delay(50); 
     }
     }
     if (incomingOutlet==2) {
     if (incomingLightState==1) {
        // Turn on  socket 2
        Serial.println("Turn on Socket 2");
    mySwitch.send(CODE_2On, 24);
     delay(50); 
     }
     if (incomingLightState==0)  {
        // Turn off socket 2
     Serial.println("Turn off Socket 2");
    mySwitch.send(CODE_2Off, 24);
    delay(50); 
     }
     }
     if (incomingOutlet==3) {
     if (incomingLightState==1) {
        // Turn on  socket 3
        Serial.println("Turn on Socket 3");
    mySwitch.send(CODE_3On, 24);
     delay(50); 
     }
     if (incomingLightState==0)  {
        // Turn off socket 3
     Serial.println("Turn off Socket 3");
    mySwitch.send(CODE_3Off, 24);
    delay(50); 
     }
     }
     if (incomingOutlet==4) {
     if (incomingLightState==1) {
        // Turn on  socket 4
        Serial.println("Turn on Socket 4");
     mySwitch.send(CODE_4On, 24);
     delay(50); 
     }
     if (incomingLightState==0)  {
        // Turn off socket 4
     Serial.println("Turn off Socket 4");
    mySwitch.send(CODE_4Off, 24);
    delay(50); 
     }
     }
      }
     delay(50);
     }    
        
    

  • Hero Member

    @mvader If you look at the similar statement in this sketch:

    #define CODE_1On 1119683
    (better written as:)
    const int CODE_1On = 1119683   // define integer with value
    mySwitch.send(CODE_1On, 24); // These codes are unique to each outlet
    

    CODE_10n is an Integer "type"

    In the other sketch you are using "outlet[(S)]" which is a char array (or pointer) type.

    #define char* outlet[]={"0","1119683","1111641","1114582"};
    (better written as:)
    const char outlet[] = {"0","1119683","1111641","1114582"};   // define an array with character arrays
    
     Serial.println(outlet[(S)]);
     mySwitch.send(outlet[(S)], 24);
    

    While the mySwitch.send( .. ) either wants a character array ("ABCDEF12121") or an integer with length (12234, 24)

    C++ does not automatically convert types...



  • I must still be missing something.
    appreciate the correction on the declared part.
    however, i still don't know how to make outlet(S) (where S is the outlet number)
    work.
    i constantly get a "outlet" is not defined.. etc
    outlet is not specifically defined, but i'm not trying to send just "outlet"
    i'm trying to send the value of outlet3
    the variable S is supposed to represent the 3 (or whatever other outlet # is sent back by vera)

    this allows me to use 1 piece of code, instead of saying
    if outlet 1
    if outlet 2
    if outlet 3
    etc
    i can just say outlet(S)
    but that' isn't working. why? not possible or not coded correctly?


  • Hero Member

    @mvader sorry for confusing you.. try this:

    const unsigned long outlet[]= {0,1119683,1111641,1114582};   
    

    i.s.o.

    #define char* outlet[]={"0","1119683","1111641","1114582"}; 
    


  • I think that may have done the trick.. i'll follow up in a bit
    thanks!


Log in to reply
 

Suggested Topics

181
Online

10.2k
Users

10.6k
Topics

109.2k
Posts