“硬币存款机”的版本间的差异
来自Labplus盛思维基百科
Tangliufeng(讨论 | 贡献) 小 |
Tangliufeng(讨论 | 贡献) |
||
第11行: | 第11行: | ||
== 使用教程 == | == 使用教程 == | ||
程序控制流程:<br/> | 程序控制流程:<br/> | ||
− | 1.通过光线传感器检测是否有硬币落下,当检测到硬币落下,计数器加1,并加数值写到EEPROM中,实现断电储存计数器。 | + | 1.通过光线传感器检测是否有硬币落下,当检测到硬币落下,计数器加1,并加数值写到EEPROM中,实现断电储存计数器。<br/> |
− | 2.当检测到光线传感器为暗时超过5秒,即将EEPROM中的计数器值清零。 | + | 2.当检测到光线传感器为暗时超过5秒,即将EEPROM中的计数器值清零。<br/> |
<br/> | <br/> | ||
=== <font size=3px>组装说明</font> === | === <font size=3px>组装说明</font> === | ||
第25行: | 第25行: | ||
!模块名称||数量 | !模块名称||数量 | ||
|- | |- | ||
− | |||x1 | + | |W1主控板||x1 |
|- | |- | ||
− | |||x1 | + | |光线传感器||x1 |
+ | |- | ||
+ | |LED灯||x1 | ||
+ | |- | ||
+ | |数码管||x1 | ||
|} | |} | ||
=== <font size=3px>电子模块连接说明</font> === | === <font size=3px>电子模块连接说明</font> === | ||
− | |||
− | |||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
! 模块 !!引脚 !! 说明 | ! 模块 !!引脚 !! 说明 | ||
|- | |- | ||
− | | | + | | 光线传感器 || W1-A0/A1接口|| |
|- | |- | ||
− | | | + | | LED灯||W1-3/11接口 || |
|- | |- | ||
+ | | 数码管||W1-I2C接口 || | ||
|} | |} | ||
=== <font size=3px>Arduino程序/图形化程序</font> === | === <font size=3px>Arduino程序/图形化程序</font> === | ||
+ | [[:File:M0005_bank_SW_V1.2_Release.rar|硬币存款机Arduino程序包下载]] | ||
<pre style="color:blue"> | <pre style="color:blue"> | ||
+ | #include "Wire.h" | ||
+ | #include "TM1650.h" | ||
+ | #include "EEPROM.h" | ||
+ | |||
+ | #define Light_int A0 | ||
+ | |||
+ | #define Led_out 3 | ||
+ | |||
+ | |||
+ | typedef union | ||
+ | { | ||
+ | uint8_t byteData[2]; | ||
+ | uint16_t intData; | ||
+ | }BYTE_INT; | ||
+ | |||
+ | BYTE_INT counct; | ||
+ | |||
+ | |||
+ | unsigned int Light_sr = 0; | ||
+ | |||
+ | |||
+ | int val,vall; | ||
+ | int16_t temp = 0; | ||
+ | TM1650 d; | ||
+ | char line[] = {"----"}; | ||
+ | unsigned long blanktime; | ||
+ | |||
+ | unsigned char TEMP; | ||
+ | |||
+ | void Display_LED(); | ||
+ | |||
+ | void hexToBCD(int16_t val); | ||
+ | char getTochar(uint8_t bcd); | ||
+ | unsigned int getTime(unsigned long ntime); | ||
+ | |||
+ | unsigned int number=0; /* 代表那个非常大的数字 */ | ||
+ | unsigned char ge,shi,bai,qian,wan; | ||
+ | int16_t DispData[5]; //数据缓冲数组 | ||
+ | int ks=0; | ||
+ | unsigned long flag=0; | ||
+ | void setup() | ||
+ | { | ||
+ | // put your setup code here, to run once: | ||
+ | Wire.begin(); // join i2c bus (address optional for master) | ||
+ | d.init(); | ||
+ | d.displayOff(); | ||
+ | |||
+ | counct.byteData[0] = EEPROM.read(1); | ||
+ | counct.byteData[1] = EEPROM.read(2); | ||
+ | |||
+ | pinMode(Light_int,INPUT); | ||
+ | pinMode(Led_out,OUTPUT); | ||
+ | analogWrite(Led_out, 60); | ||
+ | number = counct.intData; | ||
+ | hexToBCD(counct.intData); | ||
+ | d.displayString(line); | ||
+ | d.setBrightness(2); | ||
+ | d.displayOn(); | ||
+ | Serial.begin(115200); | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | Display_LED();//光存钱罐 | ||
+ | } | ||
+ | |||
+ | /********************************************************************************** | ||
+ | * | ||
+ | **********************************************************************************/ | ||
+ | void hexToBCD(int16_t val) | ||
+ | { | ||
+ | int i; | ||
+ | int16_t num = val; | ||
+ | if (num > 999) num = 999; | ||
+ | for (i=0; num > 99; i++) num -= 100; | ||
+ | if(i != 0) line[1] = getTochar(i); else line[1] = ' '; | ||
+ | for (i=0; num > 9; i++) num -= 10; | ||
+ | line[2] = getTochar(i); | ||
+ | line[3] = getTochar(num); | ||
+ | line[0] = ' '; | ||
+ | } | ||
+ | /********************************************************************************** | ||
+ | * | ||
+ | **********************************************************************************/ | ||
+ | char getTochar(uint8_t bcd) | ||
+ | { | ||
+ | char c; | ||
+ | switch (bcd) | ||
+ | { | ||
+ | case 0: | ||
+ | c = '0'; | ||
+ | break; | ||
+ | case 1: | ||
+ | c = '1'; | ||
+ | break; | ||
+ | case 2: | ||
+ | c = '2'; | ||
+ | break; | ||
+ | case 3: | ||
+ | c = '3'; | ||
+ | break; | ||
+ | case 4: | ||
+ | c = '4'; | ||
+ | break; | ||
+ | case 5: | ||
+ | c = '5'; | ||
+ | break; | ||
+ | case 6: | ||
+ | c = '6'; | ||
+ | break; | ||
+ | case 7: | ||
+ | c = '7'; | ||
+ | break; | ||
+ | case 8: | ||
+ | c = '8'; | ||
+ | break; | ||
+ | default: | ||
+ | c = '9'; | ||
+ | break; | ||
+ | } | ||
+ | return c; | ||
+ | } | ||
+ | /******************************************************************************************************** | ||
+ | *时间获取函数 | ||
+ | ********************************************************************************************************/ | ||
+ | unsigned int getTime(unsigned long ntime) | ||
+ | { | ||
+ | if(millis() < ntime) | ||
+ | { | ||
+ | return ((unsigned int)((millis() + (~ntime) + 1))); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | return ((unsigned int)((millis() - ntime))); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void Display_LED() | ||
+ | { | ||
+ | Light_sr = analogRead(Light_int);//读取光线值 | ||
+ | // Serial.println("1: ");//测试用 | ||
+ | // Serial.println(Light_sr);//测试用 | ||
+ | |||
+ | if(Light_sr<5) | ||
+ | { | ||
+ | delay(1); | ||
+ | if(analogRead(Light_int)<5) | ||
+ | { | ||
+ | blanktime = millis(); | ||
+ | while(Light_sr<30) | ||
+ | { | ||
+ | Light_sr = analogRead(Light_int); | ||
+ | if((millis()-blanktime)>=5000) | ||
+ | { | ||
+ | number = 0; | ||
+ | counct.intData = number; | ||
+ | |||
+ | EEPROM.write(1,counct.byteData[0]); | ||
+ | EEPROM.write(2,counct.byteData[1]); | ||
+ | |||
+ | hexToBCD(number); | ||
+ | d.displayString(line); | ||
+ | d.displayOn(); | ||
+ | delay(2000); | ||
+ | ks = 0; | ||
+ | break; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | ks=1; | ||
+ | } | ||
+ | // Serial.println("2: "); //测试用 | ||
+ | // Serial.println(Light_sr); //测试用 | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | if(ks) //环境变暗,即投下硬币 | ||
+ | { | ||
+ | ks = 0; | ||
+ | number++; | ||
+ | Serial.print("number:"); | ||
+ | Serial.println(number); | ||
+ | counct.intData = number; | ||
+ | |||
+ | EEPROM.write(1,counct.byteData[0]); | ||
+ | EEPROM.write(2,counct.byteData[1]); | ||
+ | |||
+ | hexToBCD(number); | ||
+ | d.displayString(line); | ||
+ | d.displayOn(); | ||
+ | } | ||
+ | } | ||
</pre> | </pre> | ||
第52行: | 第248行: | ||
!width="10%"|Version !!width="15%"| Date !! <small>新增/删除/修复</small> | !width="10%"|Version !!width="15%"| Date !! <small>新增/删除/修复</small> | ||
|- | |- | ||
− | | || || style="text-align:left"| | + | | V1.2|| || style="text-align:left"| |
|} | |} |
2018年5月28日 (一) 17:40的版本
概述
本作品可实现自动记录硬币存贮数量功能,支持断电记忆。使用时先根据制作说明将作品组装完整,然后 接上电源(9V电池或USB供电),打开主控板开关,放入硬币即可自动计数。硬币经过入口时,遮挡入口下 方光线传感器,用光线传感器遮挡次数记录放入硬币的数量;需要重置存款机时,遮挡光线传感器5秒, 数码管自动归零。DIY动手组装,锻炼动手能力,了解智能创意电子套件的使用,激发创新思维,增加学 习乐趣。
使用教程
程序控制流程:
1.通过光线传感器检测是否有硬币落下,当检测到硬币落下,计数器加1,并加数值写到EEPROM中,实现断电储存计数器。
2.当检测到光线传感器为暗时超过5秒,即将EEPROM中的计数器值清零。
组装说明
电子模块清单
模块名称 | 数量 |
---|---|
W1主控板 | x1 |
光线传感器 | x1 |
LED灯 | x1 |
数码管 | x1 |
电子模块连接说明
模块 | 引脚 | 说明 |
---|---|---|
光线传感器 | W1-A0/A1接口 | |
LED灯 | W1-3/11接口 | |
数码管 | W1-I2C接口 |
Arduino程序/图形化程序
#include "Wire.h" #include "TM1650.h" #include "EEPROM.h" #define Light_int A0 #define Led_out 3 typedef union { uint8_t byteData[2]; uint16_t intData; }BYTE_INT; BYTE_INT counct; unsigned int Light_sr = 0; int val,vall; int16_t temp = 0; TM1650 d; char line[] = {"----"}; unsigned long blanktime; unsigned char TEMP; void Display_LED(); void hexToBCD(int16_t val); char getTochar(uint8_t bcd); unsigned int getTime(unsigned long ntime); unsigned int number=0; /* 代表那个非常大的数字 */ unsigned char ge,shi,bai,qian,wan; int16_t DispData[5]; //数据缓冲数组 int ks=0; unsigned long flag=0; void setup() { // put your setup code here, to run once: Wire.begin(); // join i2c bus (address optional for master) d.init(); d.displayOff(); counct.byteData[0] = EEPROM.read(1); counct.byteData[1] = EEPROM.read(2); pinMode(Light_int,INPUT); pinMode(Led_out,OUTPUT); analogWrite(Led_out, 60); number = counct.intData; hexToBCD(counct.intData); d.displayString(line); d.setBrightness(2); d.displayOn(); Serial.begin(115200); } void loop() { Display_LED();//光存钱罐 } /********************************************************************************** * **********************************************************************************/ void hexToBCD(int16_t val) { int i; int16_t num = val; if (num > 999) num = 999; for (i=0; num > 99; i++) num -= 100; if(i != 0) line[1] = getTochar(i); else line[1] = ' '; for (i=0; num > 9; i++) num -= 10; line[2] = getTochar(i); line[3] = getTochar(num); line[0] = ' '; } /********************************************************************************** * **********************************************************************************/ char getTochar(uint8_t bcd) { char c; switch (bcd) { case 0: c = '0'; break; case 1: c = '1'; break; case 2: c = '2'; break; case 3: c = '3'; break; case 4: c = '4'; break; case 5: c = '5'; break; case 6: c = '6'; break; case 7: c = '7'; break; case 8: c = '8'; break; default: c = '9'; break; } return c; } /******************************************************************************************************** *时间获取函数 ********************************************************************************************************/ unsigned int getTime(unsigned long ntime) { if(millis() < ntime) { return ((unsigned int)((millis() + (~ntime) + 1))); } else { return ((unsigned int)((millis() - ntime))); } } void Display_LED() { Light_sr = analogRead(Light_int);//读取光线值 // Serial.println("1: ");//测试用 // Serial.println(Light_sr);//测试用 if(Light_sr<5) { delay(1); if(analogRead(Light_int)<5) { blanktime = millis(); while(Light_sr<30) { Light_sr = analogRead(Light_int); if((millis()-blanktime)>=5000) { number = 0; counct.intData = number; EEPROM.write(1,counct.byteData[0]); EEPROM.write(2,counct.byteData[1]); hexToBCD(number); d.displayString(line); d.displayOn(); delay(2000); ks = 0; break; } else { ks=1; } // Serial.println("2: "); //测试用 // Serial.println(Light_sr); //测试用 } } } if(ks) //环境变暗,即投下硬币 { ks = 0; number++; Serial.print("number:"); Serial.println(number); counct.intData = number; EEPROM.write(1,counct.byteData[0]); EEPROM.write(2,counct.byteData[1]); hexToBCD(number); d.displayString(line); d.displayOn(); } }
FAQ
版本历史记录
Version | Date | 新增/删除/修复 |
---|---|---|
V1.2 |