这是我的代码。它在我的Arduino MEGA 2560上编译并运行。
#include "Arduino.h"//Standard Arduino Library
#include "Print.h" //Print to serial library, Serial.Println would not work by default for some reason.
#define led_pin 13 //Set var led_pin to equal 13, meaning pin 13
#define pinstate digitalRead(read_pin)
#define read_pin 50 // " 50, "50
int loopstore; //Def as global var so it won't be reset on next loop iteration
void setup() //Setup function, used to set pins and vars before running main Arduino Program
{
Serial.begin(9600); //Start serial comms, set baud rate
Serial.println("\nHello User.\n\nArduio has set baud rate and started serial communication.\n\nThank You, Sketch will run momentarily.\n\n");
pinMode(led_pin,OUTPUT); //Set led_pin to digital output mode;
pinMode(read_pin,INPUT); //", " read mode
}
void loop() //Actual program
{
loopstart = 1;
do
{
++loopstore;
} while (loopstart == 0);
Serial.println("This program has ran this many times:\n");
Serial.println(loopstore);
digitalWrite(led_pin,HIGH); //Set pin high
delay(1000); //Wait
if (pinstate == 1) //If else statement for outputting pin state to console
{
Serial.println("\nPin is in a HIGH state\n\n"); // Output to console
}
else
{
Serial.println("Pin is in a LOW state\n\n"); // Output to console
}
delay(1500); //Wait
digitalWrite(led_pin,LOW); //Set pin to Low State
delay(1000); //Wait
if (pinstate == 1) //If else funct for outputting pin state to console
{
Serial.println("Pin is in a HIGH state\n\n"); // Output to console
}
else
{
Serial.println("Pin is in a LOW state\n\n"); // Output to console
}
delay(1000); //Wait
digitalWrite(led_pin,HIGH); //Set pin high
delay(1500); //Wait
if (pinstate == 1) //If else funct for outputting pin state to console
{
Serial.println("Pin is in a HIGH state\n\n"); // Output to console
}
else
{
Serial.println("Pin is in a LOW state\n\n"); // Output to console
}
delay(1000); //Wait
digitalWrite(led_pin,LOW); //Set pin to Low State
delay(1500); //Wait
if (pinstate == 1) //If else funct for outputting pin state to console
{
Serial.println("Pin is in a HIGH state\n\n"); // Output to console
}
else
{
Serial.println("Pin is in a LOW state\n\n"); // Output to console
}
delay(1000); //Wait
digitalWrite(led_pin,HIGH); //Set pin high
delay(500); //Wait
if (pinstate == 1) //If else funct for outputting pin state to console
{
Serial.println("Pin is in a HIGH state\n\n"); // Output to console
}
else
{
Serial.println("Pin is in a LOW state\n\n"); // Output to console
}
delay(1000); //Wait
digitalWrite(led_pin,LOW); //Set pin to Low State
delay(500); //Wait
if (pinstate == 1) //If else funct for outputting pin state to console
{
Serial.println("Pin is in a HIGH state\n\n"); // Output to console
}
else
{
Serial.println("Pin is in a LOW state\n\n"); // Output to console
}
}
我正在学习使用Arduino,我制作了一个快速而又脏的程序,只需设置Arduino(13)上LED引脚的引脚状态,然后用引脚50读取它。这是一个毫无意义的程序,但我我正在努力学习/练习。
对于额外练习,我还为loop()函数运行的次数设置了一个计数器。
我做的“Do While”声明增加了计数器。然后我将结果打印到序列中。如果loopstart == 0,则看到“Do While”语句再次运行。这是不可能的,因为它永远不会被设置为0.我想要一种“直通”计数器,但这是最好的方法吗?
我几乎肯定有一种方法可以更有效地完成我上面所做的事情,但是对Arduino(以及一般的编程)不熟悉,我不知道我怎么能够简化这一点。
任何人都有任何建议或者他们可以指向我的地方吗?
我曾尝试在线查找C ++中的计数器示例,但我找不到任何超出“For”循环的内容。
感谢您的时间并帮助孩子学习!
编辑:不知道CodeReview。谢谢!
答案 0 :(得分:0)
也许这样的事情最适合你。 如果我理解正确你的程序基本上切换了led的3倍,然后检查前后的延迟(这是可变的)引脚状态。
如果您希望我详细介绍其他部分,请问我将编辑我的帖子;)
#include "Arduino.h"//Standard Arduino Library
#include "Print.h" //Print to serial library, Serial.Println would not work by default for some reason.
// Some debugging macros
#define DEBUG 1
#define slog(a) { if (DEBUG) Serial.println(a); }; // No debug directive will be in compiled file if DEBUG is set to 0
// Let's define our port mapping
#define LED_PIN _BV(7) //bit 7 on PORTB on 2560 correspond to digital pin 13 (check http://arduino.cc/en/Hacking/PinMapping2560)
#define READ_PIN _BV(3)
// Some quick access for led pin / read pin manipulation
// Let's use bitwise operation on PORTB, digitalRead => 28 cycle, direct read with PORTB => 1 cycle (28x faster!)
// I prefer defining setters as functions, easier to read, but you are not force to (it is exactly the same once compiled)
//#define LED_ON() { PORTB |= LED_PIN; } // Put LED_PIN bit to 1 on PORTB
//#define LED_OFF() { PORTB &= ~LED_PIN; } // Put LED_PIN bit to 0 on PORTB
#define LED_TOGGLE() { PORTB ^= LED_PIN; } // Put LED_PIN bit to 0 if it was 1 or to 1 if it was 0
#define READ_STATE PORTB & READ_PIN // Read LED_PIN bit on PORTB
int loopcount = 0; //Def as global var so it won't be reset on next loop iteration
// Let's create an array which will define all the delays before and after serial calls
// ex: {1000, 1500} => 1000ms before serial output, 1500ms after serial output
int wrapDelays[][2] = { {1000, 1500}, {1000, 1000}, {1500, 1000}, {1500, 1000}, {500, 1000}, {500, 0} };
void setup() //Setup function, used to set pins and vars before running main Arduino Program
{
if (DEBUG) Serial.begin(9600); //Start serial comms, set baud rate
slog("\nHello User.\n\nArduio has set baud rate and started serial communication.\n\nThank You, Sketch will run momentarily.\n\n");
// Output/Input for PORTB (in DDRB register)
DDRB |= LED_PIN; // Set LED_PIN to digital output mode (put LED_PIN bit to 1)
DDRB &= ~READ_PIN; // Ensure READ_PIN is in input mode (put READ_PIN bit to 0)
}
// ToggleLed, write read pin state on serial, and wait `delayBefore`ms before and `delayAfter`ms after
void toggleLedBetweenDelays(int delayBefore, int delayAfter){
LED_TOGGLE();
if (delayBefore > 0) delay(delayBefore);
if (READ_STATE) {//If else funct for outputting pin state to console
slog("Pin is in a HIGH state\n\n"); // Output to console
}
else
slog("Pin is in a LOW state\n\n"); // Output to console
if (delayAfter > 0) delay(delayAfter);
}
void loopCounter(){
loopcount++; // increase count of loops
slog("This program has ran this many times:\n");
slog(loopcount);
}
void loop() //Actual program
{
for (int i = 0, l = sizeof(wrapDelays) / sizeof(wrapDelays[0]); i < l; ++i)
{
toggleLedBetweenDelays(wrapDelays[i][0], wrapDelays[i][1]);
}
loopCounter();
}
注意:不要忘记它不是因为您编写的代码较小,最终编译的二进制文件也会更小/优化。