Здравствуйте!
В этой статье вы найдете код программы, который использовался в шестой серии сериала «Не спали свою Ардуинку», а так же схему подключения TFT дисплея 1.8 160*128 пикселей, к Arduino Mega 2560.
Суть шестой серии заключается в том, чтобы показать пример, как создать вот такие индикаторы для TFT дисплея. Который может пригодиться для индикации состояния GSM сети, а также для индикации уровня заряда батареи.
Код программы, который содержит две функции. Они позволяют выводить на TFT дисплей, анимацию индикатора уровня сигнала GSM сети, а также анимацию индикатора уровня заряда батареи.
//Начало скетча
#include "TFT.h"
#include "SPI.h"
TFT TFTscreen = TFT(53, 49, 50);//CS, DC, RESET
int buf = 6;
void antena(int x_pixel, int y_pixel, int signal){
if(buf != signal){
buf = signal;
TFTscreen.stroke(255, 255, 255);
TFTscreen.line(x_pixel, y_pixel, x_pixel+9, y_pixel);
TFTscreen.line(x_pixel+4, y_pixel+1, x_pixel+4, y_pixel+10);
TFTscreen.line(x_pixel+1, y_pixel+1, x_pixel+3, y_pixel+3);
TFTscreen.line(x_pixel+5, y_pixel+3, x_pixel+7, y_pixel+1);
if(signal == 0){
TFTscreen.stroke(0, 0, 0);
TFTscreen.line(x_pixel+6, y_pixel+8, x_pixel+6, y_pixel+10);
TFTscreen.line(x_pixel+8, y_pixel+6, x_pixel+8, y_pixel+10);
TFTscreen.line(x_pixel+10, y_pixel+4, x_pixel+10, y_pixel+10);
TFTscreen.line(x_pixel+12, y_pixel+2, x_pixel+12, y_pixel+10);
TFTscreen.line(x_pixel+14, y_pixel, x_pixel+14, y_pixel+10);
}
if(signal == 1){
TFTscreen.line(x_pixel+6, y_pixel+8, x_pixel+6, y_pixel+10);
TFTscreen.stroke(0, 0, 0);
TFTscreen.line(x_pixel+8, y_pixel+6, x_pixel+8, y_pixel+10);
TFTscreen.line(x_pixel+10, y_pixel+4, x_pixel+10, y_pixel+10);
TFTscreen.line(x_pixel+12, y_pixel+2, x_pixel+12, y_pixel+10);
TFTscreen.line(x_pixel+14, y_pixel, x_pixel+14, y_pixel+10);
}
if(signal == 2){
TFTscreen.line(x_pixel+6, y_pixel+8, x_pixel+6, y_pixel+10);
TFTscreen.line(x_pixel+8, y_pixel+6, x_pixel+8, y_pixel+10);
TFTscreen.stroke(0, 0, 0);
TFTscreen.line(x_pixel+10, y_pixel+4, x_pixel+10, y_pixel+10);
TFTscreen.line(x_pixel+12, y_pixel+2, x_pixel+12, y_pixel+10);
TFTscreen.line(x_pixel+14, y_pixel, x_pixel+14, y_pixel+10);
}
if(signal == 3){
TFTscreen.line(x_pixel+6, y_pixel+8, x_pixel+6, y_pixel+10);
TFTscreen.line(x_pixel+8, y_pixel+6, x_pixel+8, y_pixel+10);
TFTscreen.line(x_pixel+10, y_pixel+4, x_pixel+10, y_pixel+10);
TFTscreen.stroke(0, 0, 0);
TFTscreen.line(x_pixel+12, y_pixel+2, x_pixel+12, y_pixel+10);
TFTscreen.line(x_pixel+14, y_pixel, x_pixel+14, y_pixel+10);
}
if(signal == 4){
TFTscreen.line(x_pixel+6, y_pixel+8, x_pixel+6, y_pixel+10);
TFTscreen.line(x_pixel+8, y_pixel+6, x_pixel+8, y_pixel+10);
TFTscreen.line(x_pixel+10, y_pixel+4, x_pixel+10, y_pixel+10);
TFTscreen.line(x_pixel+12, y_pixel+2, x_pixel+12, y_pixel+10);
TFTscreen.stroke(0, 0, 0);
TFTscreen.line(x_pixel+14, y_pixel, x_pixel+14, y_pixel+10);
}
if(signal >= 5){
TFTscreen.line(x_pixel+6, y_pixel+8, x_pixel+6, y_pixel+10);
TFTscreen.line(x_pixel+8, y_pixel+6, x_pixel+8, y_pixel+10);
TFTscreen.line(x_pixel+10, y_pixel+4, x_pixel+10, y_pixel+10);
TFTscreen.line(x_pixel+12, y_pixel+2, x_pixel+12, y_pixel+10);
TFTscreen.line(x_pixel+14, y_pixel, x_pixel+14, y_pixel+10);
}
}
}
int buf_bat = 6;
void battery(int x_pixel, int y_pixel, int signal){
if(buf_bat != signal){
buf_bat = signal;
TFTscreen.stroke(255, 255, 255);
TFTscreen.rect(x_pixel+1, y_pixel, 12, 9);
TFTscreen.line(x_pixel, y_pixel+2, x_pixel, y_pixel+7);
if(signal == 0){
TFTscreen.stroke(0, 0, 0);
TFTscreen.line(x_pixel+11, y_pixel+1, x_pixel+11, y_pixel+8);
TFTscreen.line(x_pixel+10, y_pixel+1, x_pixel+10, y_pixel+8);
TFTscreen.line(x_pixel+9, y_pixel+1, x_pixel+9, y_pixel+8);
TFTscreen.line(x_pixel+8, y_pixel+1, x_pixel+8, y_pixel+8);
TFTscreen.line(x_pixel+7, y_pixel+1, x_pixel+7, y_pixel+8);
TFTscreen.line(x_pixel+6, y_pixel+1, x_pixel+6, y_pixel+8);
TFTscreen.line(x_pixel+5, y_pixel+1, x_pixel+5, y_pixel+8);
TFTscreen.line(x_pixel+4, y_pixel+1, x_pixel+4, y_pixel+8);
TFTscreen.line(x_pixel+3, y_pixel+1, x_pixel+3, y_pixel+8);
TFTscreen.line(x_pixel+2, y_pixel+1, x_pixel+2, y_pixel+8);
}
if(signal == 1){
TFTscreen.line(x_pixel+11, y_pixel+1, x_pixel+11, y_pixel+8);
TFTscreen.line(x_pixel+10, y_pixel+1, x_pixel+10, y_pixel+8);
TFTscreen.stroke(0, 0, 0);
TFTscreen.line(x_pixel+9, y_pixel+1, x_pixel+9, y_pixel+8);
TFTscreen.line(x_pixel+8, y_pixel+1, x_pixel+8, y_pixel+8);
TFTscreen.line(x_pixel+7, y_pixel+1, x_pixel+7, y_pixel+8);
TFTscreen.line(x_pixel+6, y_pixel+1, x_pixel+6, y_pixel+8);
TFTscreen.line(x_pixel+5, y_pixel+1, x_pixel+5, y_pixel+8);
TFTscreen.line(x_pixel+4, y_pixel+1, x_pixel+4, y_pixel+8);
TFTscreen.line(x_pixel+3, y_pixel+1, x_pixel+3, y_pixel+8);
TFTscreen.line(x_pixel+2, y_pixel+1, x_pixel+2, y_pixel+8);
}
if(signal == 2){
TFTscreen.line(x_pixel+11, y_pixel+1, x_pixel+11, y_pixel+8);
TFTscreen.line(x_pixel+10, y_pixel+1, x_pixel+10, y_pixel+8);
TFTscreen.line(x_pixel+9, y_pixel+1, x_pixel+9, y_pixel+8);
TFTscreen.line(x_pixel+8, y_pixel+1, x_pixel+8, y_pixel+8);
TFTscreen.stroke(0, 0, 0);
TFTscreen.line(x_pixel+7, y_pixel+1, x_pixel+7, y_pixel+8);
TFTscreen.line(x_pixel+6, y_pixel+1, x_pixel+6, y_pixel+8);
TFTscreen.line(x_pixel+5, y_pixel+1, x_pixel+5, y_pixel+8);
TFTscreen.line(x_pixel+4, y_pixel+1, x_pixel+4, y_pixel+8);
TFTscreen.line(x_pixel+3, y_pixel+1, x_pixel+3, y_pixel+8);
TFTscreen.line(x_pixel+2, y_pixel+1, x_pixel+2, y_pixel+8);
}
if(signal == 3){
TFTscreen.line(x_pixel+11, y_pixel+1, x_pixel+11, y_pixel+8);
TFTscreen.line(x_pixel+10, y_pixel+1, x_pixel+10, y_pixel+8);
TFTscreen.line(x_pixel+9, y_pixel+1, x_pixel+9, y_pixel+8);
TFTscreen.line(x_pixel+8, y_pixel+1, x_pixel+8, y_pixel+8);
TFTscreen.line(x_pixel+7, y_pixel+1, x_pixel+7, y_pixel+8);
TFTscreen.line(x_pixel+6, y_pixel+1, x_pixel+6, y_pixel+8);
TFTscreen.stroke(0, 0, 0);
TFTscreen.line(x_pixel+5, y_pixel+1, x_pixel+5, y_pixel+8);
TFTscreen.line(x_pixel+4, y_pixel+1, x_pixel+4, y_pixel+8);
TFTscreen.line(x_pixel+3, y_pixel+1, x_pixel+3, y_pixel+8);
TFTscreen.line(x_pixel+2, y_pixel+1, x_pixel+2, y_pixel+8);
}
if(signal == 4){
TFTscreen.line(x_pixel+11, y_pixel+1, x_pixel+11, y_pixel+8);
TFTscreen.line(x_pixel+10, y_pixel+1, x_pixel+10, y_pixel+8);
TFTscreen.line(x_pixel+9, y_pixel+1, x_pixel+9, y_pixel+8);
TFTscreen.line(x_pixel+8, y_pixel+1, x_pixel+8, y_pixel+8);
TFTscreen.line(x_pixel+7, y_pixel+1, x_pixel+7, y_pixel+8);
TFTscreen.line(x_pixel+6, y_pixel+1, x_pixel+6, y_pixel+8);
TFTscreen.line(x_pixel+5, y_pixel+1, x_pixel+5, y_pixel+8);
TFTscreen.line(x_pixel+4, y_pixel+1, x_pixel+4, y_pixel+8);
TFTscreen.stroke(0, 0, 0);
TFTscreen.line(x_pixel+3, y_pixel+1, x_pixel+3, y_pixel+8);
TFTscreen.line(x_pixel+2, y_pixel+1, x_pixel+2, y_pixel+8);
}
if(signal == 5){
TFTscreen.line(x_pixel+11, y_pixel+1, x_pixel+11, y_pixel+8);
TFTscreen.line(x_pixel+10, y_pixel+1, x_pixel+10, y_pixel+8);
TFTscreen.line(x_pixel+9, y_pixel+1, x_pixel+9, y_pixel+8);
TFTscreen.line(x_pixel+8, y_pixel+1, x_pixel+8, y_pixel+8);
TFTscreen.line(x_pixel+7, y_pixel+1, x_pixel+7, y_pixel+8);
TFTscreen.line(x_pixel+6, y_pixel+1, x_pixel+6, y_pixel+8);
TFTscreen.line(x_pixel+5, y_pixel+1, x_pixel+5, y_pixel+8);
TFTscreen.line(x_pixel+4, y_pixel+1, x_pixel+4, y_pixel+8);
TFTscreen.line(x_pixel+3, y_pixel+1, x_pixel+3, y_pixel+8);
TFTscreen.line(x_pixel+2, y_pixel+1, x_pixel+2, y_pixel+8);
}
}
}
void setup()
{
TFTscreen.begin();
TFTscreen.background(0,0,0);
}
void loop() {
antena(3, 3, random(0,5));
battery(145, 4, random(0,5));
/*
int pot_0 = analogRead(A0);
int p0 = map (pot_0, 0, 1023, 0, 5);
antena(3,3,p0);
int pot_1 = analogRead(A1);
int p1 = map (pot_1, 0, 1023, 0, 5);
battery(145, 4, p1);*/
delay(2000);
}
//Конец скетча
А это схема подключения TFT дисплея 1.8 160х128 пикселей, к Arduino Mega 2560.