本网页已闲置超过3分钟,按键盘任意键或点击空白处,即可回到网页
最热资讯


本文要介绍的是一种基本的模型火车布局,其中包含经过的侧线,火车以自动顺序运行和停止。
制作模型火车布局是一个很好的爱好,自动化它们会让它变得更好!让我们来看看它自动化的一些优势:
所以,事不宜迟,让我们开始吧!
第 1 步:观看项目
第 2 步:获取所有零件和组件
首先,请确保您具备以下所有条件:
底部带有红外接近传感器的轨道段(我使用的是 Kato S62 轨道)
第 3 步:将程序上传到 Arduino 板
如果您的计算机上没有Arduino IDE,请先下载。然后使用这一段code:
int i=0; //Integer to store the locomotive's speed at a scale from 0 to 255.
int switchLimit = 80;// Integer to store the speed limit at which the train will enter the siding.
void check_n_switch(){
if(digitalRead(A0)==HIGH){//Checking if the sensor detects the train passing the sensored track.
if(i<=switchLimit){//If the speed value is greater than the set value.
switch_to_pass();//Direct the train to the siding.
}
if(i>switchLimit){//If the speed value is less than the set value.
switch_to_main();//Direct the train to the mainline.
}
}
}
void switch_to_pass(){
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
delay(200);
digitalWrite(12,LOW);
}
void switch_to_main(){
digitalWrite(12,LOW);
digitalWrite(11,HIGH);
digitalWrite(11,HIGH);
delay(200);
digitalWrite(11,LOW);
}
void setup() {
pinMode(A0,INPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop() {
switch_to_pass();//Switching turnouts to the siding since the train will start the journey fro there.
for(i=0;i<=40;i++){//Increasing the speed of the locmotive to 40, at this speed the lights turn on but the train remains at rest.
analogWrite(9,i);
delay(10);
}
delay(1000);
for(i=40;i<=90;i++){//Increasing the speed of the locomotive to 90
analogWrite(9,i);
check_n_switch();
delay(500);
}
delay(4000);
for(i=90;i<=180;i++){//Increasing the speed of the locomotive to 180.
analogWrite(9,i);
check_n_switch();
delay(250);
}
delay(3000);
for(i=180;i!=90;i--){//Decreasing the speed of the locmotive back to 90.
analogWrite(9,i);
check_n_switch();
delay(500);
}
delay(2000);
while(digitalRead(A0)==LOW){//Wait for the train to cross the sensored track.
}
switch_to_pass();//Switch the turnouts to direct the train to the siding.
delay(2000);//Wait for the train to enter the siding.
for(i=90;i!=35;i--){//Reduce the speed of the train gradually, bringing it to a halt.
analogWrite(9,i);
check_n_switch();
delay(500);
}
for(i=35;i!=0;i--){
analogWrite(9,i);
check_n_switch();
delay(62);
}
delay(5000);//Wait for 5 seconds before repeating the whole process again.
}
第 4 步:铺设轨道并进行布局
如上图所示,用经过的壁板制作一个椭圆形环。确保传感器轨道与列车在穿过传感器轨道后要穿过的第一个道岔之间的距离大于火车的长度,这样当火车穿过道岔时,没有任何部分超过传感器轨道。
第 5 步:电路原理图
在继续之前,请确保您浏览了完整的电路原理图和所有细节。
第 6 步:将道岔连接到 L298N 驱动板的输出
将两个道岔的红线和黑线分别相互连接,形成并联。然后,将红线连接到电机驱动板的 out4 端子,将黑线连接到电机驱动板的 out3 端子。
第 7 步:将电源馈线轨道连接到 L298N 驱动板的剩余输出
将电源馈线的白线连接到电机驱动板的 out1 端子,将蓝线连接到电机驱动板的 out2 端子。
第 8 步:将 L298N 驱动板连接到 Arduino 板的电源引脚
将 12 伏引脚连接到 Arduino 板的 VIN 引脚,将 GND 引脚连接到 Arduino 板的 GND 引脚,最好将电机驱动器的 5 伏引脚连接到 Arduino 板的 5 伏引脚。
第 9 步:将传感器连接到 Arduino 板
将传感器的 VCC 引脚连接到 Arduino 板的 5V 引脚,GND 引脚连接到 Arduino 板的 GND 引脚,OUT 引脚连接到 Arduino 板的 A0 引脚。
第 10 步:将电机驱动器的输入引脚连接到 Arduino 板
将 Arduino 板的数字引脚连接到电机驱动板的输入引脚,如下所示:
第 11 步:将火车放在轨道上
检查所有接线连接后,将火车放在壁板上。
第 12 步:启动设置
启动设置并确保道岔切换到壁板,如果没有,则只需反转与电机驱动器建立的道岔的连接。此外,确保火车开始向前移动。如果列车运行方向错误,则将馈线轨道与电机驱动器的连接反向。
第13步:完成!
到这一步,该项目已经完成。您可以修改 Arduino 代码以更改布局的功能、添加更多侧板等。这一切都是可定制的!
如果您对此项目有任何想法、意见或问题,请在下方留言。
以上内容翻译自网络,原作者:Kushagra Keshari,如涉及侵权,可联系删除。
物联网全新硬件解决方案——5款好用的MCU推荐
2019-04-11
ST发布自由的Linux桌面开发程序,面向STM32微控制单元
2016-03-17
华为手环3 pro拆解:成本价为$20.34如何做到人人称赞的品质
2019-10-21
同是Cortex-M4内核超低功耗MCU:STM32L4和MSP432你选哪一个?
2015-06-04
处理器和微控制器到底有什么样的区别,这篇文章说的够清楚了
2017-07-11
ESP32入门:如何为ESP32设置Arduino IDE
2020-05-14
diy最高境界:无处不在的电子项目,7个有趣的项目发明
2019-11-13
STM32的Cube AI是什么?
2019-10-25
基于Attiny85微控制器的4个简单但有创意的项目diy
2019-11-12
工程师必须掌握的Keil5如何安装
2019-09-19
讨论