Hi,
This is my first go at using Arduino and I don't think I've done to bad,
I can program in VB 6, but I could never get on with C so I have shocked myself.
I am trying to program the Arduino with a Ethernet shield, and Motor shield.
As you can see below I have a basic code written but I am having trouble getting UDP data, I would like to be able to send 'A123', 'DHIGH' and/or 'E255' and have the Arduino decode this to
A = Servo 1 and 123 = Servo Placement
the same as 'DHIGH' would be D = Motor Drive 1 A and HIGH = Output HIGH
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h> // Ethernet board connected - Uses A0, A1, (D2 or D4), D10, D11, D12, D13
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <Servo.h> // Servo Library - Can be used on D3 D5 D6 D9
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
IPAddress ip(192, 168, 1, 177); // IP Address
unsigned int localPort = 8888; // Local IP port to listen on
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; // Subnet Address
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
EthernetUDP Udp; // An EthernetUDP instance to let us send and receive packets over UDP
Servo myservoA; // create servo object to control a servo
Servo myservoB; // a maximum of eight servo objects can be created
int dir1PinA = 7; // motor A Dir 1
int dir2PinA = 8; // motor A Dir 2
int speedPinA = 6; // motor A Speed
int dir1PinB = 2; // motor B Dir Pin 1
int dir2PinB = 4; // motor B Dir Pin 2
int speedPinB = 9; // motor B Speed
const int TempIn = A0; // Temp sensor on A0
int TempValue = 0; // Setup val for temp
int Servo1 = 90; // Setup for servo1
int Servo2 = 90; // Setup for servo2
int SpeedA = 0; // Setup for motor speed A
int SpeedB = 0; // Setup for motor speed B
int Dir1A = 0; // Setup for motor A dir 1
int Dir2A = 0; // Setup for motor A dir 2
int Dir1B = 0; // Setup for motor B dir 1
int Dir2B = 0; // Setup for motor B dir 2
void setup() { // run setup only once on boot
Ethernet.begin(mac,ip); // Enable Ethernet
Udp.begin(localPort); // Start UDP Connection
myservoA.attach(3); // attaches the servo on pin 9 to the servo object
myservoB.attach(5); // attaches the servo on pin 6 to the servo object
pinMode(dir1PinA, OUTPUT); // attaches the motor drive dir 1
pinMode(dir2PinA, OUTPUT); // attaches the motor drive dir 2
pinMode(speedPinA, OUTPUT); // attaches the motor drive speed
pinMode(dir1PinB, OUTPUT); // attaches the motor drive dir 1
pinMode(dir2PinB, OUTPUT); // attaches the motor drive dir 2
pinMode(speedPinB, OUTPUT); // attaches the motor drive speed
Serial.begin(9600); // Start Serial Output
Serial.write("SYSTEM BOOT"); // Send Power on to serial
myservoA.write(Servo1); // tell servo to go to position 90
myservoB.write(Servo2); // tell servo to go to position 90
analogWrite(speedPinA, SpeedA); // tell the motor A drive to stop
analogWrite(speedPinB, SpeedB); // tell the motor B drive to stop
digitalWrite(dir1PinA, LOW); // tell the motor drive A to brake
digitalWrite(dir2PinA, LOW); // tell the motor drive A to brake
digitalWrite(dir1PinB, LOW); // tell the motor drive B to brake
digitalWrite(dir2PinB, LOW); // tell the motor drive B to break
} // end of setup
void loop() // Start Running System
{
int packetSize = Udp.parsePacket(); // if there's data available, read a packet
if(packetSize)
{
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE); // read the packet into packetBufffer
char ch = packetBuffer[0];
switch(ch)
{
case '0': // if 0 is sent via udp then do this -->>
myservoA.write(90); // tell servo to go to position
myservoB.write(90); // tell servo to go to position
analogWrite(speedPinA, 0); // tell the motor A drive to stop
analogWrite(speedPinB, 0); // tell the motor B drive to stop
digitalWrite(dir1PinA, LOW); // tell the motor drive A to brake
digitalWrite(dir2PinA, LOW); // tell the motor drive A to brake
digitalWrite(dir1PinB, LOW); // tell the motor drive B to brake
digitalWrite(dir2PinB, LOW); // tell the motor drive B to break
break; // end of case 0
// place to put cases
default : // if recived info vai udp and not found in the case do this -->>
Serial.print("No Case Found for: "); // if case not found send info via serial
Serial.print(ch); // send udp info back to serial
Serial.print(" - "); // send udp info back to serial
Serial.print(packetBuffer); // send udp info back to serial
break; // end of case not found
}
TempValue = analogRead(TempIn); // Read temp value
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); // open udp on address that has connected to arduino to send feed back to
Udp.write("Dir1 A = " );
Udp.println(Dir1A); // send Dir 1 A value back via udp
Udp.write("\nDir 2 A = ");
Udp.println(Dir2A); // send Dir 2 A value back via udp
Udp.write("\nSpeed A = ");
Udp.println(SpeedA); // send Speed A value back via udp
Udp.write("\nDir 1 B = ");
Udp.println(Dir1B); // send Dir 1 B value back via udp
Udp.write("\nDir 2 B = ");
Udp.println(Dir2B); // send Dir 2 B value back via udp
Udp.write("\nSpeed B = ");
Udp.println(SpeedB); // send Speed B value back via udp
Udp.write("\nServo 1 = ");
Udp.println(Servo1); // send Servo 1 value back via udp
Udp.write("\nServo 2 = ");
Udp.println(Servo2); // send Servo 2 value back via udp
Udp.write("\nTemp = ");
Udp.println(TempValue); // send temp value back via udp
Udp.endPacket(); // close udp
}
char packetBuffer[] = " "; //Clear the buffers so as not to leave text behind from one messag to the next
delay(15); // waits 15ms for the servo to reach the position and/or udp input wait ...
} // end system run, but now loop and start system run again
Any help on this would be great,
Thanks