允许我通过通讯端口改变状态的Arduino程序?

时间:2015-10-31 15:23:17

标签: arduino serial-port arduino-uno comm

新来的!多次被推荐到这里寻求帮助所以我在这里。

我应该编写一个程序,允许我通过通讯端口改变闪烁的LED灯的速率。我确信这很容易,但我真的不清楚,因为我在这堂课中落后了。

任何事情都会有所帮助,我真的想学习如何做到这一点,而不是来到这里得到答案。

先谢谢!

    // global variables
      #include <EEPROM.h>
     unsigned long ms_runtime;
     int state;
       // possible values 0 -> 1 -> 2 -> 3 -> 0
     int one_ms_timer;
//  define all timers as unsigned long (they are incremented every 100ms = 0.1s)
unsigned long timer1;
unsigned long button_dbnc_tmr = 0;
//  timer1 is used for blinking LED
const int LED1 = 13;
// function prototypes
void read_memory(void);
void update_memory(void);
void comm_control(void);
void led_control(void);
void turnoff(void);
void flash_1s(void);
void flash_2s(void);
void flash_3s(void);
void timers(void);

void setup()
{
        read_memory();
        pinMode(LED1, OUTPUT);
        Serial.begin(9600);
        //initialize uart
}
void loop()
{
    static bool allow_change;
    static int counter;
    timers();
    comm_control();
    led_control();

}
void led_control()
{
    switch (state)

    {
    case 0:
        turnoff();
        break;
    case 1:
        flash_1s();
        break;
    case 2:
        flash_2s();
        break;
    case 3:
        flash_3s();
        break;
    }
}

void turnoff()
{
    digitalWrite(LED1, LOW);
}
void flash_1s()
{
    if (timer1 < 10)
        digitalWrite(LED1, HIGH);
    else
    {
        digitalWrite(LED1, LOW);
        if (timer1 >= 20)
            timer1 = 0;
    }
}
void flash_2s()
{
    if (timer1<20)
        digitalWrite(LED1, HIGH);
    else
    {
        digitalWrite(LED1, LOW);
        if (timer1 >= 30)
            timer1 = 0;
    }
}
void flash_3s()
{
    if (timer1<30)
        digitalWrite(LED1, HIGH);
    else
    {
        digitalWrite(LED1, LOW);
        if (timer1 >= 40)
            timer1 = 0;
    }
}
void read_memory()
{
    timer1 = EEPROM.read(one_ms_timer);
    timer1++;
    EEPROM.write(one_ms_timer, timer1);
    Serial.begin(9600);
}
void update_memory()
{
    EEPROM.update(timer1, one_ms_timer);
}
void comm_control(void);
{
    char comm_reveier = serial_read;
    if (
        )
}
void timers(void)
{
    if (millis() > (ms_runtime + 1))
    {
        ms_runtime = ms_runtime + 1;
        one_ms_timer++;
    }
    else if (ms_runtime > millis())
        ms_runtime = millis();

    if (one_ms_timer > 99) //every 100 ms
    {
        one_ms_timer = 0;
        button_dbnc_tmr++;
        timer1++;
    }
}

2 个答案:

答案 0 :(得分:0)

在Arduino板上加载标准Firmata库。然后使用您选择的库来构建comm prog。可以在here找到这些概述。

答案 1 :(得分:0)

假设您使用的是Arduino IDE,此代码示例应该可以让您大致了解需要执行的操作:

// pins for the LEDs:
const int ledPin = 13;

// Default blink rate
int rate = 1000;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int rate = Serial.parseInt();
  }

  digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(rate);                  // wait for a second
  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
  delay(rate);                  // wait for a second  
}