Pages: [1]   Go Down
Author Topic: UDP Code Help  (Read 299 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey I just started to work with UDP communication between a computer and an Arduino. I am working on a project that involves a computer sending UDP packets to an Arduino, which issues commands. The commands that the Arduino issues is moving a servo back and forth, Moving a stepper back and forth, and some other things. For this project I am using an Arduino Uno, Arduino Ethernet Shield, and an Adafruit Motor Shield.

The problem that I am having is that when I send a packet to the Arduino it reads the first one correctly, but the packet buffer seems to keep the command from  before and reissues it, even though it is being cleared. I was wondering if anyone had any suggestions for how to fix this problem. The code is shown below, and any help that you would be able to provide would be greatly appreciated.

Code:
#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <Servo.h>        //This includes Servo Libraries
#include <AFMotor.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

//Testing LED's
int led = 8;

//Other Varriables
//SC Varriables
int light;                //C

//Firing Varriables
int fStatus;               

//Movement Varriables
int mUD;                   //Movement Up and Down
int mLR;                   //Movement Left and Right
//Tilting Servo Varriables
Servo tiltServo;
const int tiltPin = 9;    //Sets the Tilt sero pin
bool bTilt;                // If true enables the ability for the servo to tilt
bool bTiltDirPos;          //If true servo should tilt only in the positive direction
float tiltSpeed = .01;     //Degrees in which servo tilts per tic
float tiltVal = 0;        //Tilting servo starting position*/

//Stepper Motor Definition
AF_Stepper motor(48, 2);

//FPun
const int FPin = 7;

//Other Varriables
float t = 1;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  pinMode(led, OUTPUT);
  pinMode(FPin, OUTPUT);
  digitalWrite(led, LOW);
  light = 1;
  fStatus = 0;
  mUD = 0;
  mLR = 0;
  Serial.begin(9600);
 
 
  //Initialize Tilt Servo
  tiltServo.attach(tiltPin);
  tiltServo.write(tiltVal);
  bTilt = false;
  bTiltDirPos = true;
 
  motor.setSpeed(50);
}
 
void loop() {
  while(t=1){
     //Controll Tilting
if(bTiltDirPos)
{
if(bTiltDirPos)
{
if(tiltVal <180)
{
tiltVal +=tiltSpeed;
tiltServo.write(tiltVal);
}
}
}
  }
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer); //Changed from packet buffer
    if(packetBuffer[packetSize]='w'){
      Serial.println("Moving Up!");
      TiltUp();
    }
    else if(packetBuffer[packetSize]='s'){
      Serial.println("Moving Down!");
      TiltDown();
    }
    else if(packetBuffer[packetSize]='d'){
      Serial.println("Rotating Right!");
      mLR = 2;
      MovementLeftRight();
    }
    else if(packetBuffer[packetSize]='a'){
      Serial.println("Rotating Left!");
      mLR = 1;
      MovementLeftRight();
    } 
    else if(packetBuffer[packetSize]='q'){
      Serial.println("Movement Up Down Stop!");
      TiltStop();
    } 
    else if(packetBuffer[packetSize]='e'){
      Serial.println("Rotating Left Right Stopped!");
      mLR = 0;
      MovementStopLeftRight();
    } 
    else if(packetBuffer[packetSize]='o'){
      Serial.println("S On!");
      light = 1;
      fStatus = 0;
      Fire();
    } 
    else if(packetBuffer[packetSize]='p'){
      Serial.println("S Off!");
      light = 0;
    }     
    else if(packetBuffer[packetSize]='z'){
      Serial.println("F Started!");
      Fire();
    } 
    else if(packetBuffer[packetSize]=='x'){
      Serial.println("S F!");
      light = 1;
      Fire();
      fStatus = 0;
    }
    else if(packetBuffer[packetSize]='t'){
      Serial.println("Stopping all Movement!");
      MovementStopAll();
    } 
    else if(packetBuffer[packetSize]='y'){
      Serial.println("Stopping Everything!");
      StopEverything();
    }   
    else if(packetBuffer[packetSize]='g'){
      Serial.println("S C!");
      SafetyCheck();
    } 
  for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++) packetBuffer[i] = 0; //Clear Packet Buffer

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  delay(10);
}



void MovementUpDown(){
  if(mUD = 0){
    bTilt = false;
  }
  else if(mUD = 1){
    bTilt = true;
    bTiltDirPos = false;
  }
  else if(mUD = 2){
    bTilt = true;
    bTiltDirPos = false;
  }
}

void MovementLeftRight(){
  if(mLR = 0){
    motor.step(0, FORWARD, SINGLE);
  }
  else if(mLR = 1){
    motor.step(1, FORWARD, SINGLE);
    }
  else if(mLR = 2){
    motor.step(1, BACKWARD, SINGLE);
  }
}


void MovementStopAll(){
  mUD = 0;
  mLR = 0;
  MovementUpDown();
  MovementLeftRight();
}

void MovementStopLeftRight(){
  mLR = 0;
  MovementLeftRight();
}

void MovementStopUpDown(){
  mUD = 0;
  MovementUpDown();
}
   
void StopFiring(){
  fStatus = 0;
  light = 0;
  Fire();
}
 
void StopEverything(){
  MovementStopAll();
  StopFiring();
}


void Fire(){
  if(light = 0){
    digitalWrite(FPin, HIGH);
    fStatus = 1;
    Serial.println("TGIS!");
  }
  else if(light = 1){
    digitalWrite(FPin, LOW);
    fStatus = 0;
    Serial.println("SION!TGISF");
  }
}

//Check the safety status
void SafetyCheck(){
  Serial.println("SI: ");
      if(light = 1){
        Serial.print("On");
      }
      else if(light = 0){
        Serial.print("Off");
      }
      Serial.print("!");
}

//Check Movement Status of all parts
void MovementStatus(){
  Serial.println("T I:");
  if(mLR = 0){
    Serial.println("Not moving Left of Right");
  }
  else if(mLR = 1){
    Serial.println("Moving Left");
  }
  else if(mLR = 2){
    Serial.println("Moving Right");
  }
  Serial.print(" and ");
  if(mUD = 0){
    Serial.print("Not moving Up or Down");
  }
  else if(mUD = 1){
    Serial.print("moving up");
  }
  else if(mUD = 2){
    Serial.print("moving down");
  }
  Serial.print("!");
}

//Tilt Servo Commands
void TiltUp()
{
  bTilt = true;
  bTiltDirPos = true;
}

void TiltDown()
{
  bTilt = true;
  bTiltDirPos = false;
}

void TiltStop()
{
  bTilt = false;
}
 
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 169
Posts: 12441
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(packetBuffer[packetSize]='d'){

the = is an assignment
the == is a compare

occurs multiple times in your code.

TIP: press CTRL-T before copying code -> does some reformatting.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: