Arduino Mega project

Onderstaand is een Arduino Mega, met een 8 x 8 LED Matrix, buzzer, RGB Led in pingpong bal, LDR, TFT kleurenscherm, joystick, temperatuurmeter, klok en een 2 x  20 LCD scherm.

Onderstaande de code :

///////////////////////////////////////////////////////////////////
//                                                               //
//      TFT display, piep, LCD, RGB-LED, temperatuur, clock      //
//                    MatrixLED + LDR                            //
//                                                               //
//                      november 2016                            //
//                   (c) Tonnie Raemakers                        //
//                                                               //
//-------------------- PIN toewijzingen ------------------------ //
//                                                               //
//  - Clock aansluitingen :                                      //
//  - PIN 20 = SDA van Clock kleur draad wit                     //
//  - PIN 21 = SCL van Clock kleur draad grijs                   //
//  - PIN GND = GND - PIN VCC = +5V                              //
//  - Standaard adress DS3231 Clock is 0x57                      //
//                                                               //
//  - PIN  7 ( PWM ) = RGB LED kleur draad rood                  //
//  - PIN  6 ( PWM ) = RGB LED kleur draad groen                 //
//  - PIN  5 ( PWM ) = RGB LED kleur draad blauw                 //
//  - PIN 22 ( PWM ) = piep kleur draad oranje                   //
//  - PIN A0 kleur draad oranje. LDR één kant naar plus          //
//  - en weerstand 10 k naar massa                               //
//                                                               //
//  - LCD aansluitingen :                                        //
//  - PIN 20 = SDA van LCD kleur draad grijs                     //
//  - PIN 21 = SCL van LCD kleur draad wit                       //
//  - PIN GND = GND - PIN VCC = +5V                              //
//                                                               //
//  - LED-Matrix aansluitingen :                                 //
//  - PIN 36 = CLK van LED-Matrix kleur draad geel               //
//  - PIN 38 = CS van LED-Matrix kleur draad groen               //
//  - PIN 40 = DIN van LED-Matrix kleur draad blauw              //
//  - PIN GND = GND - PIN VCC = +5V                              //
//                                                               //
//  - TFT aansluitingen :                                        //
//  - PIN +5V = +5V en BL van TFT kleur draad rood               //
//  - PIN GND = GND van TFT                                      //
//  - PIN 50 = MISO van TFT kleur draad wit                      //
//  - PIN 52 = SCK van TFT kleur draad bruin                     //
//  - PIN 51 = MOSI van TFT kleur draad groen                    //
//  - PIN 10 ( PWM ) = LCD CS van TFT kleur draad oranje         //
//  - PIN  4 ( PWM ) = SD CS van TFT kleur draad blauw           //
//  - PIN  9 ( PWM ) = D/C van TFT kleur draad geel              //
//  - PIN  8 ( PWM ) = RESET van TFT kleur draad paars           //
//  - PIN GND = GND van TFT                                      //
//                                                               //
//  - PIN 24 = Out van DS18B20 temp sensor kleur draad groen     //
//                                                               //
//  - PIN GND = GND - PIN VCC = +5V                              //
//                                                               //
//  - Joystick aansluitingen :                                   //
//  - PIN 34 = SW kleur draad oranje                             //
//  - PIN 15 = VRY kleur draad paars                             //
//  - PIN 14 = VRX kleur draad blauw                             //
//  - PIN GND = GND - PIN VCC = +5V                              //
//                                                               //
//                                                               //
///////////////////////////////////////////////////////////////////

#define cs 10                              // TFT
#define dc 9                               // TFT
#define rst 8                              // TFT
#include <TFT.h>                           // Arduino TFT library
#include <SPI.h>                           // Arduino TFT library
#define ONE_WIRE_BUS 24                    // Tempsensor op PIN 24
#include <DallasTemperature.h>;            // Tempsensor lib
#include <Wire.h>;
#include <OneWire.h>;
#include "LedControl.h";                   // LED-Matrix 8x8 lib
LedControl lc=LedControl(40,36,38,1);      // Din,CLK,LOAD,LED-Mat
#include "Sodaq_DS3231.h";
OneWire oneWire(ONE_WIRE_BUS);             // Tempsensor onewire
DallasTemperature sensors(&oneWire);       // Tempsensor omrekenen
#include <LiquidCrystal_I2C.h>             // LCD lib
LiquidCrystal_I2C lcd ( 0x27, 16, 2 );     // Adres 0x27,16char,2regels
int BlauwPin = 5;                          // RGB-LED Rode draad
int GroenPin = 6;                          // RGB-LED Blauwe draad
int RoodPin  = 7;                          // RGB-LED Groene draad
int Teller = 0;                            // RGB-LED Teller op nul
int MatrixTeller = 0;                      // MatirxTeller op nul
int Jaar = 0;                              // Jaartal omzetten 
                                           // naar tweecijferig
int Randomnummer = 0;                      // Willekeurig nummer
int LDRWaarde = 0;                         // Zet LDR waarde op 0
int LDRsensor = A0;                        // LDR zit op PIN A0
char sensorPrintout[4];                    // ?
char weekDag[][10] = {"Zondag", "Maandag",
     "Dinsdag", "Woensdag", "Donderdag", 
     "Vrijdag", "Zaterdag"};               // Weekdagen toekennen
unsigned long delaytime = 100;             // Delay tbv LED-Matrix
int Temperatuurvorig;
int Temperatuur;
int Piep = 22;
int VRX = 14;                              // Joystick X op pin 30
int VRY = 15;                              // Joystick Y op pin 32
int ButtonWaarde = 0;
int Button = 34;                           // Button op pin 34
int JoystickXwaarde = 0;                   // Joystick X waarde
int JoystickYwaarde = 0 ;                  // Joystick Y wwarde
int JoystickTeller = 0;

///////////////////////////////////////////////////////////////////
//                        Void Setup                             //
///////////////////////////////////////////////////////////////////

void setup () {

  pinMode(Button, INPUT);                  // Button PIN 34 input
  digitalWrite(Button, HIGH);              // PIN HIGH maken
  Wire.begin();                            // T.b.v. tijd module
  rtc.begin();                             // T.b.v. tijd module
  int Temperatuur;                         // Temperatuur ophalen
  sensors.requestTemperatures();           // Temperatuur ophalen
  Temperatuur=(sensors.getTempCByIndex(0));// Temperatuur ophalen
  lcd.init();                              // Init LCD1 display
  lcd.backlight();                         // Backlight LCD1 aan
  pinMode(RoodPin, OUTPUT);                // RGB Rood als output
  pinMode(GroenPin, OUTPUT);               // RGB Groen als output
  pinMode(BlauwPin, OUTPUT);               // RGB Blauw als output
  lc.shutdown(0, false);                   // Wake-up LED-Matrix
  lc.setIntensity(0, 1);                   // Brightness medium
  lc.clearDisplay(0);                      // and clear the display
  TFT TFTscreen = TFT(cs, dc, rst);
  TFTscreen.begin();
  TFTscreen.background(0, 0, 0);           // Achtergrond (R,G,B)
  TFTscreen.stroke(0, 200, 200);           // PrintKleur (R,G,B)
  TFTscreen.setTextSize(2);
  TFTscreen.text("(c)", 2, 2);
  TFTscreen.text("by Tonnie", 20, 55);
  TFTscreen.text("Raemakers", 50, 110);

  for (int i = 0; i < 17; i++) {
    lcd.clear() ;                          // Leeg LCD display
    lcd.setCursor(16 - i, 0);              // Cursor positie (16-i)
    lcd.print("Versie 1.03");              // Print tekst
    lcd.setCursor(19 - i, 1);              // Cursor positie (19-i)
    lcd.print("(c) by Tonnie");            // Print tekst
    delay(100);
  }

  noTone(Piep);                            // Geen piep
  tone(Piep, 150, 50);                     // Piep 150 Hz 50 msec
  delay(50);                               // wacht 50 msec
  noTone(Piep);                            // Geen piep
  delay(1000);
  lcd.clear();                             // leeg LCD display
}

void loop ( ) {

///////////////////////////////////////////////////////////////////
//                      Joystick uitlezen                        //
///////////////////////////////////////////////////////////////////

  ButtonWaarde = digitalRead(Button);
  if (ButtonWaarde == 0)                   // Als Button is
                                           // ingedrukt dan
  {
    while (JoystickTeller < 100)
    {
      lcd.clear();
      JoystickXwaarde = analogRead(VRX);
      JoystickYwaarde = analogRead(VRY);
      // Waarde omzetten van 0-1023 naar 0 - 100
      JoystickXwaarde = map(JoystickXwaarde,0,1023,0,100);
      JoystickYwaarde = map(JoystickYwaarde,0,1023,100,0);
      lcd.setCursor(1, 0);
      lcd.print("Geheim menu");
      lcd.setCursor(1, 1);
      lcd.print(JoystickXwaarde);
      lcd.print('-');
      lcd.print(JoystickYwaarde);
      delay(75);
      JoystickTeller++;
    }
  }
  lcd.clear();

///////////////////////////////////////////////////////////////////
//              Klok uitlezen en printen op LCD scherm           //
///////////////////////////////////////////////////////////////////

  DateTime now = rtc.now();                // Tijd/datum ophalen
  lcd.setCursor(3, 0);
  lcd.print(weekDag[now.dayOfWeek()]);     // Print weekdag
  lcd.setCursor(0, 1);
  if (now.date() < 10)
    lcd.print('0');
  lcd.print(now.date(), DEC);              // Print datum XX-XX-XX
  lcd.print('-');
  if (now.month() < 10)
    lcd.print('0');
  lcd.print(now.month(), DEC);
  lcd.print('-');
  Jaar = now.year() - 2000;                // Jaartal 20xx -> xx
  lcd.print(Jaar);
  lcd.setCursor(11, 1);
  if (now.hour() < 10)
    lcd.print('0');
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  if (now.minute() < 10)
    lcd.print('0');
  lcd.print(now.minute(), DEC);

///////////////////////////////////////////////////////////////////
//          Temperatuur ophalen en printen op TFT scherm         //
///////////////////////////////////////////////////////////////////

  sensors.requestTemperatures();
  Temperatuur = (sensors.getTempCByIndex(0));
  if (Temperatuur != Temperatuurvorig)     // Print pas
  {                                        // temperatuur
    TFT TFTscreen = TFT(cs, dc, rst);      // anders is
    TFTscreen.background(128, 128, 128);   // Kleur R,G,B
    TFTscreen.stroke(0, 0, 255);           // PrintKleur R,G,B
    TFTscreen.setTextSize(2);              // Tekst grootte
    TFTscreen.text("Temperatuur", 17, 15); // Print Temperatuur
    TFTscreen.setTextSize(4);              // Tekst grootte
    TFTscreen.stroke(255, 0, 0);           // PrintKleur R,G,B
    if (Temperatuur  < 10) TFTscreen.text(" < 10 C", 40, 45);
    if (Temperatuur == 10) TFTscreen.text("10 C", 40, 55);
    if (Temperatuur == 11) TFTscreen.text("11 C", 40, 55);
    if (Temperatuur == 12) TFTscreen.text("12 C", 40, 55);
    if (Temperatuur == 13) TFTscreen.text("13 C", 40, 55);
    if (Temperatuur == 14) TFTscreen.text("14 C", 40, 55);
    if (Temperatuur == 15) TFTscreen.text("15 C", 40, 55);
    if (Temperatuur == 16) TFTscreen.text("16 C", 40, 55);
    if (Temperatuur == 17) TFTscreen.text("17 C", 40, 55);
    if (Temperatuur == 18) TFTscreen.text("18 C", 40, 55);
    if (Temperatuur == 19) TFTscreen.text("19 C", 40, 55);
    if (Temperatuur == 20) TFTscreen.text("20 C", 40, 55);
    if (Temperatuur == 21) TFTscreen.text("21 C", 40, 55);
    if (Temperatuur == 22) TFTscreen.text("22 C", 40, 55);
    if (Temperatuur == 23) TFTscreen.text("23 C", 40, 55);
    if (Temperatuur == 24) TFTscreen.text("24 C", 40, 55);
    if (Temperatuur == 25) TFTscreen.text("25 C", 40, 55);
    if (Temperatuur == 26) TFTscreen.text("26 C", 40, 55);
    if (Temperatuur == 27) TFTscreen.text("27 C", 40, 55);
    if (Temperatuur == 28) TFTscreen.text("28 C", 40, 55);
    if (Temperatuur == 29) TFTscreen.text("29 C", 40, 55);
    if (Temperatuur == 30) TFTscreen.text("30 C", 40, 55);
    if (Temperatuur == 31) TFTscreen.text("31 C", 40, 55);
    if (Temperatuur  > 31) TFTscreen.text("> 31 C", 40, 45);
  }
  Temperatuurvorig = Temperatuur;

///////////////////////////////////////////////////////////////////
//                            Piep                               //
///////////////////////////////////////////////////////////////////

  LDRWaarde = analogRead(LDRsensor);       // Uitlezen LDR
  if (LDRWaarde < 350)                     // Als LDR < 350 piepen
  {
    Randomnummer = random(100, 2000);      // Willekeurig getal 
                                           // tussen 100 en 2000
    noTone(Piep);                          // Geen piep op pin 22
    tone(Piep, Randomnummer, 50);          // Piep op pin 46 
                                           // random Hz / 50 mS
    delay(75);
  }

///////////////////////////////////////////////////////////////////
//                         LED-Matrix                            //
///////////////////////////////////////////////////////////////////

  byte a[6]={B00000000,B00000000,B01111100,
            B10000010,B10000010,B01111100};  // Getal 0
  byte b[6]={B00000000,B00000000,B00000000,
            B10000100,B11111110,B10000000};  // Getal 1
  byte c[6]={B00000000,B00000000,B11000100,
            B10100010,B10010010,B10001100};  // Getal 2
  byte d[6]={B00000000,B00000000,B01000100,
            B10000010,B10010010,B01101100};  // Getal 3
  byte e[6]={B00000000,B00000000,B00110000,
            B00101000,B00100100,B11111110};  // Getal 4
  byte f[6]={B00000000,B00000000,B01001110,
            B10001010,B10001010,B01110010};  // Getal 5
  byte g[6]={B00000000,B00000000,B01111100,
            B10010010,B10010010,B01100000};  // Getal 6
  byte h[8]={B00000000,B00000000,B11000010,
            B00100010,B00010010,B00001110};  // Getal 7
  byte i[8]={B00000000,B00000000,B01101100,
            B10010010,B10010010,B01101100};  // Getal 8
  byte j[6]={B00000000,B00000000,B00001100,
            B10010010,B10010010,B01111100};  // Getal 9

  if ( MatrixTeller == 0 )
  {
    lc.setRow(0, 0, a[0]);
    lc.setRow(0, 1, a[1]);
    lc.setRow(0, 2, a[2]);
    lc.setRow(0, 3, a[3]);
    lc.setRow(0, 4, a[4]);
    lc.setRow(0, 5, a[5]);
    lc.setRow(0, 6, a[6]);
    lc.setRow(0, 7, a[7]);
    lc.setRow(0, 8, a[8]);
  }
  if ( MatrixTeller == 1 )
  {
    lc.setRow(0, 0, b[0]);
    lc.setRow(0, 1, b[1]);
    lc.setRow(0, 2, b[2]);
    lc.setRow(0, 3, b[3]);
    lc.setRow(0, 4, b[4]);
    lc.setRow(0, 5, b[5]);
    lc.setRow(0, 6, b[6]);
    lc.setRow(0, 7, b[7]);
    lc.setRow(0, 8, b[8]);
  }
  if ( MatrixTeller == 2 )
  {
    lc.setRow(0, 0, c[0]);
    lc.setRow(0, 1, c[1]);
    lc.setRow(0, 2, c[2]);
    lc.setRow(0, 3, c[3]);
    lc.setRow(0, 4, c[4]);
    lc.setRow(0, 5, c[5]);
    lc.setRow(0, 6, c[6]);
    lc.setRow(0, 7, c[7]);
    lc.setRow(0, 8, c[8]);
  }
  if ( MatrixTeller == 3 )
  {
    lc.setRow(0, 0, d[0]);
    lc.setRow(0, 1, d[1]);
    lc.setRow(0, 2, d[2]);
    lc.setRow(0, 3, d[3]);
    lc.setRow(0, 4, d[4]);
    lc.setRow(0, 5, d[5]);
    lc.setRow(0, 6, d[6]);
    lc.setRow(0, 7, d[7]);
    lc.setRow(0, 8, d[8]);
  }
  if ( MatrixTeller == 4 )
  {
    lc.setRow(0, 0, e[0]);
    lc.setRow(0, 1, e[1]);
    lc.setRow(0, 2, e[2]);
    lc.setRow(0, 3, e[3]);
    lc.setRow(0, 4, e[4]);
    lc.setRow(0, 5, e[5]);
    lc.setRow(0, 6, e[6]);
    lc.setRow(0, 7, e[7]);
    lc.setRow(0, 8, e[8]);
  }
  if ( MatrixTeller == 5 )
  {
    lc.setRow(0, 0, f[0]);
    lc.setRow(0, 1, f[1]);
    lc.setRow(0, 2, f[2]);
    lc.setRow(0, 3, f[3]);
    lc.setRow(0, 4, f[4]);
    lc.setRow(0, 5, f[5]);
    lc.setRow(0, 6, f[6]);
    lc.setRow(0, 7, f[7]);
    lc.setRow(0, 8, f[8]);
  }
  if ( MatrixTeller == 6 )
  {
    lc.setRow(0, 0, g[0]);
    lc.setRow(0, 1, g[1]);
    lc.setRow(0, 2, g[2]);
    lc.setRow(0, 3, g[3]);
    lc.setRow(0, 4, g[4]);
    lc.setRow(0, 5, g[5]);
    lc.setRow(0, 6, g[6]);
    lc.setRow(0, 7, g[7]);
    lc.setRow(0, 8, g[8]);
  }
  if ( MatrixTeller == 7 )
  {
    lc.setRow(0, 0, h[0]);
    lc.setRow(0, 1, h[1]);
    lc.setRow(0, 2, h[2]);
    lc.setRow(0, 3, h[3]);
    lc.setRow(0, 4, h[4]);
    lc.setRow(0, 5, h[5]);
    lc.setRow(0, 6, h[6]);
    lc.setRow(0, 7, h[7]);
    lc.setRow(0, 8, h[8]);
  }
  if ( MatrixTeller == 8 )
  {
    lc.setRow(0, 0, i[0]);
    lc.setRow(0, 1, i[1]);
    lc.setRow(0, 2, i[2]);
    lc.setRow(0, 3, i[3]);
    lc.setRow(0, 4, i[4]);
    lc.setRow(0, 5, i[5]);
    lc.setRow(0, 6, i[6]);
    lc.setRow(0, 7, i[7]);
    lc.setRow(0, 8, i[8]);
  }
  if ( MatrixTeller == 9 )
  {
    lc.setRow(0, 0, j[0]);
    lc.setRow(0, 1, j[1]);
    lc.setRow(0, 2, j[2]);
    lc.setRow(0, 3, j[3]);
    lc.setRow(0, 4, j[4]);
    lc.setRow(0, 5, j[5]);
    lc.setRow(0, 6, j[6]);
    lc.setRow(0, 7, j[7]);
    lc.setRow(0, 8, j[8]);
  }
  MatrixTeller = MatrixTeller + 1;
  if (MatrixTeller == 10)
  {
    MatrixTeller = 0;
  }
  delay(delaytime);

///////////////////////////////////////////////////////////////////
//          Einde programma en RGB LED éénmaal aansturen         //
///////////////////////////////////////////////////////////////////

  if (Teller > 255) Teller = 0;                  // Op het eind 
  else Teller = Teller + 4;                      // steeds de
  analogWrite(RoodPin, 255 - Teller);            // drie kleuren
  analogWrite(BlauwPin, Teller - 255);           // van de RGB LED
  analogWrite(GroenPin, Teller);                 // veranderen
}