C 需要帮助!让我的Arduino代码工作
我的arduino代码有一些问题,我需要帮助使其工作,它将被用来控制一些LED灯。点亮、变暗、眨眼,让他们奔跑 我在整个脚本中添加了注释,以便您可以阅读问题等等C 需要帮助!让我的Arduino代码工作,c,arduino,C,Arduino,我的arduino代码有一些问题,我需要帮助使其工作,它将被用来控制一些LED灯。点亮、变暗、眨眼,让他们奔跑 我在整个脚本中添加了注释,以便您可以阅读问题等等 /*This script is supposed to make a arduino board control the lights on a lego creation for a school project. Button1 should make lights brighter Button2 should make
/*This script is supposed to make a arduino board control the lights on a lego creation for a school project.
Button1 should make lights brighter
Button2 should make ligts shine less
Button3 should make the light blink
Button4 is supposed to make the light run after each other.
*/
//Setting up all the lights and buttons
const int lights[] = {9, 8, 7};
int light = 0;
const int button1 = 24;
int buttonstate1 = 0;
const int button2 = 25;
int buttonstate2 = 0;
const int button3 = 26;
int buttonstate3 = 0;
const int button4 = 27;
int buttonstate4 = 0;
int lightStrength = 50;
int blinkToggle = 0;
//Setting up the input's and output's
void setup() {
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(lights[0], OUTPUT);
pinMode(lights[1], OUTPUT);
pinMode(lights[2], OUTPUT);
//Here i have a problem with make the list take all in one line of code.
analogWrite(lights[0], lightStrength);
analogWrite(lights[1], lightStrength);
analogWrite(lights[2], lightStrength);
}
void loop() {
//checking if the buttons are being pressed
buttonstate1 = digitalRead(button1);
buttonstate2 = digitalRead(button2);
buttonstate3 = digitalRead(button3);
buttonstate1 = digitalRead(button4);
//Make the lights brighter
while (lightStrength <= 235){
if (buttonstate1 == HIGH){
delay(10);
lightStrength = lightStrength + 1;
delay(10);
analogWrite(lights[0], lightStrength);
analogWrite(lights[1], lightStrength);
analogWrite(lights[2], lightStrength);
}
}
//make the lights darker
while (lightStrength >= 45){
if (buttonstate2 == HIGH){
delay(5);
lightStrength = lightStrength - 1;
delay(5);
analogWrite(lights[0], lightStrength);
analogWrite(lights[1], lightStrength);
analogWrite(lights[2], lightStrength);
}
}
//Blink the lights
while(blinkToggle == 1){
if (buttonstate3 == HIGH){
delay(100);
blinkToggle = 0;
}
}
while(blinkToggle == 0){
if (buttonstate3 == HIGH){
delay(100);
blinkToggle = 1;
}
}
while(blinkToggle == 1){
delay(150);
analogWrite(lights[0], 0);
analogWrite(lights[1], 0);
analogWrite(lights[2], 0);
delay(150);
analogWrite(lights[0], lightStrength);
analogWrite(lights[1], lightStrength);
analogWrite(lights[2], lightStrength);
}
//make the lights run (Not completed because the other code didn't work)
if (buttonstate4 == HIGH){
delay(100);
}
}
/*此脚本旨在使arduino板控制学校项目乐高玩具上的灯光。
按钮1应该使灯光更亮
按钮2应使灯的亮度降低
按钮3应使灯闪烁
按钮4应该使灯互相跟随。
*/
//设置所有灯和按钮
常量int lights[]={9,8,7};
int-light=0;
const int button1=24;
int buttonstate1=0;
const int button2=25;
int buttonstate2=0;
const int button3=26;
int buttonstate3=0;
const int button4=27;
int buttonstate4=0;
强度=50;
int-blinkToggle=0;
//设置输入和输出
无效设置(){
pinMode(按钮1,输入);
pinMode(按钮2,输入);
pinMode(按钮3,输入);
pinMode(按钮4,输入);
pinMode(灯[0],输出);
pinMode(灯[1],输出);
pinMode(灯[2],输出);
//在这里,我有一个问题,让列表在一行代码中包含所有内容。
模拟写入(灯光[0],灯光强度);
模拟写入(光[1],光强度);
模拟写入(光[2],光强度);
}
void循环(){
//检查按钮是否被按下
buttonstate1=数字读取(button1);
buttonstate2=数字读取(button2);
buttonstate3=数字读取(button3);
buttonstate1=数字读取(button4);
//把灯弄亮些
而(光强度=45){
如果(按钮状态2==高){
延误(5);
光强度=光强度-1;
延误(5);
模拟写入(灯光[0],灯光强度);
模拟写入(光[1],光强度);
模拟写入(光[2],光强度);
}
}
//闪灯
while(blinkToggle==1){
如果(按钮状态3==高){
延迟(100);
闪烁切换=0;
}
}
while(blinkToggle==0){
如果(按钮状态3==高){
延迟(100);
闪烁开关=1;
}
}
while(blinkToggle==1){
延迟(150);
模拟写入(灯[0],0);
模拟写入(灯[1],0);
模拟写入(灯[2],0);
延迟(150);
模拟写入(灯光[0],灯光强度);
模拟写入(光[1],光强度);
模拟写入(光[2],光强度);
}
//使灯光运行(未完成,因为其他代码不起作用)
如果(按钮状态4==高){
延迟(100);
}
}
首先,询问“需要帮助”是一种普遍现象。。你绝对应该问一个更好的问题。例如,我不能完全理解你的意思,问题是什么,在某些情况下,预期的行为是什么(例如,如果我按下更多按钮会发生什么?)
无论如何,这不是一个评论,而是一个答案,因为在你的代码中有一些常见的错误,也许这些错误足以让你的代码正常工作
例如,您将所有测试都编写为
while (lightStrength <= 235){
if (buttonstate1 == HIGH){
这不会停止您的代码
然后,blinkToggle
的测试是错误的。您不应该在一段时间内测试它们,而应该使用if(例如if(blinkToggle==1){
)
按钮4没有实现,我不明白它的行为是什么,所以我无法告诉您如何实现它
我不理解//这里我遇到了一个问题,无法让列表在一行代码中包含所有内容。
注释。如果要在不重复的情况下写入相同的值,请使用for循环:
byte i;
for (i = 0; i < 3; i++)
analogWrite(lights[i], lightStrength);
字节i;
对于(i=0;i<3;i++)
模拟写入(光[i],光强度);
现在,给你的节目一些建议
首先是去bouncing。如果你不知道bouncing是什么,请在google上查找或搜索。不管怎样,我发现Bounce2库在你处理按钮时非常有用。搜索它的示例或只是要求查看更多示例
我建议你在这个程序中改变你的方法。有限状态机这种方法更适合这种情况。即使不使用复杂的库,只需简单的C,您也可以制作一个非常可靠的程序欢迎使用堆栈溢出,请阅读:LED是电流控制的,而不是电压控制的,您确定硬件是正确的吗?到底是什么问题?
byte i;
for (i = 0; i < 3; i++)
analogWrite(lights[i], lightStrength);