Arduino Uno重启后停止正常运行

时间:2016-11-15 19:30:46

标签: arduino

我正在使用与2个Arduino Uno连接的RF 433 MHz模块,一个用于Rx,另一个用于Tx。只要我编码并刻录到电路板上,一切正常。当我重新启动任何2个arduinos时出现问题。接收器停止接收,我不得不重新上传代码。

//Receiver Code
#include <VirtualWire.h>
#include <LiquidCrystal.h>

// Definitions for the LCD connections
#define RS 2
#define E 3
#define D4 4
#define D5 5
#define D6 6
#define D7 7

LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
const int led_pin = 13;
const int transmit_pin = 12;
const int receive_pin = 11;
const int transmit_en_pin = 1;

int i=0;

// Setup function - run one time
void setup() {
    lcd.begin(16,2);                  // Defining the LCD
    vw_set_tx_pin(transmit_pin);
    vw_set_rx_pin(receive_pin);
    vw_set_ptt_pin(transmit_en_pin);
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(1500);       // Bits per sec

    vw_rx_start();       // Start the receiver PLL running

    pinMode(led_pin, OUTPUT);
     lcd.setCursor(0,0); 
  lcd.print("YOLO MF") ;
  delay(1000);
    lcd.clear();                     // Clear the LCD
}

// Loop function - runs forever
void loop() { 
  uint8_t buf[VW_MAX_MESSAGE_LEN];      // Variable to hold the received data
  uint8_t buflen = VW_MAX_MESSAGE_LEN;  // Variable to hold the length of the received data
  lcd.setCursor(0,0); 
  lcd.print("Flex Sensor Test") ;
  lcd.setCursor(0,1);
  lcd.print("With MUX: ") ;
  vw_wait_rx();                         //wait for data
  if (vw_get_message(buf, &buflen))    // If data is received
  {
    digitalWrite(led_pin, HIGH);
    for (i=0;i<1;i++)               // Get the two first bytes
  {
    //Serial.write(buf[i]);// Debugging purpose
    lcd.write(buf[i]);// Write the first bytes on the LCD
  }
  delay(100);
  digitalWrite(led_pin, LOW);
  delay(100);  
  //Serial.println(); 
  }

}


//Transmitter code
/ Include needed libraries
#include <VirtualWire.h>

// Defininition
const int txPowerPin = 13;
const int transmit_pin = 12;
const int receive_pin = 2;
const int transmit_en_pin = 1;
float volt1=0;
const int analogInPin = A5; 
int adc = 0; 
float volt=0;
char msg0[0];

// Setup function - run one time
void setup() {
    vw_set_tx_pin(transmit_pin);
    vw_set_rx_pin(receive_pin);
    vw_set_ptt_pin(transmit_en_pin);
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(1500);       // Bits per sec
    pinMode(txPowerPin, OUTPUT);
}

// Loop function - runs forever
void loop() {
  adc = analogRead(analogInPin);
   volt1 = adc;
  volt = (volt1/1023) * 5;



 if ((volt >= 2.33) && (volt < 2.43))
  {
    delay(63);
    digitalWrite(txPowerPin, HIGH);

    msg0[0] = {'I'};
    vw_send((uint8_t *)msg0, strlen(msg0)); // Sending the msg
    vw_wait_tx();                         // Wait for tx to finish
    //Serial.write('I');
    delay(63);
    digitalWrite(txPowerPin, LOW);                 
  }
  else if ((volt >= 2.85) && (volt <= 3.03))
  {
    delay(63);
    digitalWrite(txPowerPin, HIGH);

    msg0[0] = {'u'};
    vw_send((uint8_t *)msg0, strlen(msg0)); // Sending the msg
    vw_wait_tx();                         // Wait for tx to finish
    //Serial.write('u');
    delay(63);
    digitalWrite(txPowerPin, LOW);                 
  }

    //Serial.println(); 

else if ((volt >3.35))
  {
    delay(63);
    digitalWrite(txPowerPin, HIGH);

    msg0[0] = {'O'};
    vw_send((uint8_t *)msg0, strlen(msg0)); // Sending the msg
    vw_wait_tx();                         // Wait for tx to finish

    delay(63);
    digitalWrite(txPowerPin, LOW);                 
  }
  else
  {
    delay(123);
    digitalWrite(txPowerPin, HIGH);

    msg0[0] = {' '};
    vw_send((uint8_t *)msg0, strlen(msg0)); // Sending the msg
    vw_wait_tx();                         // Wait for tx to finish

    delay(163);
    digitalWrite(txPowerPin, LOW);
  }
}

0 个答案:

没有答案