Aloha Jon –
Thanks for the response. Kinda getting matters in hand. What stumps me are the delay()'s I had to add into the code to get things underway. Commenting out the serial data, and adding the minimum of delay()'s obtained through testing, I get reasonable response over RF. That done, I had to write in 4 new variables, as just mapping x&y to new values for PWM did not seem to work. I operate the joystick in a “+” fashion, as written in the code, although I seem to get differential turning with stick approaching “x” positions (under investigation). Code below.
void loop() {
if ( radio.available() )
{
bool done = false;
while (!done)
{
done = radio.read( joystick, sizeof(joystick) );
//Serial.print("X = ");
//Serial.print(joystick[0]); // x value
//Serial.print(" Y = ");
//Serial.println(joystick[1]); // y value
x = joystick[0];
y = joystick[1];
//**********BEGIN HERE RF CONTROL OF MOTORS********
if (y > 532)
{
fory = map(y, 512, 1023, 0, 200);
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMA,fory);
analogWrite(PWMB,fory);
delay(40);
}
if (y < 492)
{
revy = map(y, 0, 512, 200, 0);
digitalWrite (AIN1,LOW);
digitalWrite (AIN2,HIGH);
digitalWrite (BIN1,LOW);
digitalWrite (BIN2,HIGH);
analogWrite(PWMA,revy);
analogWrite(PWMB,revy);
delay(40);
}
if (492 < y < 532)
{
if (x > 532) // turn right, zero point turn
{
rtx = map(x, 512, 1023, 0, 200);
digitalWrite (AIN1,LOW);
digitalWrite (AIN2,HIGH);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMA,rtx);
analogWrite(PWMB,rtx);
delay(40);
}
if (x < 492) // turn left, zero point turn
{
lex = map(x, 0, 512, 200, 0);
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
digitalWrite (BIN1,LOW);
digitalWrite (BIN2,HIGH);
analogWrite(PWMA,lex);
analogWrite(PWMB,lex);
delay(40);
}
if ( 492 < y < 532 && 492 < x < 532) // joystick centered
{
Stop();
}
}
}
}
else //****IF NO RADIO, GO TO OBSTACLE CHECKING
{
robotControl();
//Serial.println("No radio available");
}
delay(50);
} //********END LOOP*********
I built up a quickie transmitter in this process with an AMini, moving from the BB’d328P-PU. Maybe that was the trick. Anyway, love these A’s, as those blue-colored Micro’s are always giving me headaches when trying to just drop a sketch onto it. Am posting full code below, in case anyone can suggest improvements. I am particularly interested in improving the chooseLR() function such that gives less predictable actions by the bot. Meaning, well, noobing, I lack the words to describe.
A hui hou
Mark
//AStar_md08a_Tamiya_nRF24
//24 Aug 2014
//**************RADIO STUFF******
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <NewPing.h>
#define CE_PIN 4
#define CSN_PIN 9
//*********PING STUFF************
#define TRIGPIN A0
#define ECHOPIN A1
#define MAXDIST 500
#define COLLDIST 20
#define CLOSINGDIST 40
//#define ALERTDIST COLLDIST*3
NewPing sonar(TRIGPIN, ECHOPIN, MAXDIST); // Ping sensor pins for library
//*********MOTOR DRIVER STUFF*********
#define PWMA 10
#define AIN1 7
#define AIN2 8
#define PWMB 11
#define BIN1 6
#define BIN2 5
#define STBY 12 // HIGH to enable, LOW to disable
#define MAXSPEED 120
int speedSet = 0;
//*******MORE RADIO STUFF************
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
int joystick[2];
int x = 0;
int y = 0;
//int speedX = 0;
//int speedY = 0;
//********MOTOR SPEED VARIABLES OVER RADIO**********
int fory = 0;
int revy = 0;
int rtx = 0;
int lex = 0;
//*********PING VARS & PING FUNCTION*******
int curDist = 0;
int val[3];
//const int numReadings = 3;
int readPing() {
delay(50);
unsigned int uS = sonar.ping();
int cm = uS/US_ROUNDTRIP_CM;
return cm;
}
//***********SETUP********
void setup() {
Serial.begin(9600);
//****md08a motor driver****
pinMode(PWMA,OUTPUT);
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(STBY,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
pinMode(PWMB,OUTPUT);
val[0] = 0;
val[1] = 0;
val[2] = 0;
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
delay(2000);
digitalWrite(STBY,HIGH);
Forward();
}
//*********LOOP*************
void loop() {
if ( radio.available() )
{
bool done = false;
while (!done)
{
done = radio.read( joystick, sizeof(joystick) );
//Serial.print("X = ");
//Serial.print(joystick[0]); // x value
//Serial.print(" Y = ");
//Serial.println(joystick[1]); // y value
x = joystick[0];
y = joystick[1];
//**********BEGIN HERE RF CONTROL OF MOTORS********
if (y > 532)
{
fory = map(y, 512, 1023, 0, 200);
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMA,fory);
analogWrite(PWMB,fory);
delay(40);
}
if (y < 492)
{
revy = map(y, 0, 512, 200, 0);
digitalWrite (AIN1,LOW);
digitalWrite (AIN2,HIGH);
digitalWrite (BIN1,LOW);
digitalWrite (BIN2,HIGH);
analogWrite(PWMA,revy);
analogWrite(PWMB,revy);
delay(40);
}
if (492 < y < 532)
{
if (x > 532) // turn right, zero point turn
{
rtx = map(x, 512, 1023, 0, 200);
digitalWrite (AIN1,LOW);
digitalWrite (AIN2,HIGH);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMA,rtx);
analogWrite(PWMB,rtx);
delay(40);
}
if (x < 492) // turn left, zero point turn
{
lex = map(x, 0, 512, 200, 0);
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
digitalWrite (BIN1,LOW);
digitalWrite (BIN2,HIGH);
analogWrite(PWMA,lex);
analogWrite(PWMB,lex);
delay(40);
}
if ( 492 < y < 532 && 492 < x < 532) // joystick centered
{
Stop();
}
}
}
}
else //****IF NO RADIO, GO TO OBSTACLE CHECKING
{
robotControl();
//Serial.println("No radio available");
}
delay(50);
} //********END LOOP*********
//*******OBSTACLE CHECKING******
void robotControl(){
curDist = readPing(); // read distance
if (curDist < CLOSINGDIST)
{
if (curDist < COLLDIST)
{
changePath();
}
else
{
aheadSlow();
}
}
else Forward();
}
//*********PING CONTROL**********
void changePath(){
Stop();
turnLeft();
delay(500);
Stop();
delay(100);
val[0] = curDist; //take left reading
delay(100);
turnRight();
delay(500);
Stop();
delay(100);
val[1] = curDist; //take center reading
delay(100);
turnRight();
delay(500);
Stop();
delay(100);
val[2] = curDist; //take right reading
delay(100);
turnLeft();
delay(500);
chooseLR();
}
void chooseLR(){ //changed this funxn from V3
if(val[0] > COLLDIST) // left clear
{
if(val[2] > COLLDIST) //right clear
{
if(val[0] > val[2])// left is more clear
{
turnLeft();
delay(500);
Forward();
}
else
{
turnRight();
delay(500);
Forward();
}
}
/*
else
{
turnLeft();
delay(500);
Forward();
}
*/
}
else
{
if(val[2] > COLLDIST) //right clear
{
turnRight();
delay(500);
Forward();
}
else
{
Reverse();
delay(600);
if (random(2) == 0)
{
turnLeft();
delay(500);
}
else turnRight();
delay(500);
}
}
}
//********MOTOR CONTROL**********
void Forward ()
{
Serial.println("Forward");
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
//for (speedSet = 0; speedSet < MAXSPEED; speedSet +=1)
for (speedSet = 0; speedSet < MAXSPEED; ++speedSet)
{
analogWrite(PWMA,speedSet);
analogWrite(PWMB,speedSet);
//delay(5);
}
delay(5);
}
void aheadSlow ()
{
Serial.println("Ahead slow");
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMA,MAXSPEED*0.5);
analogWrite(PWMB,MAXSPEED*0.5);
delay(10);
}
void Reverse ()
{
Serial.println("Reverse");
digitalWrite (AIN1,LOW);
digitalWrite (AIN2,HIGH);
digitalWrite (BIN1,LOW);
digitalWrite (BIN2,HIGH);
//for (speedSet = 0; speedSet < MAXSPEED; speedSet +=1)
for (speedSet = 0; speedSet < MAXSPEED; ++speedSet)
{
analogWrite(PWMA,speedSet*0.5);
analogWrite(PWMB,speedSet*0.5);
}
delay(5);
}
void turnLeft ()
{
Serial.println("Turning left");
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
digitalWrite (BIN1,LOW);
digitalWrite (BIN2,HIGH);
//for (speedSet = 0; speedSet < MAXSPEED; speedSet +=1)
for (speedSet = 0; speedSet < MAXSPEED; ++speedSet)
{
analogWrite(PWMA,speedSet*0.5);
analogWrite(PWMB,speedSet*0.5);
}
delay(5);
}
void turnRight ()
{
Serial.println("Turning right");
digitalWrite (AIN1,LOW);
digitalWrite (AIN2,HIGH);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
//for (speedSet = 0; speedSet < MAXSPEED; speedSet +=1)
for (speedSet = 0; speedSet < MAXSPEED; ++speedSet)
{
analogWrite(PWMA,speedSet*0.5);
analogWrite(PWMB,speedSet*0.5);
}
delay(5);
}
void Stop ()
{
Serial.println("Stopped");
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,HIGH);
analogWrite(PWMA,255);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,HIGH);
analogWrite(PWMB,255);
}
void turnAround()
{
Reverse();
delay(500);
turnLeft();
delay(600);
}