arduino设计数字钟,求大佬指导
有没有大佬来帮我看看到底哪错了???编译没问题,可是屏幕什么都出不来
#include "U8glib.h"
//U8GLIB_MINI12864 u8g(10, 9, 8);
///////////////////
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);// I2C / TWI
// draw函数用于包含实现显示内容的语句
//变量定义
int xingqi=0;
struct t{
int shi=0;
int fen=0;
int miao=0;
int year=2019;
int month=1;
int day=1;
char a;
}now_time,clock1,clock2;
//////////////////////////////////////////////////////////////
void draw()
{
// 设置字体
u8g.setFont(u8g_font_unifont);
// 设置文字及其显示位置
u8g.drawStr( 0, 10, "time:");
//显示界面
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(30, 10);
u8g.print(now_time.shi);
u8g.setPrintPos(50, 10);
u8g.print(now_time.fen);
u8g.setPrintPos(80, 10);
u8g.print(now_time.miao);
u8g.setFont(u8g_font_unifont);
u8g.drawStr( 0, 30, "date:");
u8g.setPrintPos(25,30);
u8g.print(now_time.year);
u8g.setPrintPos(50, 30);
u8g.print(now_time.month);
u8g.setPrintPos(80, 30);
u8g.print(now_time.day);
u8g.setFont(u8g_font_unifont);
//闹钟显示
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(30, 65);
u8g.print(clock1.shi);
u8g.setPrintPos(50, 65);
u8g.print(clock1.fen);
u8g.setPrintPos(80, 65);
u8g.print(clock1.miao);
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(30, 75);
u8g.print(clock2.shi);
u8g.setPrintPos(50, 75);
u8g.print(clock2.fen);
u8g.setPrintPos(80, 75);
u8g.print(clock2.miao);
}
/*//#define REMOTE
#if defined(REMOTE)
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
#endif*/
////////////////////////////////////////////////////
//按键定义
#define CHOOSE 9
#define ADD 7
#define REDUCE 8
#define ledPin1 4
#define ledPin2 5
#define ledPin3 6
#define A 2
#define B 3
///////////////////////////////////////////////////////////
//初始化函数
void setup(void) {
//////////////////
// flip screen, if required
// u8g.setRot180();
// set SPI backup if required
//u8g.setHardwareBackup(u8g_backup_avr_spi);
// assign default color value指定默认颜色值
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
/////////////////////
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(CHOOSE, INPUT);
pinMode(ADD, INPUT);
pinMode(REDUCE, INPUT);
pinMode(A, INPUT);
pinMode(B, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
#if defined(REMOTE)
irrecv.enableIRIn(); // Start the receiver
#endif
draw();//显示界面
delay(1500);
}
/////////////////////////////////////////////////////////////
//时钟正常运行时
void zhengchang()
{
while (1)
{
delay(1000);
now_time.miao++;
if (now_time.miao==60)
{
now_time.miao=0;
now_time.fen++;
if (now_time.fen==60)
{
now_time.fen=0;
now_time.shi++;
if (now_time.shi==24)
{
now_time.shi=0;
now_time.day++;
if (now_time.month==4||now_time.month==6||now_time.month==9||now_time.month==11)
{
if (now_time.day==31)
{
now_time.day=1;
now_time.month++;
if (now_time.month==13)
{
now_time.month=1;
now_time.year++;
}
}
}
else if (now_time.month==1||now_time.month==3||now_time.month==5||now_time.month==7||now_time.month==8||now_time.month==10||now_time.month==12)
{
if (now_time.day==32)
{
now_time.day=1;
now_time.month++;
if (now_time.month==13)
{
now_time.month=1;
now_time.year++;
}
}
}
else if (now_time.month==2)
{
if ((now_time.year%4==0&&now_time.year%100!=0)||now_time.year%400==0)//计算该年为闰年还是平年
{
if (now_time.day==30)
{
now_time.day=1;
now_time.month++;
}
}
else
{
if (now_time.day==29)
{
now_time.day=1;
now_time.month++;
}
}
}
}
}
}
}
}
/////////////////////////////////////////////////////////////////
//调整界面
void tiaozheng()
{
int number1=0;
int flag=1;
while(flag)
{
switch(read_key())
{
case CHOOSE:number1++;break;
default:flag=0;
}
}
while(!flag)
{
switch(number1)
{
case 1:
switch(read_key())
{
case ADD:now_time.shi++;break;
case REDUCE:now_time.shi--;break;
case CHOOSE:number1++;break;
}break;
case 2:
switch(read_key())
{
case ADD:now_time.fen++;break;
case REDUCE:now_time.fen--;break;
case CHOOSE:number1++;break;
}break;
case 3:
switch(read_key())
{
case ADD:now_time.miao++;break;
case REDUCE:now_time.miao--;break;
case CHOOSE:number1++;break;
}break;
case 4:
switch(read_key())
{
case ADD:now_time.year++;break;
case REDUCE:now_time.year--;break;
case CHOOSE:number1++;break;
}break;
case 5:
switch(read_key())
{
case ADD:now_time.month++;break;
case REDUCE:now_time.month--;break;
case CHOOSE:number1++;break;
}break;
case 6:
switch(read_key())
{
case ADD:now_time.day++;break;
case REDUCE:now_time.day--;break;
case CHOOSE:number1++;break;
}break;
case 7:
switch(read_key())
{
case ADD:clock1.shi++;break;
case REDUCE:clock1.shi--;break;
case CHOOSE:number1++;break;
}break;
case 8:
switch(read_key())
{
case ADD:clock1.fen++;break;
case REDUCE:clock1.fen--;break;
case CHOOSE:number1++;break;
}break;
case 9:
switch(read_key())
{
case ADD:clock1.miao++;break;
case REDUCE:clock1.miao--;break;
case CHOOSE:number1++;break;
}break;
case 10:
switch(read_key())
{
case ADD:clock2.shi++;break;
case REDUCE:clock2.shi--;break;
case CHOOSE:number1++;break;
}break;
case 11:
switch(read_key())
{
case ADD:clock2.fen++;break;
case REDUCE:clock2.fen--;break;
case CHOOSE:number1++;break;
}break;
case 12:
switch(read_key())
{
case ADD:clock2.miao++;break;
case REDUCE:clock2.miao--;break;
case CHOOSE:number1++;break;
}break;
case 13:flag=1;break;
}
}
}
////////////////////////////////////////////////////////////////
//闹钟界面
void naozhong()
{
if(now_time.shi==clock1.shi&&now_time.fen==clock1.fen&&now_time.miao==clock1.miao)
//灯亮,怎么亮来着?
digitalWrite(ledPin1,HIGH);
delay(3000);
digitalWrite(ledPin1,LOW);
if(now_time.shi==clock2.shi&&now_time.fen==clock2.fen&&now_time.miao==clock2.miao)
digitalWrite(ledPin2,HIGH);
delay(3000);
digitalWrite(ledPin2,LOW);
//整点报时
if(now_time.fen==0&&now_time.miao==0)
digitalWrite(ledPin3,HIGH);
delay(4000);
digitalWrite(ledPin3,LOW);
}
/////////////////////////////////////////////////////////////////
#if defined(REMOTE)
int read_key()
{
int key_temp;
if (irrecv.decode(&results)) {
switch (results.value) {
case 0xFD807F: Serial.println("CHOOSE"); irrecv.resume(); return CHOOSE; break;
case 0xFD906F: Serial.println("ADD"); irrecv.resume(); return ADD; break;
case 0xFD20DF: Serial.println("REDUCE"); irrecv.resume(); return REDUCE; break;
case 0xFD08F7: Serial.println("A"); irrecv.resume(); return A; break;
case 0xFD8877: Serial.println("B"); irrecv.resume(); return B; break;
default: irrecv.resume(); return 0; break;
} // Receive the next value
}
else {
return 0;
}
}
#else
/////////////////////////////////////////////////////////////////////
//计算星期
void week()
{
int t=now_time.month+2;
int y=now_time.year%100;
int w;
if(now_time.month==1||now_time.month==2)
{
t=now_time.month+12;
y++;
}
w=y+y/4+now_time.year/100/4-2*(now_time.year/100)+26*(t+1)/10+now_time.day-1;
xingqi=w%7;
}
//////////////////////////////////////////////////////////////////////
//读取按键值
int read_key()
{
int key_temp;
if (digitalRead(CHOOSE) == LOW) {
key_temp = CHOOSE;
return key_temp;
}
if (digitalRead(ADD) == LOW) {
key_temp = ADD;
return key_temp;
}
if (digitalRead(REDUCE) == LOW) {
key_temp = REDUCE;
return key_temp;
}
if (digitalRead(A) == LOW) {
key_temp = A;
return key_temp;
}
if (digitalRead(B) == LOW) {
key_temp = B;
return key_temp;
}
return 0;
}
#endif
///////////////////////////////////////////////////////
//主函数
int main()
{
zhengchang();
draw();
naozhong();
tiaozheng();
return 0;
}
void loop() {
// put your main code here, to run repeatedly:
// u8glib图片循环结构:
u8g.firstPage();
do {
draw();
}
while( u8g.nextPage() );
// 等待一定时间后重绘
delay(50);
}
换个显屏试试 gada888 发表于 2019-2-26 08:39
换个显屏试试
显示屏没问题啊,测试过了(贪吃蛇都能运行)
页:
[1]