Le programmeur des anneaux ...

Une démonstration d'interruptions multiples

Une activité tirée du forum arduino .

On se propose ici de commander une série de 3 anneaux néopixels à l'aide d'une carte arduino nano et de 4 boutons, il ne restera ensuite qu'a se montrer créatif !

1
2
#define inter 2 //pull-up IN
3
#define button1 3 //pull-up IN
4
#define button2 4 //pull-up IN
5
#define button3 5 //pull-up IN
6
#define button4 6 //pull-up IN
7
8
9
#include <Adafruit_NeoPixel.h>
10
#ifdef __AVR__
11
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
12
#endif
13
14
#define PINA 9 // On Trinket or Gemma, suggest changing this to 1
15
#define PINB 8 
16
#define PINC 7 
17
// How many NeoPixels are attached to the Arduino?
18
#define NUMPIXELS 8 // Popular NeoPixel ring size
19
#define DELAYVAL 500
20
21
Adafruit_NeoPixel pixelsA(NUMPIXELS, PINA, NEO_GRB + NEO_KHZ800);
22
Adafruit_NeoPixel pixelsB(NUMPIXELS, PINB, NEO_GRB + NEO_KHZ800);
23
Adafruit_NeoPixel pixelsC(NUMPIXELS, PINC, NEO_GRB + NEO_KHZ800);
24
25
26
volatile unsigned long button_time = 0;
27
int debounce = 50; // debounce latency in ms
28
29
volatile int trigger = 0;
30
31
32
33
34
void setup() {
35
  Serial.begin(19200);
36
pinMode(button1, INPUT_PULLUP);
37
pinMode(button2, INPUT_PULLUP);
38
pinMode(button3, INPUT_PULLUP);
39
pinMode(button4, INPUT_PULLUP);
40
pinMode(inter, INPUT_PULLUP);
41
pixelsA.begin();
42
pixelsB.begin();
43
pixelsC.begin();
44
pinMode(PINA,OUTPUT);
45
pinMode(PINB,OUTPUT);
46
pinMode(PINC,OUTPUT);
47
Serial.println("Starting...");
48
49
attachInterrupt(digitalPinToInterrupt(inter), detect, FALLING);
50
}
51
52
void loop() {
53
pixelsA.clear(); 
54
pixelsB.clear(); 
55
pixelsC.clear(); 
56
for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
57
58
    // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
59
    // Here we're using a moderately bright green color:
60
    pixelsA.setPixelColor(i, pixelsA.Color(0, 50, 0));
61
    pixelsB.setPixelColor(i, pixelsB.Color(50, 0, 0));
62
    pixelsC.setPixelColor(i, pixelsC.Color(0, 0, 50));
63
    pixelsA.show();   // Send the updated pixel colors to the hardware.
64
 pixelsB.show();
65
 pixelsC.show();
66
    delay(DELAYVAL);
67
}
68
69
}
70
void detect(){
71
  
72
if ( millis()-button_time > 250) {
73
//delayMicroseconds(32000);
74
//Serial.println(digitalRead(button1));
75
//Serial.println(digitalRead(button2));
76
//Serial.println(digitalRead(button3));
77
if (!digitalRead(button1)) trigger=1;
78
if (!digitalRead(button2)) trigger=2;
79
if (!digitalRead(button3)) trigger=3;
80
if (!digitalRead(button4)) trigger=4;
81
Serial.println(trigger);
82
button_time = millis() ;
83
}
84
}
85
86
87
88
installer l'IDE arduino
Démonstration anneaux Néopixels

Le principe du montage, pour voir le câblage , consulter le code démo-anneaux