Last Updated:

Arduino Code syntax highlighter example

Sandro Bilbeisi
Sandro Bilbeisi

Arduino Code syntax highlighter example

Β 

// Author: Jure Korber
// Date: 6.2.2017

#include "DHT.h"
#include <UTFT.h>
#include <avr/pgmspace.h>
#include <DS3231_Simple.h>
#include <Wire.h>
#include "ClickButton.h"
#include "Adafruit_WS2801.h" 
#include "SPI.h"  
DS3231_Simple Clock;


// LED LIGHTS:
uint8_t dataPin  = 11; // DATA ZA LED
uint8_t clockPin = 13; // CLOCK ZA LED
int lednumb = 15; // NUMBER OF LEDS
int red = 155; // Variable for RED intensity
int blu = 155; // Variable for BLUE intensity
int grn = 155; // Variable for GREEN intensity
int l;
int stopled = 0; 
int intervalmix = 20; 
long previousMillis1; 
long currentMillis1; 
int q, w, e;
int intensity; 

// RELATIVE HUMIDITY AND TEMPERATURE:  
#define RHT1PIN 9
#define RHT2PIN 10
#define DHTTYPE DHT22 
DHT dht1(RHT1PIN, DHTTYPE);
DHT dht2(RHT2PIN, DHTTYPE);
unsigned long previousMillisRHT = 0;
const long intervalRHT = 5000; // refresh interval for RHT sensors (temp, humidity)
int readSelect = 0; 
float h1;
float h2;
float t1;
float t2;
const int relayPrintPin = 7; 
const int relayRpiPin = 8; 

enum PinAssignments {                                           // CW = count up, CCW = count down
  encoderPinA = 18,                                              // DAT
  encoderPinB = 19,                                              // CLK
  clearButton = 15                                              // BUTTON
};

volatile byte encoderPos = 0;                                   // a counter for the dial
byte lastReportedPos = 1;                                       // change management
static bool rotating = false;                                   // debounce management
int fanState = 1; 
ClickButton button1(clearButton, LOW);

const int FumePin = A0; 

// Stuff that FAN use: 
const int fanPowerPin = 4;
const int fanPWMPin = 5;
const int fanRPMPin = 3;
double Calc;
float CalcFiltered; 
int i = 0; 
unsigned long millisOld;
volatile uint32_t deltaMillisshare;
volatile uint8_t flagshare;
int power=20;

// Stuff for LCD display: 
// Declare which fonts we will be using
UTFT myGLCD(CTE32HR,38,39,40,41);
extern uint8_t arial_bold[];
extern uint8_t Arial_roundx24[];
extern uint8_t Grotesk32[];
extern uint8_t pisava[];
extern unsigned int vlaga[1564];
extern unsigned int temp[1260];
extern unsigned int fan[6561];
extern unsigned int light[4756];
extern unsigned int plug[1020];
extern unsigned int wifi[832];

// function to read 
void rpm (){
  i=i+1;
    if (i==1){
       deltaMillisshare = micros()-millisOld;
       millisOld = micros();
       flagshare = 1; 
       i=0;}}

Adafruit_WS2801 strip = Adafruit_WS2801(25, dataPin, clockPin);

void setup()
{

  myGLCD.InitLCD();
  Clock.begin();
  dht1.begin();
  dht2.begin();
  delay(50); 
  myGLCD.clrScr();
  myGLCD.fillScr(20,20,20);
  myGLCD.setColor(2, 2, 2);
  
  myGLCD.fillRect(0, 0, 479, 5); // zgoraj
  myGLCD.fillRect(0, 314, 479, 319); // spodaj
  myGLCD.fillRect(0, 0, 5, 319); // leva
  myGLCD.fillRect(474, 0, 479, 319); // desna
  myGLCD.fillRect(0, 157, 479, 162); 
  myGLCD.fillRect(237, 0, 242, 319);
  myGLCD.fillRect(0, 272, 479, 277);
  myGLCD.fillRect(0, 38, 479, 43);
  
  myGLCD.setColor(red, blu, grn);
  myGLCD.fillRect(330, 225, 440, 260);
  
  myGLCD.setColor(250, 100, 100);
  myGLCD.setBackColor(VGA_TRANSPARENT);
  myGLCD.setFont(Arial_roundx24);
  myGLCD.print("ENCLOSURE", 49, 12);
  myGLCD.print("FILAMENT", 295, 12);
  myGLCD.setColor(150, 150, 150);
  //myGLCD.print("21:49  08.02.", 260, 285);
  myGLCD.print("OFF", 55, 285);
  myGLCD.print("ON", 165, 285);
  myGLCD.setFont(pisava);
  myGLCD.print("31.2'C", 80, 50);
  myGLCD.print("40.7%", 80, 105);
  myGLCD.print("23.7'C", 320, 50);
  myGLCD.print("26.1%", 320, 105);
  myGLCD.print("87", 130, 170);
  myGLCD.printNumI(2130,115, 220);
  myGLCD.print("50%", 350, 170);
  myGLCD.drawBitmap (18, 104, 34, 45, vlaga);
  myGLCD.drawBitmap (24, 50, 28, 44, temp);
  myGLCD.drawBitmap (258, 104, 34, 45, vlaga);
  myGLCD.drawBitmap (264, 50, 28, 44, temp);
  myGLCD.drawBitmap (15, 178, 81, 80, fan);
  myGLCD.drawBitmap (250, 173, 58, 81, light);
  myGLCD.drawBitmap (15, 282, 32, 25, wifi);
  myGLCD.drawBitmap (125, 281, 34, 29, plug);

 pinMode(9, INPUT); // RHT 1
 pinMode(10, INPUT);// RHT 2
 pinMode(fanRPMPin, INPUT); // FAN
 pinMode(fanPWMPin, OUTPUT); // FAN
 pinMode(fanPowerPin, OUTPUT); // FAN
 pinMode(relayPrintPin, OUTPUT); // FAN
 pinMode(relayRpiPin, OUTPUT); // FAN
 pinMode(fanPowerPin, OUTPUT); // FAN
 digitalWrite(fanPowerPin, HIGH); // FAN
 digitalWrite(relayPrintPin, LOW); // FAN
 digitalWrite(relayRpiPin, LOW); // FAN

 button1.debounceTime   = 20;   // Debounce timer in ms
 button1.multiclickTime = 50;  // Time limit for multi clicks
 button1.longClickTime  = 1000; // time until "held-down clicks" register

  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
  pinMode(clearButton, INPUT);
 
attachInterrupt(digitalPinToInterrupt(encoderPinA), doEncoderA, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderPinB), doEncoderB, CHANGE);
 

  Serial.begin(9600);
  strip.begin();
  strip.show();

    h1 = dht1.readHumidity();
    t1 = dht1.readTemperature();
    h2 = dht2.readHumidity();
    t2 = dht2.readTemperature();
  
 attachInterrupt(1, rpm, RISING); 
}

void loop()

{
 static double deltaMillis;
 static uint8_t flag; 

  /*for (l=0; l < strip.numPixels(); l++) {
      strip.setPixelColor(l ,red , grn, blu);}
      strip.show();*/

currentMillis1 = millis();
if (currentMillis1 - previousMillis1 >= intervalmix && stopled == 0)
{
  previousMillis1 = currentMillis1;
   for (q=0; q < strip.numPixels(); q++) {
    strip.setPixelColor(q,Wheel(w));
   }
 
    w++;
   if (w > 256)
   {
    e=0; 
    }
     
  strip.show();   // write all the pixels out
}
     
noInterrupts();
     flag = flagshare; 
     if (flag == 1)
     {
      deltaMillis = deltaMillisshare; 
      flag = 0; 
     }
interrupts();
     
Calc = (30000000/deltaMillis);
Calc = constrain(Calc,0,2800);
CalcFiltered = 0.1*Calc+0.9*(int)CalcFiltered;

constrain(power,0,100);
analogWrite(fanPWMPin, map(power,0,100,70,255));

    DateTime MyDateAndTime;
    MyDateAndTime = Clock.read();
 
Serial.print (Calc,0);
Serial.print (" RPM Power: ");
Serial.print (power);
Serial.print (" Fume: ");
Serial.print (analogRead(FumePin));
Serial.print (" T1:");
Serial.print (t1,1);
Serial.print ("C H1:");
Serial.print (h1,1);
Serial.print ("% T2:");
Serial.print (t2,1);
Serial.print ("C H1:");
Serial.print (h2,1);
Serial.print ("% ");
Serial.println (digitalRead(20));

myGLCD.setBackColor(20,20,20);
myGLCD.setFont(pisava);

if (fanState==1){
digitalWrite(fanPowerPin,HIGH);
myGLCD.printNumI(power, 130, 170,2,' ');
myGLCD.print("%", 178, 170);
myGLCD.printNumI(int(CalcFiltered),115, 220,4,' ');}

if (fanState==0)
{digitalWrite(fanPowerPin,LOW);
  myGLCD.print("OFF", 130, 170);
  myGLCD.printNumI(0000,115, 220,4,' ');}

  myGLCD.setFont(Arial_roundx24);
  myGLCD.printNumI(MyDateAndTime.Hour+1,260, 285, 2,' ');
  myGLCD.print(":", 292, 285);
  myGLCD.printNumI(MyDateAndTime.Minute,308, 285,2,'0');
  myGLCD.printNumI(MyDateAndTime.Day,372, 285,2,'0');
  myGLCD.print(".", 404, 285);
  myGLCD.printNumI(MyDateAndTime.Month,420, 285,2,'0');

unsigned long currentMillis = millis();
  if (currentMillis - previousMillisRHT >= intervalRHT) {
    if (readSelect == 0)
    h1 = dht1.readHumidity();
     if (readSelect == 1)
    t1 = dht1.readTemperature();
     if (readSelect == 2)
    h2 = dht2.readHumidity();
     if (readSelect == 3)
    t2 = dht2.readTemperature();

    readSelect++; 
    
    if (readSelect == 4)
    readSelect = 0; 
    
    previousMillisRHT = currentMillis;

        
myGLCD.setBackColor(20,20,20);
myGLCD.setFont(pisava);
       myGLCD.printNumF(t1,1, 80, 50,'.',3,' ');
       myGLCD.printNumF(h1,1, 80, 105,'.',3,' ');
       myGLCD.printNumF(t2,1, 320, 50,'.',3,' ');
       myGLCD.printNumF(h2,1, 320, 105,'.',3,' ');
    } 

      rotating = true;                                              // reset the debouncer

  // Print changes
  if (lastReportedPos != encoderPos) {                          // verify if encoder position has changed
     if (lastReportedPos<encoderPos)
     {power++;}
     else {power--;}
    lastReportedPos = encoderPos;                               // update new position 
  }

  button1.Update();
 if (button1.clicks != 0)
 {if (fanState == 0)
          fanState = 1;
  else fanState = 0;}
  
}



//Input a value 0 to 255 to get a color value.
//The colours are a transition r - g -b - back to r
uint32_t Wheel(byte WheelPos)
{
  if (WheelPos < 85) {
   return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
   WheelPos -= 85;
   return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170; 
   return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  } 
}

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  myGLCD.setColor(r,b,g);
  myGLCD.fillRect(330, 225, 440, 260);
  myGLCD.setColor(150, 150, 150);
  myGLCD.setFont(pisava);
  intensity = (r+b+g)/7.65;
  myGLCD.printNumI(intensity, 326, 170,3,' ');
  
  uint32_t c;
 {
  c = (int)(r);
  c <<= 8;
  c |= (int)(g);
  c <<= 8;
  c |= (int)(b);
  return c;
  }
}

Comments