Hello there.
I am receiving two analog readings from arduino nano to arduino uno using RF modules.Until now everything is fine.I am applying some code on this analoge reading.when i try to display these lines on serial monitor
Serial.print(speedLeft);
Serial.print("\t" );
Serial.print(speedRight);
Serial.println(" ");
serial monitor suddenly stop displaying at a certain result.
Later i noticed that the problem is from this code line:
analogWrite(pwm1, abs(spdR));
When i disable this line everything goes right.
This is the full Code:
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
const byte pwm2 = 10;//motorLSpeedPin
const byte dir2 = 8;//motorLDirPin
const byte pwm1 = 9;//motorRSpeedPin
const byte dir1 = 7;//motorRDirPin
//variables
//Joystick input variables
int joyXValue = 0;//horizontal
int joyYValue = 0;//vertical
int joyValueMax = 1023;
int joyValueMin = 0;
int joyValueMid = 511;
int joyValueMidUpper = joyValueMid + 10;
int joyValueMidLower = joyValueMid - 10;
//DC motor variables
int speedFwd = 0;
int speedTurn = 0;
int speedLeft = 0;
int speedRight = 0;
byte motorSpeed = 0;
byte motorSpeedMax = 255;
byte motorSpeedMin = 0; //set to smallest value that make motor move (default 0)
// DC motor that I use start to move at 90 pwm value
RH_ASK driver;
void setup(){
pinMode(pwm2, OUTPUT);
pinMode(dir2, OUTPUT);
pinMode(pwm1, OUTPUT);
pinMode(dir1, OUTPUT);
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
void loop(){
uint8_t buf[4];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
joyXValue=(buf[1]<<8)|buf[0];
joyYValue=(buf[3]<<8)|buf[2];
// Serial.print(joyXValue);// Horizontal.
// Serial.print("H<==============>V");
// Serial.println(joyYValue);//vertical..
///////////////////////////////////////////////////////////
if(joyYValue > 521)//backward
{
speedFwd = map(joyYValue, 521, 1023, 0, 255);//map(joyYValue,joyValueMidUpper,joyValueMax,motorSpeedMin,motorSpeedMax
}
else if(joyYValue < 501) //forward
{
speedFwd = map(joyYValue, 501, 0, 0, -255);//map(joyYValue,joyValueMidLower,joyValueMin,-motorSpeedMin,-motorSpeedMax
}
else
{
speedFwd =0;
}
if(joyXValue > 521) //right
{
speedTurn = map(joyXValue, 521, 1023, 0, 255);
}
else if(joyXValue < 501) //left
{
speedTurn = map(joyXValue, 501, 0, 0, -255);
}
else
{
speedTurn =0;
}
speedLeft = speedFwd + speedTurn;
speedRight = speedFwd - speedTurn;
speedLeft = constrain(speedLeft, -255, 255);
speedRight = constrain(speedRight, -255, 255);
MoveRobot(speedLeft,speedRight);
// Serial.print(speedFwd);
// Serial.print("\t" );
// Serial.print(speedTurn);
// Serial.print("\t" );
/*
Serial.print(speedFwd);
Serial.print("\t" );
Serial.print(speedTurn);
Serial.println(" ");*/
Serial.print(speedLeft);
Serial.print("\t" );
Serial.print(speedRight);
Serial.println(" ");
////////////////////////////////////////////////////////////////////////
}
} //End void loop
//////////////////////////////////////////////////
void MoveRobot(int spdL, int spdR){
if(spdL>0)
{
digitalWrite(dir2, HIGH);
}
else
{
digitalWrite(dir2, LOW);
}
if(spdR>0)
{
digitalWrite(dir1, HIGH);
}
else
{
digitalWrite(dir1, LOW);
}
analogWrite(pwm2, abs(spdL));
// analogWrite(pwm1, abs(spdR));// the problem in this code.
}