#include "constants.h" #include //#include #include #include #include #include "SSD1306Wire.h" // legacy: #include "SSD1306.h" #define NUM_LEDS 1 #define STEP_INPUT 2 #define SW_A_INPUT 3 #define SW_B_INPUT 4 #define MP3_RX 5 #define SDA_PIN 6 #define SCL_PIN 7 #define LED_OUT 21 #define S1_INPUT 8 #define MP3_TX 10 #define STEP_OUTPUT 9 #define DEBOUNCE_DELAY_MS 100 #define MP3_1_DURATION_MS 1000 #define MP3_2_DURATION_MS 3000 bool mp3_launched = false; unsigned long mp3_launch_time; long step_response_result; CRGB leds[NUM_LEDS]; //EspSoftwareSerial::UART mySoftwareSerial; DFRobotDFPlayerMini myDFPlayer; // Initialize the OLED display using Arduino Wire: #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup_pins(){ pinMode(SW_A_INPUT,INPUT_PULLUP); pinMode(SW_B_INPUT,INPUT_PULLUP); pinMode(S1_INPUT,INPUT_PULLUP); pinMode(MP3_RX,INPUT); pinMode(LED_OUT,OUTPUT); pinMode(STEP_OUTPUT,OUTPUT); pinMode(SDA_PIN,OUTPUT); pinMode(SCL_PIN,OUTPUT); } void setup_display(){ if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } // Show initial display buffer contents on the screen -- // the library initializes this with an Adafruit splash screen. display.display(); delay(1000); // Pause for 2 seconds // Clear the buffer display.clearDisplay(); display.clearDisplay(); display.drawBitmap(0, 0, epd_bitmap_logo_TP, 128, 64, 1); display.display(); delay(1000); display.clearDisplay(); display.drawBitmap(0, 0, epd_bitmap_logo_ip_paris, 128, 64, 1); display.display(); delay(1000); } void setup() { setup_display(); FastLED.addLeds(leds, NUM_LEDS); // GRB ordering is assumed FastLED.setBrightness(100); leds[0] = CRGB::Black; FastLED.show(); Serial.begin(9600); for(int i=0;i<5;i++){ leds[0] = CRGB::Green; FastLED.show(); delay(500); leds[0] = CRGB::Black; FastLED.show(); delay(500); } // mySoftwareSerial.begin(38400, SWSERIAL_8N1, MP3_RX, MP3_TX, false); // if (!mySoftwareSerial) { // If the object did not initialize, then its configuration is invalid // Serial.println("Invalid EspSoftwareSerial pin configuration, check config"); // while (1) { // Don't continue with invalid configuration // delay (1000); // } // } // mySoftwareSerial.begin(9600); // myDFPlayer.begin(mySoftwareSerial); // myDFPlayer.setTimeOut(500); // myDFPlayer.play(1); // Joue le premier son // mp3_launch_time = millis(); } void display_value_on_oled(int value){ display.clear(); display.drawString(0, 20,"SR value:"); display.drawString(50, 20,String(value)); display.display(); } void loop() { step_response_result = 0;//tx_rx(); display_value_on_oled(step_response_result); int sw_A = 1;//digitalRead(SW_A_INPUT); int sw_B = 1;//digitalRead(SW_B_INPUT); int sw_1 = 1;//digitalRead(S1_INPUT); if(!sw_A){ Serial.print("sw_A pressed!"); leds[0] = CRGB::Red; //CHSV( hue, 255, 255) FastLED.show(); delay(DEBOUNCE_DELAY_MS); } if(!sw_B){ Serial.print("sw_B pressed!"); leds[0] = CRGB::Green; FastLED.show(); delay(DEBOUNCE_DELAY_MS); } if(!sw_1){ Serial.print("sw_1 pressed!"); leds[0] = CRGB::Blue; FastLED.show(); if((millis() - mp3_launch_time) > MP3_2_DURATION_MS){ myDFPlayer.play(2); // Play the second mp3 mp3_launch_time = millis(); } delay(DEBOUNCE_DELAY_MS); } if(sw_A&&sw_B&&sw_1){ leds[0] = CRGB::Black; FastLED.show(); } yield(); } long tx_rx(){ //Function to execute rx_tx algorithm and return a value //that depends on coupling of two electrodes. //Value returned is a long integer. int read_high; int read_low; int diff; long int sum; int N_samples = 100; //Number of samples to take. Larger number slows it down, but reduces scatter. sum = 0; for (int i = 0; i < N_samples; i++){ digitalWrite(STEP_OUTPUT,HIGH); //Step the voltage high on conductor 1. read_high = analogRead(STEP_INPUT); //Measure response of conductor 2. delayMicroseconds(100); //Delay to reach steady state. digitalWrite(STEP_OUTPUT,LOW); //Step the voltage to zero on conductor 1. read_low = analogRead(STEP_INPUT); //Measure response of conductor 2. diff = read_high - read_low; //desired answer is the difference between high and low. sum += diff; //Sums up N_samples of these measurements. } return sum; } //End of tx_rx function.