| | 30 | |
| | 31 | |
| | 32 | == Correction == |
| | 33 | |
| | 34 | ** Affichage de la lumière sur l'écran OLED** |
| | 35 | {{{#!c |
| | 36 | #include <SPI.h> |
| | 37 | #include <Wire.h> |
| | 38 | #include <Adafruit_GFX.h> |
| | 39 | #include <Adafruit_SSD1306.h> |
| | 40 | |
| | 41 | // unsigned int waitFor(timer, period) |
| | 42 | // Timer pour taches périodique |
| | 43 | // arguments : |
| | 44 | // - timer : numéro de timer entre 0 et MAX_WAIT_FOR_TIMER-1 |
| | 45 | // - period : période souhaitée |
| | 46 | // retour : |
| | 47 | // - nombre de période écoulée depuis le dernier appel |
| | 48 | // |
| | 49 | #define MAX_WAIT_FOR_TIMER 16 |
| | 50 | unsigned int waitFor(int timer, unsigned long period){ |
| | 51 | static unsigned long waitForTimer[MAX_WAIT_FOR_TIMER]; |
| | 52 | unsigned long newTime = micros() / period; |
| | 53 | int delta = newTime - waitForTimer[timer]; |
| | 54 | if ( delta < 0 ) delta += 1 + (0xFFFFFFFF / period); |
| | 55 | if ( delta ) waitForTimer[timer] = newTime; |
| | 56 | return delta; |
| | 57 | } |
| | 58 | |
| | 59 | // Configuration des broches |
| | 60 | #define PIN_LUMI 1 |
| | 61 | #define OLED_RESET 4 |
| | 62 | |
| | 63 | Adafruit_SSD1306 display(OLED_RESET); |
| | 64 | |
| | 65 | |
| | 66 | // Variables globales pour la communication inter-taches |
| | 67 | byte lumi, lumiFull; |
| | 68 | |
| | 69 | void Lumi (int timer, unsigned long period, byte pin, byte *lumi, byte *lumiFull) |
| | 70 | { |
| | 71 | if (!waitFor(timer,period)) return; |
| | 72 | *lumi = map(analogRead(pin),0,1023,0,99); |
| | 73 | *lumiFull = 1; |
| | 74 | } |
| | 75 | |
| | 76 | void Oled1 (byte *mess, byte *full) { |
| | 77 | if (! (*full) ) return; |
| | 78 | *full = 0; |
| | 79 | Serial.println(*mess); |
| | 80 | display.clearDisplay(); |
| | 81 | display.setCursor(0,0); |
| | 82 | display.print("Lumiere : "); |
| | 83 | display.println(*mess); |
| | 84 | display.display(); |
| | 85 | } |
| | 86 | |
| | 87 | void setup() { |
| | 88 | Serial.begin(115200); |
| | 89 | display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) |
| | 90 | display.clearDisplay(); |
| | 91 | display.setTextSize(1); |
| | 92 | display.setTextColor(WHITE); |
| | 93 | } |
| | 94 | |
| | 95 | void loop() { |
| | 96 | Lumi (0,1000000, PIN_LUMI, &lumi, &lumiFull); |
| | 97 | Oled1 (&lumi, &lumiFull); |
| | 98 | } |
| | 99 | }}} |