DR HACK

itafsqarhyazeubebgcazh-CNzh-TWhrcsdanlenettlfifrglkadeelhtiwhihuisidgajakolvltmkmsmtnofaplptrorusrskslesswsvthtrukurvicyyi

Giorno virtuale v.2 per presepe

Questa è una variante del progetto Giorno virtuale, è solo stato aggiunto un display lcd 8x2..

 

gv01

 

 Sketch Arduino :

#include <TimerOne.h>
#include <LiquidCrystal.h>

//switch
#define switchPin A0

//config
#define incre 1000 // 500 centesimi = 5 sec | 6000 centesimi = 60 sec = 1 min
//incre max 16843009 centesimi massimi = 16843 sec = 280 min = 4 ore
//(duti max) * (incre max) = 4294967295 centesimi = 42949672 = sec = 715827 min = 11930 ore = 497 giorni = 16 mesi
#define timertick 20000 // 250000 = 25 centesimi = 0.25 sec 
#define inccen 2 // = timertick / 10000
#define fine 3 // minimo 2
#define stagione 254 //da 1 a 255

//mosfet
#define sole 11

//LCD
#define LCD_RS 2
#define LCD_Enable 3
#define LCD_D4 7
#define LCD_D5 4
#define LCD_D6 6
#define LCD_D7 5

String LCD_R1 = "        ";
String LCD_R2 = "        ";

volatile unsigned long mise = 0; // centesimi di secondo
byte duti = 0; // da 0 a 255
boolean onof = false;
boolean btst = false;
boolean hilo = false;

long luce = 0;

LiquidCrystal lcd(LCD_RS, LCD_Enable, LCD_D4, LCD_D5, LCD_D6, LCD_D7);

void setup() {

	pinMode(switchPin, INPUT);
	pinMode(sole, OUTPUT);

	byte font[8];
	
	font[0] = 0b00100;
	font[1] = 0b10101; 
	font[2] = 0b01010;
	font[3] = 0b10001; 
	font[4] = 0b01010; 
	font[5] = 0b10101; 
	font[6] = 0b00100; 
	font[7] = 0b00000;

	lcd.createChar(0, font);

	font[0] = 0b00001;
	font[1] = 0b10000; 
	font[2] = 0b00110;
	font[3] = 0b01100; 
	font[4] = 0b01100; 
	font[5] = 0b00110; 
	font[6] = 0b10000; 
	font[7] = 0b00010;

	lcd.createChar(1, font);

	lcd.begin(8, 2);
	lcd.clear();

	Timer1.initialize(timertick);
	Timer1.attachInterrupt(Tick);

}

void Tick(){

	uint8_t img = 2;

	mise += inccen;

	if (onof == true) {
		if (duti == 0) {
			LCD_R1 = " SEMPRE ";
			LCD_R2 = " GIORNO ";
			analogWrite(sole, stagione);
		} else {
			if ((mise % (incre * (unsigned long)duti)) == 0 ) {
				hilo = !hilo;
				mise = 0;
			}

			LCD_R1 = "";
			for (int i=0; i<map(mise, 0, (incre * (unsigned long)duti), 0, 8); i++){
				LCD_R1 += " ";
			}

			if (hilo == true) {
				img = 0;
				unsigned long x = ((incre * (unsigned long)duti)/fine);
				
				if (mise < x) {
				    luce = map(mise, 0, x, 0, stagione); //luce = (mise * stagione) / x; 
				} else if ((mise >= x) && (mise <= (x*(fine-1)))) {
					luce = stagione; 
				} else if (mise > (x*(fine-1))) {
				    luce = (stagione - map(mise, (x*(fine-1)), (incre * (unsigned long)duti), 0, stagione)); //luce = (((x*fine)-mise) * stagione) / x; 
				}

				analogWrite(sole, constrain(luce, 0, stagione));
				
			} else {
				img = 1;
				analogWrite(sole, LOW);
			}
		}

	} else {
		LCD_R1 = "TANTI   ";
		LCD_R2 = "  AUGURI";
		digitalWrite(sole, LOW);
	}

	if ((mise % (inccen * 10))  == 0) {
		lcd.clear();
		lcd.setCursor(0,0);
		lcd.print(LCD_R1);
		if (img < 2) {lcd.write(img);}
		lcd.setCursor(0,1);
		lcd.print(LCD_R2);
	}
}

void RstSta() {
	btst = true;
	mise = 0;
	hilo = true;
}

void PrtTime(){

	int secondi = (incre * duti)/100;
	int minuti = secondi / 60;

	secondi = secondi % 60;
	minuti = minuti % 60;

	if (minuti < 10) {LCD_R2 = " 0";} else {LCD_R2 = "  ";}	
	LCD_R2 += minuti; 
	if (secondi < 10) {LCD_R2 += ":0";} else {LCD_R2 += ":";}
	LCD_R2 += secondi;

}

void loop() {
  
	switch(int(analogRead(switchPin)/10)){
		case (99):
			if ((btst == false) && (onof == true)) {
				RstSta();
				duti++;
				PrtTime();
			}
		break;
		case (100):
			if (btst == false) {
				onof = !onof;
				RstSta();
				duti = 0;
			}
		break;
		case (101):
			if ((btst == false) && (onof == true)) {
				RstSta();
				duti--;
				PrtTime();
			}
		break;
		case (0):
			btst = false;
		break;
	}

}

Schema elettrico :

Galleria :

Aggiungi commento


Codice di sicurezza
Aggiorna

We use cookies to improve our website and your experience when using it. Cookies used for the essential operation of the site have already been set. To find out more about the cookies we use and how to delete them, see our privacy policy.

I accept cookies from this site.

EU Cookie Directive Module Information