Forums Coding Corner Sensors and RTC displayed on digitiser

Viewing 1 reply thread
  • Author
    Posts
    • #6863
      PBH

      Really grateful towards Martyn. His Kickstarter project now implemented with sensorarray and digitiser. His quick respons to a problem I had is also much appreciated. The boards work perfectly and the explanations helped me on my way as I never worked with these kinds of electronics before. I now managed to finish my first sketch with these boards. I don’t know if this is the place to share, but anyway here it is. It should be self explanatory.

      /* This sketch is based on Short Circuits (kickstarter) basic sketches and uses the
      Short Circuits boards Motherboard (MB), Digitiser (DI) and Sensor array (SE).
      It shows time and temperature alternatively every minute (mode1 by sw1) and runs through
      time/date/year/temp/humidity/noiselevel/lightlevel (modes 2-7) through sw2 with output to DI.
      The LEDs show which mode is current, except for mode6 noiselevel where the LEDS warn about the
      noiselevel.
      The brightness of DI output is controlled by the LDR
      Not used: DI Pot and SE card reader
      Attention to the comments in setup() to initialise the RTC

      Motherboard connections:
      D2 SE DHT
      D3 (PWM) DI SER
      D4 DI CLK
      D5 (PWM) DI LAT
      D6 (PWM) DI OE
      D7-8,9(PWM) available
      D10 (PWM) SE CS
      D11 (PWM) SE Din
      D12 SE Dout
      D13 SE CLK
      A0 DI Sw1 (can use any pin)
      A1 DI Sw2 (can use any pin)
      A2 SE MIC (needs analog pin)
      A3 SE LDR (needs analog pin)
      A4-5 MB RTC (internally connected)
      */

      // INITIALISATION FOR THE DHT
      #include <DHT_U.h> // includes DHT_U library
      #define DHTPIN 2 // digital pin connected to the DHT sensor
      #define DHTTYPE DHT11 // indicate the sensor type: DHT11 or DHT22
      DHT_Unified dht(DHTPIN, DHTTYPE); // sets the pin and type

      // INITIALISATION FOR THE RTC #1/2
      #include <RTClib.h> // include RTClib library
      RTC_DS1307 rtc; // create an instance of the DS1307

      // INPUT PIN VARIABLES AND INITIALISATION FOR THE DIGITISER
      const int SER = 3;
      const int CLK = 4;
      const int LATCH = 5;
      const int OE = 6;
      const int sw1 = A0;
      const int sw2 = A1;

      /* bits in sequence: LED 1,2,3,4, Digit 4,3,2,1 (U5). LED on = 1, Digit on = 0 */
      const int digit[4] = { B00001110,B00001101,B00001011,B00000111 };

      /* digit sequence: G, F, A, B, DP, C, D, E. Check: Digit on = 0 */
      const int number[10] = { B10001000,B11101011,B01001100,B01001001,B00101011,
      B00011001,B00111000,B11001011,B00001000,B00001011};
      int digit0 = digit[0]; // to switch digit0 on
      int digit1 = digit[1]; // to switch digit1 on
      int digit2 = digit[2]; // to switch digit2 on
      int digit3 = digit[3]; // to switch digit3 on

      // INPUT PIN VARIABLES AND INITIALISATION FOR THE SENSOR ARRAY
      const int MIC = A2;
      const int LDR = A3;

      int micMin = 1024; // Sets micMin to 1024 before the readings decrease the number
      int micMax = 0; // Sets micMax to 0 before the readings increase the number
      int micReading = 0; // variable to hold mics raw input value
      int light = 0; // variable to hold light level reading

      // INITIALISATION FOR MAIN LOOP
      int loopSize = 50; // to calculate average noiselevel
      int dimVal = 0; // to dim LEDs and digits
      int thousands = 0; // value for digit0
      int hundreds = 0; // value for digit1
      int tens = 0; // value for digit2
      int ones = 0; // value for digit3
      int noiseCounter = 0; // to determine average noiselevel
      int micVal = 0; // to determine average noiselevel
      int modeSwitchCounter = 0; // to delay automatical mode switches
      int mode = 1; // 1:time, 2:date, 3:year, 4:temp, 5:humidity, 6:sound, 7:light
      int sw1State = 0; // state of button1
      int sw1StateLast = 0; // last state of button 1
      int sw2State = 0; // state of button2
      int sw2StateLast = 0; // last state of button 2
      int digitValue = 0; // value to be displayed
      int secondPrevious = 0; // if value of second (time) changes > …
      int loopWait = 10; // 10 seconds wait before automatic mode switch
      int decimalPoint = 0; // decimal point for digit1
      int letterC = B10011100; // prepares for displaying ‘C’ on digit3 in mode4
      int letterH = B00101010; // prepares for displaying ‘H’ on digit3 in mode5
      int letterL = B10111100; // prepares for displaying ‘L’ on digit0 in mode7
      bool interval = true; // to change modes automatically

      //Void Setup – runs once at start
      void setup() {
      // Serial.begin(9600); // Initialise Serial Monitor only for checks
      rtc.begin(); // initialise RTC
      dht.begin(); // initialise DHT sensor

      pinMode(SER, OUTPUT); // SER (data) pin as output
      pinMode(CLK, OUTPUT); // CLK (clock) pin as output
      pinMode(LATCH, OUTPUT); // LATCH pin as output
      pinMode(OE, OUTPUT); // Output Enable

      analogWrite(OE, 255); // Set brightness minimal (0 = full brightness)

      pinMode(sw1, INPUT); // Switch 1
      pinMode(sw2, INPUT); // Switch 2
      pinMode(LDR, INPUT); // Declaring the LDR pin as an input

      // INITIALISATION FOR THE RTC #2/2
      /*
      To set the time on the RTC to match your computer’s clock, uncomment the next line and upload the code.
      To prevent it resetting to the same time every time you reset the device, add the “//” to the beginning of the line then uplaod the code again.
      The time will then be set and saved even after a reset (with a coin cell present).
      */
      //rtc.adjust(DateTime(__DATE__,__TIME__)); // Set the RTC to Compile Date and Time
      }

      //Void Loop – repeats forever

      void loop() {

      // DIM LIGHTS
      light = analogRead(LDR); // save current LDR value to variable
      dimVal = map(light,0,1024,255,0); // dimming value
      analogWrite(OE,dimVal); // set brightness of digits and LEDs. less light > less brightness

      // STATE OF SWITCHES
      sw1State = digitalRead(sw1);
      sw2State = digitalRead(sw2);
      if(sw1State != sw1StateLast){
      if(sw1State == HIGH){
      mode = 1;
      interval = true;
      }
      delay(10); // delay to avoid bouncing
      sw1StateLast = sw1State;
      }
      if(sw2State != sw2StateLast){
      if(sw2State == HIGH){
      mode++;
      interval = false;
      if(mode > 7){
      mode = 1;
      }
      }
      delay(10); // delay to avoid bouncing
      sw2StateLast = sw2State;
      }

      // EXECUTE MODES
      digit0 = digit[0]; // switching all digits on (again)
      digit1 = digit[1];
      digit2 = digit[2];
      digit3 = digit[3];

      DateTime now = rtc.now(); // get the current time

      if(interval){ // at start of program, or when sw1 is activated
      if(now.second() != secondPrevious){ // every second
      secondPrevious = now.second();
      modeSwitchCounter++;
      if(modeSwitchCounter >= loopWait){ // change mode 1<->4 after loopWait seconds
      if(mode == 1){
      mode = 4;
      }else if(mode == 4){
      mode = 1;
      }
      modeSwitchCounter = 0;
      }
      }
      }

      //PREPARE VALUES/BYTES FOR SENDING
      if(mode == 1) { // TIME
      digitValue = (now.hour() * 100 + now.minute()); // calculate time in 4 digits
      digit0 = digit[0] | B10000000; // turn LED1 on
      }
      if(mode == 2) { // DATE
      digitValue = (now.day() * 100 + now.month());
      digit0 = digit[0] | B01000000; // turn LED2 on
      }
      if(mode == 3) { // YEAR
      digitValue = now.year();
      digit0 = digit[0] | B00100000; // turn LED3 on
      }
      if(mode == 4) { // TEMPERATURE
      sensors_event_t event; // Prepares for the following events
      dht.temperature().getEvent(&event); // Gets the temperature data from the DHT
      digitValue = 100 * event.temperature; // Times 100 as digit4 should show ‘C’ (and digit2 ‘.’)
      digit0 = digit[0] | B00010000; // turn LED 4 on
      }
      if(mode == 5) { // HUMIDITY // displaying LED1,4 (=5)
      sensors_event_t event; // Prepares for the following events
      dht.humidity().getEvent(&event); // Gets the relative humidity data from the DHT
      digitValue = 10 * event.relative_humidity; // Times 10 as digit4 should show ‘H’
      digit0 = digit[0] | B10010000; // turn LED1,4 (=5) on
      }
      if(mode == 6) { // NOISE LEVEL
      if(noiseCounter < loopSize) { // continue display without changing values
      noiseCounter++;
      micVal = analogRead(MIC); // read value from microphone
      micMin = min(micMin, micVal); // decreases micMin if micVal is less than it
      micMax = max(micMax, micVal); // increases micMax if micVal is more than it
      }
      else { // change values
      micReading = micMax – micMin; // calculates the average levels
      digitValue = map(micReading,0,1024,0,100); // Maps micReading to a percentage

      noiseCounter = 0; // reset noise measuring loop variables
      micMax = 0;
      micMin = 1024;
      }
      }
      if(mode == 7) { // LIGHT LEVEL // dispolaying LEDs 1,2,4 (=7)
      digitValue = map(light,0,1024,5,100); // maps reading to 5-100
      digit0 = digit[0] | B11010000; // turn LED1,2,4 (=7) on
      }

      // PREPARE VALUES FOR DIGITS
      thousands = digitValue / 1000;
      hundreds = (digitValue%1000)/100;
      tens = (digitValue%100) / 10;
      ones = (digitValue%10);
      if(thousands == 0){ // if no value for digit0 switch it off
      digit0 = digit0 | B00000001;
      }
      if(thousands == 0 && hundreds == 0){ // if also no value for digit1 switch it off
      digit1 = digit1 | B00000010;
      }

      if(mode == 6){ // setting of LEDs in accordance with noiselevel in stead of mode
      if(tens == 0) {
      digit0 = digit0; // all LED off
      }
      else if(tens > 0 && tens <=2) {
      digit0 = digit0 | B10000000; // LED1 on
      }
      else if(tens > 2 && tens <= 3) {
      digit0 = digit0 | B11000000; // LED1,2 on
      }
      else if(tens > 3 && tens <= 5) {
      digit0 = digit0 | B11100000; // LED1,2,3 on
      }
      else if(tens > 5) {
      digit0 = digit0 | B11110000; // LED1,2,3,4 on
      }
      }

      // OUTPUT TO DIGITISER
      // SETTING DIGIT0
      if(mode == 7){
      digit0 ^= 1UL << 0; // switches state of digit0 (to on again, as it is supposed to be off)
      digitalWrite(LATCH, LOW);
      shiftOut(SER,CLK,LSBFIRST,letterL); // displays ‘L’ on digit0
      shiftOut(SER,CLK,LSBFIRST,digit0);
      digitalWrite(LATCH,HIGH);
      }else{
      digitalWrite(LATCH, LOW);
      shiftOut(SER,CLK,LSBFIRST,number[thousands]);
      shiftOut(SER,CLK,LSBFIRST,digit0);
      digitalWrite(LATCH,HIGH);
      }
      delay(2);

      //SETTING DIGIT1
      if(mode == 1){
      decimalPoint = number[hundreds];
      if (now.second() % 2 == 0){ // decimal point should be inserted in digit1 as in mode4, but blinking per second
      decimalPoint &= ~(1UL << 3); // clears the 3rd bit (= the decimal point on)
      } else {
      decimalPoint |= 1UL << 3; // setting the 3rd bit (= the decimal point off)
      }
      digitalWrite(LATCH, LOW);
      shiftOut(SER,CLK,LSBFIRST,decimalPoint);
      shiftOut(SER,CLK,LSBFIRST,digit1);
      digitalWrite(LATCH,HIGH);
      } else if(mode == 4){ // decimal point should be inserted in digit1
      decimalPoint = number[hundreds];
      decimalPoint &= ~(1UL << 3); // clears the 3rd bit (= the decimal point on)
      digitalWrite(LATCH, LOW);
      shiftOut(SER,CLK,LSBFIRST,decimalPoint);
      shiftOut(SER,CLK,LSBFIRST,digit1);
      digitalWrite(LATCH,HIGH);
      } else {
      digitalWrite(LATCH, LOW);
      shiftOut(SER,CLK,LSBFIRST,number[hundreds]);
      shiftOut(SER,CLK,LSBFIRST,digit1);
      digitalWrite(LATCH,HIGH);
      }
      delay(2);

      //SETTING DIGIT2
      digitalWrite(LATCH, LOW);
      shiftOut(SER,CLK,LSBFIRST,number[tens]);
      shiftOut(SER,CLK,LSBFIRST,digit2);
      digitalWrite(LATCH,HIGH);
      delay(2);

      //SETTING DIGIT3
      if(mode == 4){ // setting digit3
      digitalWrite(LATCH, LOW);
      shiftOut(SER,CLK,LSBFIRST,letterC);
      shiftOut(SER,CLK,LSBFIRST,digit3);
      digitalWrite(LATCH,HIGH);
      }else if(mode == 5){
      digitalWrite(LATCH, LOW);
      shiftOut(SER,CLK,LSBFIRST,letterH);
      shiftOut(SER,CLK,LSBFIRST,digit3);
      digitalWrite(LATCH,HIGH);
      }else{ // regular setting of digit3
      digitalWrite(LATCH, LOW);
      shiftOut(SER,CLK,LSBFIRST,number[ones]);
      shiftOut(SER,CLK,LSBFIRST,digit3);
      digitalWrite(LATCH,HIGH);
      }
      delay(2);

      }

    • #7163

      Here are a couple of tips for copying this sketch from here.  If there are any quotes(“) remove them and introduce keyboard quotes(“).  The quotes(“) from web pages are smart quotes and dont format to arduino IDE.

      Also when it complains about a stray/342, remove minus(-) sign and the spaces and then put the minus back in.  That should clean up the errors.

       

      Kevin

Viewing 1 reply thread
  • You must be logged in to reply to this topic.