Progetto

Generale

Profilo

File » tribebots_05.03.2015_A70.ino

Codice Arduino - Moreno Petrucci, 06-03-2015 14:13

 
1
// TribeBots
2
// last update: 05.03.2015
3
// dev.hacklabetrni.org
4

    
5
#include <IRremote.h>
6
#include <IRremoteInt.h>
7

    
8
/*
9
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
10
 * An IR detector/demodulator must be connected to the input RECV_PIN.
11
 * Version 0.1 July, 2009
12
 * Copyright 2009 Ken Shirriff
13
 * http://arcfn.com
14
 */
15
 
16

    
17
int RECV_PIN = 11;
18

    
19
IRrecv irrecv(RECV_PIN);
20
IRsend irsend;
21

    
22
boolean right;
23
boolean left;
24
boolean stopped;
25
boolean play_1;
26
boolean play_2;
27

    
28
boolean ch_free; // Canale libero o occupato
29

    
30
unsigned long previousMillis = 0;
31
unsigned long previousMillis_2 = 0;
32
const long interval = 8000; // Ogni 8 sec TX  
33
const long interval_2 = 10000; // Tempo durata stato agitato
34

    
35
// RGB Led Control
36
const int yellowLEDPin = 9;    // LED connected to digital pin 9
37
const int redLEDPin = 8;     // LED connected to digital pin 8
38
const int blueLEDPin = 7; 
39

    
40
decode_results results;
41

    
42
void setup()
43
{
44
  //Serial.begin(9600);
45
  irrecv.enableIRIn(); // Start the receiver
46
  
47
  pinMode(10, OUTPUT); // Motor 2
48
  pinMode(5, OUTPUT); // Motor 1
49
  pinMode(13, OUTPUT); //Led (opzionale)
50
  
51
  // pin RGB Led
52
  pinMode(yellowLEDPin,OUTPUT);
53
  pinMode(redLEDPin,OUTPUT);
54
  pinMode(blueLEDPin,OUTPUT);
55

    
56
 ch_free = true;
57
}
58

    
59
void loop() {
60
  
61
  
62
  
63
 if (left) {
64
          analogWrite(5, 100);
65
          analogWrite(10, 0);
66
 }
67
 else if (right) {
68
          analogWrite(10, 100);
69
          analogWrite(5, 0);
70
 }
71
 else if (stopped) {
72
          analogWrite(10, 0);
73
          analogWrite(5, 0);
74
}
75
else if (play_1) { //calmo
76
          analogWrite(10, 50);
77
          analogWrite(5, 50);
78
}
79
else if (play_2) { //agitato
80
          analogWrite(10, 110);
81
          analogWrite(5, 110);
82
}
83
  
84
  
85
  unsigned long currentMillis = millis();
86
 
87
  if( (currentMillis - previousMillis >= interval) && ch_free == false) {
88
        previousMillis = currentMillis; 
89
        ch_free = true;   
90
   }
91
  
92
      // Durata di 10 sec. per stato agitato, dopo 10 sec. torna calmo
93
      if( (currentMillis - previousMillis_2 >= interval_2) && play_2 == true) {
94
          previousMillis_2 = currentMillis; 
95
          play_2 = false;   
96
          play_1 = true;
97
          Serial.println(currentMillis);
98
      }
99
  
100
  
101
  // Se il canale è libero LED Verde altrimenti ROSSO
102
  if (ch_free) {
103
      digitalWrite(redLEDPin, HIGH);
104
      digitalWrite(yellowLEDPin, LOW);
105
      digitalWrite(blueLEDPin, HIGH);
106
  } else {
107
      digitalWrite(redLEDPin, LOW);
108
      digitalWrite(yellowLEDPin, HIGH);
109
      digitalWrite(blueLEDPin, HIGH);
110
  }
111
     
112
  // Ricezione dati IR
113
  if (irrecv.decode(&results)) {
114
    
115
    ch_free = false;
116
    
117
    
118
      //Serial.println(results.value, HEX);
119
      
120
      // Led rosso quando si ricevono dati
121
      digitalWrite(redLEDPin, HIGH);
122
      digitalWrite(yellowLEDPin, LOW);
123
      digitalWrite(blueLEDPin, LOW);   
124

    
125
    switch(results.value) {
126
    
127
        //Alcuni codici sono inviati da telecomando SONY
128
        case (0x39C): // Vai a destra  FF BUTTON
129
          left = false;
130
          right = true;
131
          stopped = false;
132
          play_1 = false;
133
          break;
134
        case (0xD9C): // Vai a sinistra REW BUTTON
135
          left = true;
136
          right = false;
137
          stopped = false;
138
          play_1 = false;
139
          break;
140
        case (0x19C): // stop  STOP BUTTON
141
          left = false;
142
          right = false;
143
          stopped = true;
144
          play_1 = false;
145
          break;
146
        case (0x59C): // movimento tranquillo PLAY BUTTON
147
          play_1 = true;
148
          stopped = false;
149
          left = false;
150
          right = false;
151
//            digitalWrite(redLEDPin, HIGH);
152
//            digitalWrite(yellowLEDPin, LOW);
153
//            digitalWrite(blueLEDPin, LOW);
154
          break;
155
        case (0x4C9D): // movimento agitato RED BUTTON
156
          play_2 = true;
157
          play_1 = false;
158
          stopped = false;
159
          left = false;
160
          right = false;
161
          break;
162
        case (0xA60): // movimento agitato Ricevuto da un TribeBot "diverso - di un altra Tribù ;-) "
163
          if (stopped) {break;}
164
          play_2 = true;
165
          play_1 = false;
166
          stopped = false;
167
          left = false;
168
          right = false;
169
          digitalWrite(redLEDPin, HIGH);
170
          digitalWrite(yellowLEDPin, HIGH);
171
          digitalWrite(blueLEDPin, HIGH);
172
          break;
173
    
174
    }
175
        irrecv.resume(); // Receive the next value
176
  }
177
  delay(100);
178
  
179

    
180
   // Trasmetti solo se il canale è libero
181
   if (ch_free) {
182

    
183
      for (int i = 0; i < 3; i++) {
184
          irsend.sendSony(0xA70, 12); // Codifica Sony TV power code
185
          delay(40);
186
       }
187
       irrecv.enableIRIn(); // Riabilita la ricezione
188
       
189
       ch_free = false;
190
       
191
//       digitalWrite(redLEDPin, HIGH);
192
//       digitalWrite(yellowLEDPin, HIGH);
193
//       digitalWrite(blueLEDPin, HIGH);
194
       delay(60);
195
   }
196
}
(6-6/86)