aboutsummaryrefslogtreecommitdiff
path: root/libraries/Esplora/examples/EsploraRemote/EsploraRemote.ino
blob: 135b26a25b62ff291113554b3bf3f39e20c7db64 (plain)
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
/*
  Esplora Slave

  This sketch allows to test all the Esplora's peripherals.
  It is also used with the ProcessingStart sketch (for Processing).
  
  When uploaded, you can open the Serial monitor and write one of
  the following commands (without quotes) to get an answer:
  
  "D": prints the current value of all sensors, separated by a comma.
       See the dumpInputs() function below to get the meaning of
       each value.
       
  "Rxxx"
  "Gxxx"
  "Bxxx": set the color of the RGB led. For example, write "R255"
          to turn on the red to full brightness, "G128" to turn
          the green to half brightness, or "G0" to turn off
          the green channel.
  
  "Txxxx": play a tone with the buzzer. The number is the
           frequency, e.g. "T440" plays the central A note.
           Write "T0" to turn off the buzzer.
  

  Created on 22 november 2012
  By Enrico Gueli <enrico.gueli@gmail.com>
*/

#include <Esplora.h>

void setup() {
  while(!Serial); // needed for Leonardo-based board like Esplora
  Serial.begin(9600);
}

void loop() {
  if (Serial.available())
    parseCommand();
}

/*
 * This function reads a character from the serial line and
 * decide what to do next. The "what to do" part is given by
 * function it calls (e.g. dumpInputs(), setRed() and so on).
 */
void parseCommand() {
  char cmd = Serial.read();
  switch(cmd) {
    case 'D': dumpInputs(); break;
    case 'R': setRed(); break;
    case 'G': setGreen(); break;
    case 'B': setBlue(); break;
    case 'T': setTone(); break;
  }
}

void dumpInputs() {  
  /*
   * please note: a single row contains two instructions.
   * one is to print the sensor value, the other to print the
   * comma symbol.
   */
  Serial.print(Esplora.readButton(SWITCH_1)); Serial.print(',');
  Serial.print(Esplora.readButton(SWITCH_2)); Serial.print(',');
  Serial.print(Esplora.readButton(SWITCH_3)); Serial.print(',');
  Serial.print(Esplora.readButton(SWITCH_4)); Serial.print(',');
  Serial.print(Esplora.readSlider());         Serial.print(',');
  Serial.print(Esplora.readLightSensor());    Serial.print(',');
  Serial.print(Esplora.readTemperature(DEGREES_C)); Serial.print(',');
  Serial.print(Esplora.readMicrophone());     Serial.print(',');
  Serial.print(Esplora.readJoystickSwitch()); Serial.print(',');
  Serial.print(Esplora.readJoystickX());      Serial.print(',');
  Serial.print(Esplora.readJoystickY());      Serial.print(',');
  Serial.print(Esplora.readAccelerometer(X_AXIS)); Serial.print(',');
  Serial.print(Esplora.readAccelerometer(Y_AXIS)); Serial.print(',');
  Serial.print(Esplora.readAccelerometer(Z_AXIS)); Serial.println();
}

void setRed() {
  Esplora.writeRed(Serial.parseInt());
}

void setGreen() {
  Esplora.writeGreen(Serial.parseInt());
}

void setBlue() {
  Esplora.writeBlue(Serial.parseInt());
}

void setTone() {
  Esplora.tone(Serial.parseInt());
}