Hey,
In my case, I am trying to control the DC Motor using analogwrite pwm(0 to 255). But i am facing a problem, when 0 to 127 - Motor stopped completely and 127 to 255- Runs at high Speed.
I am using a Arduino Ethernet shield which is connected to Raspberry via Simulink UDP.
If i run the dc motor simply connecting only arduinoto dc motor. Everything is perfect which means i can control the (0 to 255) according to 0 to 5v which in sense my circuit is good but the problem arise when i use this
Arduino Ethernet shield which is connected to Raspberry via Simulink UDP.
Can anybody, check my dc motor code whether i had made any mistakes. why this problem accur
when 0 to 127 - Motor stopped completely and 127 to 255- Runs at high Speed.
#include <SPI.h> //Load Spi Lib
#include <Ethernet.h> //Load ethernet lib
#include <EthernetUdp.h> //Load UDP lIB
#include <Servo.h> //Load servo lib
/* Pin Configurations */
int servoPin = 8,
DCMotorPin = 9;
int DCMotorPower = 100;
/* Packet Initial Conditions */
int forward = 0,
left = 0,
right = 0;
Servo myservo;
int servoAngle = 90;
/* UDP Configurations */
byte mac[] = { 0xDE, 0xEB, 0xDE, 0xEB, 0xDE, 0xEB };
IPAddress ip(169, 254, 27, 47);
unsigned int localPort = 8888;
byte RaspIP[] = {169, 254, 27, 46};
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
EthernetUDP Udp;
void setup() {
Ethernet.begin(mac, ip); //Initialize Ethernet
Udp.begin(localPort); //Initialize UDP
Serial.begin(9600); //creating a serial monitor with bandrate of 9600 // Turn on Serial port
//command for servo
myservo.attach(servoPin); //Initialize Servo
myservo.write(servoAngle);
//command for DC Motor
pinMode(DCMotorPin, OUTPUT);
digitalWrite(DCMotorPin, LOW);
}
void loop() {
// Check for incoming Packets
int packetSize = Udp.parsePacket();
if (packetSize) {
IPAddress remote = Udp.remoteIP();
if (remote == RaspIP) {
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
forward = packetBuffer[0],
right = packetBuffer[1],
left = packetBuffer[2];
}
}
// PWM to control the speed of car
if (forward == 1)
analogWrite(DCMotorPin, DCMotorPower);
// Calculate Servo Orientation and write the desired value once
if (right == 1 && left == 0)
servoAngle = 60;
else if (left == 1 && right == 0)
servoAngle = 120;
else
servoAngle = 90;
myservo.write(servoAngle);
Serial.print("Forward: ");
Serial.println(forward);
Serial.print("Right: ");
Serial.println(right);
Serial.print("Left: ");
Serial.println(left);
Serial.print("Servo Angle: ");
Serial.println(servoAngle);
Serial.println();
Serial.println("----");
}