|
|

楼主 |
发表于 2015-10-11 19:15:37
|
显示全部楼层
zoologist 发表于 2015-9-23 14:06 
直接设置一个Port就好。
arduino 设置的这个 port和你程序的要一致
大神,可以请你帮我看一下代码吗?我每次接收一个信号后就不再接收信号了。我觉得我的while loop 写的有问题。- #include <SPI.h> // needed for Arduino versions later than 0018
- #include <Ethernet.h>
- #include <EthernetUdp.h> // UDP library from: [email protected] 12/30/2008
- #include <Servo.h>
- // Enter a MAC address and IP address of Arduino .
- // The IP address will be dependent on your local network:
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- IPAddress ip(169, 254, 148, 239);
- unsigned int localPort = 8888; // local port to listen on
- // buffers for receiving
- char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
- // An EthernetUDP instance to let us send and receive packets over UDP
- EthernetUDP Udp;
- // define servos
- Servo myServo;
- Servo myServo1;
- Servo myServo2;
- Servo myServo3;
- Servo myServo4;
- //servoPin[5]={5,6,7,8,9}
- //define control signal
- int ControlSignal;
- int stopPosition = 90;
- int level = 12;
- void setup() {
- myServo.attach(9);
- myServo1.attach(8);
- myServo2.attach(7);
- myServo3.attach(6);
- myServo4.attach(5);
- // start the Ethernet and UDP:
- Ethernet.begin(mac, ip);
- Udp.begin(localPort);
- Serial.begin(9600);
- }
- void loop() {
- // if there's data available, read a packet
- int packetSize = Udp.parsePacket();
- if (packetSize)
- {
- Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
- //Serial.print(packetBuffer);
- //Serial.println(packetSize);
- //reset memory
- /* for (int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++){
- packetBuffer[i]=0;
- }
- */
- //decide move direction by targetCode and controlSignal
- int j = 0;
- while (j <= 6) {
- if (packetBuffer[11] == '1' && packetSize <= 16) {
- stopPosition -= level;
- myServo.write(stopPosition);
- myServo1.write(stopPosition);
- myServo2.write(stopPosition);
- myServo3.write(stopPosition);
- myServo4.write(stopPosition);
- delay(15);
- }
- if (packetBuffer[11] == '2' && packetSize > 16) {
- stopPosition = stopPosition + level;
- myServo.write(stopPosition);
- myServo1.write(stopPosition);
- myServo2.write(stopPosition);
- myServo3.write(stopPosition);
- myServo4.write(stopPosition);
- delay(15);
- }
- j++;
- }
- //int k = 0;
- //while (k <= 2) {
- if (packetBuffer[11] == '0') {
- myServo.write(90);
- myServo1.write(90);
- myServo2.write(90);
- myServo3.write(90);
- myServo4.write(90);
- delay(15);
- }
- //k = k + 1;
- //}
- }
- }
复制代码 |
|