Robofun 機器人論壇

標題: 求救 兩組程式結合 [打印本頁]

作者: zxcaul4    時間: 2013-12-9 16:22
標題: 求救 兩組程式結合
---- SimpleMessageSystem 程式 ----
Control Arduino board functions with the following messages:

r a -> read analog pins
r d -> read digital pins
w d [pin] [value] -> write digital pin
w a [pin] [value] -> write analog pin


Base: Thomas Ouellet Fredericks
Additions: Alexandre Quessy

*/

// Include de SimpleMessageSystem library
// REMOVE THE FOLLOWING LINE IF USING WIRING
#include <SimpleMessageSystem.h>

void setup()
{

  // The following command initiates the serial port at 9600 baud. Please note this is VERY SLOW!!!!!!
  // I suggest you use higher speeds in your own code. You can go up to 115200 with the USB version, that's 12x faster
  Serial.begin(9600); //Baud set at 9600 for compatibility, CHANGE!


}

void loop()
{

  if (messageBuild() > 0) { // Checks to see if the message is complete and erases any previous messages
    switch (messageGetChar()) { // Gets the first word as a character
    case 'r': // Read pins (analog or digital)
      readpins(); // Call the readpins function
      break; // Break from the switch
    case 'w': // Write pin
      writepin(); // Call the writepin function
    }

  }

}

void readpins(){ // Read pins (analog or digital)

  switch (messageGetChar()) { // Gets the next word as a character

    case 'd': // READ digital pins

    messageSendChar('d');  // Echo what is being read
    for (char i=2;i<14;i++) {
      messageSendInt(digitalRead(i)); // Read pins 2 to 13
    }
    messageEnd(); // Terminate the message being sent
    break; // Break from the switch

  case 'a': // READ analog pins

    messageSendChar('a');  // Echo what is being read
    for (char i=0;i<6;i++) {
      messageSendInt(analogRead(i)); // Read pins 0 to 5
    }
    messageEnd(); // Terminate the message being sent

  }

}

void writepin() { // Write pin

  int pin;
  int state;

  switch (messageGetChar()) { // Gets the next word as a character

    case 'a' : // WRITE an analog pin

    pin = messageGetInt(); // Gets the next word as an integer
    state = messageGetInt(); // Gets the next word as an integer
    pinMode(pin, OUTPUT); //Sets the state of the pin to an output
    analogWrite(pin, state); //Sets the PWM of the pin
    break;  // Break from the switch


    // WRITE a digital pin
  case 'd' :

    pin = messageGetInt();  // Gets the next word as an integer
    state = messageGetInt();  // Gets the next word as an integer
    pinMode(pin,OUTPUT);  //Sets the state of the pin to an output
    digitalWrite(pin,state);  //Sets the state of the pin HIGH (1) or LOW (0)


  }

}

接收端:
#include<stdlib.h>
byte sensorValueA;                //宣告 BYTE變數  sensorValueA

int a;                             //宣告整數a

void setup() {
Serial.begin(9600);            //設定 第一組通道Serial的鮑率為9600

}
void loop() {  
  if (Serial.available() > 0){        //如果感測到第一組通道有資料傳過來
sensorValueA = Serial.read();    //將讀到的資料放到sensorValueA
  a =int (sensorValueA);          //將sensorValueA 從BYTE轉換成int 放到a

  }
Serial.print("No.1 = ");
Serial.print(a);
delay(1000);
}


各位大大接收端與simplemessagesystem 結合
作者: mzw2008    時間: 2013-12-9 16:52
這兩個程式有甚麼關係!?
沒關係的話COPY再一起就好了
SETUP 、LOOP的部分合成一個就行

但若有關係要詳述關連的部分才有辦法結合




歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/) Powered by Discuz! X3.2