trying to create a method with special variable name

Hi there!

I am pretty much a beginner probably trying to hack together something a little more advanced than I should! Any and all advice is greatly appreciated!

Essentially I am trying to create two methods to take the place of two repetitive blocks of code but I'm not sure how to instantiate the variables correctly.

In the code below I would like to create a method to take the place of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
////////////  variables in method would take the place of /ld0 and led0
 if (msg.fullMatch("/ld0", 0)){
      // then check if first item is an integer
      if (msg.isInt(0)){
        // use the integer argument for controlling the led 0 
        led0 = (msg.getInt(0));
      }
      Serial.println (led0);
    } 
///////////

/////////// and:

/////////// variables in method would take the place of led0 and the "0" in leds[0]

 {
    if (led0 == 1){
      leds[0] = CHSV( 224, 187, 150);
    }

    if (led0 == 0){
      leds[0] = CHSV( 0, 0, 0);
    }
  }
///////////

/////////// Below is my full currently working code without the methods
#include <OSCMessage.h> 
#include <SLIPEncodedSerial.h>
// these two includes are enough for our purpose
// we don't have to import the whole OSC library

#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 3

#define BAUDRATE 57600
// 9600 is too slow

//sets up an array that can be manipulated to set/clear data.
CRGB leds [NUM_LEDS];

SLIPEncodedSerial SLIPSerial(Serial);

int analogValue = 0;
long oldTime = 0;
long newTime = 0;

//tells the library that there is a strand of WS2812 leds on pin 3 and 
//that they will use the "leds" array and that there are 60 leds


void setup()
{
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  pinMode(13, OUTPUT);
  SLIPSerial.begin(BAUDRATE);
}


  static int led0; //led button 0
  static int led1; //led button 1
  static int led2; //led button 2

// define methods????



// end methods 

void loop()
{
    FastLED.clear();

  // look for incoming OSC messages:

  // first check if bytes are available
  while(SLIPSerial.available())
  {
    // create empty OSC Message
    OSCMessage msg;
    // fill OSC message with incoming bytes till you reach end of packet //
    while(!SLIPSerial.endofPacket())
    {
      int size = SLIPSerial.available();
      while (size--)
      {
        msg.fill(SLIPSerial.read());
      }
    }    

    ////test individual OSC receives      
    if (msg.fullMatch("/ld0", 0)){
      // then check if first item is an integer
      if (msg.isInt(0)){
        // use the integer argument for controlling the led 0 
        led0 = (msg.getInt(0));
      }
      Serial.println (led0);
    } 

    if (msg.fullMatch("/ld1", 0)){
      // then check if first item is an integer
      if (msg.isInt(0)){
        // use the integer argument for controlling led 1 
        led1 = (msg.getInt(0));
      }
      Serial.println (led1);
    } 

    if (msg.fullMatch("/ld2", 0)){
      // then check if first item is an integer
      if (msg.isInt(0)){
        // use the integer argument for controlling led 1 
        led2 = (msg.getInt(0));
      }
      Serial.println (led2);
    }
    
    


  //////////  testing single LED on/off::
  {
    if (led0 == 1){
      leds[0] = CHSV( 224, 187, 150);
    }

    if (led0 == 0){
      leds[0] = CHSV( 0, 0, 0);
    }
  }

  {
    if (led1 == 1){
      leds[1] = CHSV( 80, 187, 150);   
    }

    if (led1 == 0){
      leds[1] = CHSV( 0, 0, 0);
    }

  }

  {
    if (led2 == 1){
      leds[2] = CHSV( 140, 187, 150);   
    }

    if (led2 == 0){
     leds[2] = CHSV( 0, 0, 0);
    }

  }

  ////////// 
    FastLED.show();
  }

}

Last edited on
Topic archived. No new replies allowed.