BREAKING NEWS

09 November 2015

Arduino, OSC, iPhone and six LEDs - sketching

As promised I made the sketch that loaded Arduino allows you to create your control via iPhone 6 LEDs or 6 loads connected to the outputs 2,3,4,5,6 and 


Only pins 3,5 and 6 are PWM and will then drive the dimming of the LEDs.
Transfer immediately to sketck:
01
02
03
04
05
06
07
08
09
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
#include "Ethernet.h"
#include "OSCClass.h"
#include "SPI.h"
int pinLedA = 2;
int pinLedB = 3;
int pinLedC = 4;
int pinLedD = 5;
int pinLedE = 6;
int pinLedF = 7;
OSCMessage recMes;
OSCClass OSC (& recMes);
serverMac byte [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
serverIP byte [] = {192, 168, 2, 130};
int   serverPort = 10000;
char * topAddress = "butt" ;
char *subAddress[15]={ "ledA" , "ledB" , "ledC" , "ledD" , "ledE" , "ledF" , "ledAB" , "ledBC" , "ledAC" , "ledDE" , "ledEF" , "ledDF" , "fadeB" , "fadeD" , "fadeE" };
void setup () {
     Ethernet.begin (serverMac, serverIP);
     osc.begin (serverPort);
     pinMode (pinLedA, OUTPUT);
     pinMode (pinLedB, OUTPUT);
     pinMode (pinLedC, OUTPUT);
     pinMode (pinLedD, OUTPUT);
     pinMode (pinLedE, OUTPUT);
     pinMode (pinLedF, OUTPUT);
     digitalWrite (pinLedA, LOW);  // LED OFF
     digitalWrite (pinLedB, LOW);  // LED OFF
     digitalWrite (pinLedC, LOW);  // LED OFF
     digitalWrite (pinLedD, LOW);  // LED OFF
     digitalWrite (pinLedE, LOW);  // LED OFF
     digitalWrite (pinLedF, LOW);  // LED OFF
     osc.flush ();
}
void loop () {
    if (osc.available ()) {
          if (! strcmp (recMes.getAddress (0), topAddress)) {
                if (! strcmp (recMes.getAddress (1) Sub Address [0])) {digitalWrite (pinLedA, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [1])) {digitalWrite (pinLedB, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [2])) {digitalWrite (pinLedC, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [3])) {digitalWrite (pinLedD, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [4])) {digitalWrite (pinLedE, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [5])) {digitalWrite (pinLedF, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [6])) {digitalWrite (pinLedA, getIntValue (& recMes)); digitalWrite (pinLedB, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [7])) {digitalWrite (pinLedB, getIntValue (& recMes)); digitalWrite (pinLedC, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [8])) {digitalWrite (pinLedA, getIntValue (& recMes)); digitalWrite (pinLedC, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [9])) {digitalWrite (pinLedD, getIntValue (& recMes)); digitalWrite (pinLedE, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [10])) {digitalWrite (pinLedE, getIntValue (& recMes)); digitalWrite (pinLedF, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [11])) {digitalWrite (pinLedD, getIntValue (& recMes)); digitalWrite (pinLedF, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [12])) {analogWrite (pinLedB, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [13])) {analogWrite (pinLedD, getIntValue (& recMes)); }
                if (! strcmp (recMes.getAddress (1), subaddress [14])) {analogWrite (pinLedE, getIntValue (& recMes)); }
        }
    }
}
// Utility *********************************** *********
int getIntValue (OSCMessage mes *) {
    switch (mes-> getTypeTag (0)) {
        case 'i' { return mes-> getArgInt (0); }
        break ;
        case 'f' { return int (mes-> getArgFloat (0)); }
        break ;
      }
}


In the first lines include the classes that you will use for Ethernet communication and protocol handling OSC, using classes in Arduino allows you to concentrate mainly on functionality not worrying about the complexities of such a protocol.
lines 05-10: Define the pins that connect the LEDs, remember that the pins 10,11,12 and 13 are dedicated to communication with the Ethernet Shield;
Line 12: Sets the constant recMes  that stores all message types OSC sent from your smartphone;
Line 14: inizalizza an object OSC  as an instance of the class of the same name;
Lines 16-18: Lines are now known, it sets the parameters for the ethernet shield;
Line 20: sets a variable of type char  whose value is the root of the message OSC, as you may have read in previous articles the labels OSC consist of similar paths to a path or url internet, are composed of layers of which the first is "/ "(root or root), the message that you have configured in CSOs are the type / butt / Leda where butt  is the highest address of the message and Leda  sub-address;
Line 21: Sets the sub-addresses that you expect: Leda, ledB, LEDC ...
line 24 initializes the class Ethernet Mac and IP defined lines 16-18;
Line 25: Creates a server ripo CSOs risopnde on the port defined on line 18;
Lines 27-32: Define the mode of OUTPUT to the 6-pin they are connected to the LEDs;
Lines 34-39: Sets LOW to the value of power-draw each pin so that all LEDs are turned off;
Line 41: clear the message buffer instance OSC  using the method flush  of class OSC;
line 45: verify that each loop ()  arduino exists a message of type osc , this type of control is very similar to that utilizato in serial communication arduino;
Line 46: Read the value of high-level content're ready to recMes  using the method getAddress (0)  where 0 is the first level, compares this value with the value defined in topAddress  corresponds to the line 20 and if it means that the message will be that expected , you may define multiple types of messages and manage them differently side sketch, I preferred to use the same primary address to make the sketch simpler;
lines 47-52: are all similar except the test run, take into consideration the line 47 compares the value to the level of 1 recMes  with the value of the array subaddress  position 0, this indicates that the condition is verified when the address or label coming from your smartphone / butt / LEDA, in this case assigns the value received: 0 or 1 as a function of pressure or less of the button; the value assignments do it with the command digitalWrite  Arduino and vaore retrieve it with the function getIntValue  declared at lines 71-79;
lines 54-59: identical to the above except that the buttons operate in green (see article on the interface OSC) which are combinations of LEDs, so each command on together with 2 LEDs, the combinations are an example and can vary them as you getting much more complex combinations.
lines 61-63: you need to handle messages of type fade or slide, those applied to pins 3,5 and 6 for driving fading LEDs, in principle, are no different from the previous ones unless the comparison value in the state IF and the command used to turn on the LEDs fade analogWrite  instead of digitalWrite;
Lines 71-79: Define a service function or utility accessory to sketch, in practice the values ​​from the OSC messages can be either integer  (whole) or float  (with a comma) in the first case to be recovered by the method getArgInt ()  in accordance with the method getArgFloat () . However in the sketch are anticipated and managed slo values ​​of type integer, so this function checks the type of value received, it retrieves the correct way and if the float  turns it into always giving full value to the entire sketch.
And here's a video of the project in operation:




Share this:

Post a Comment

 
Back To Top
Distributed By Blogger Templates | Designed By OddThemes