I am trying to remove my IR sensor code and add in line follower code. Rather than show you the mess I came up with, I will link you to all the pieces and maybe someone can help me piece it together correctly? I can’t seem to get my motors to come on whenever I upload it.
I’m using this library for my Wicked Device motor shield
I’m using three of these line followers and they are attached to A2, A3, and A4
RedBot Library link:
And this is the code i have for the IR sensor and two motors. Like I said, I’m looking to remove the IR sensors and add three line followers.
Thank you very much for your help.
#include <Wicked_DCMotor.h>
void printCurrentSensing(void);
int num_motors = 2;
Wicked_DCMotor motor1(M1);
Wicked_DCMotor motor2(M2);
Wicked_DCMotor *m[] = {&motor1, &motor2};
int sensorPin=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.print(F("Wicked Motor Shield Library version "));
Serial.print(WickedMotorShield::version());
Serial.println(F("- DC Motors"));
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
for(int ii = 0; ii < num_motors; ii++){
Serial.println(F("================================"));
int val=analogRead(sensorPin);
Serial.println(val);
if(val<190) {
m[ii]->setDirection(DIR_CW); // clockwise
m[ii]->setBrake(BRAKE_OFF); // no brake applied
Serial.print(F("Turning on Motor M"));
Serial.print(ii+1);
Serial.println(F(", Full Speed, Clockwise"));
m[ii]->setSpeed(255); // full speed
}
if(val>190) {
m[ii]->setDirection(DIR_CCW); // counter clockwise
m[ii]->setBrake(BRAKE_OFF); // no brake applied
Serial.print(F("Turning on Motor M"));
Serial.print(ii+1);
Serial.println(F(", Full Speed, Counter Clockwise"));
m[ii]->setSpeed(255); // full speed
delay(2000);
}
}
}
for(int ii = 0; ii < num_motors; ii++){
Serial.println(F("================================"));
int val=analogRead(sensorPin);
Why is reading the sensor pin (what IS connected to it?) dependent on the number of motors?
Why is there a delay() if the value is greater than 190, and no delay() if the value is less than 190? Where is there nothing for a value of 190? Are you ever getting a value greater than 190?
The delay at the value greater than 190 is so that it can turn in it's spot instead of turning while moving. Didn't see a need for that when the value goes back under 190. There is no =190 b/c I couldn't get it to work. I don't actually need help with the IR sensor, that can all be deleted, I just need line sensor code to work with my motor code.
When you post links use the chain icon in the tool bar to make them clickable.
xsamarax:
There is a link for the RedBot library on this page:
Why not post that link here instead of forcing everyone to go to the sparkfun product page and hunt for the link?
xsamarax:
And this is the code i have for the IR sensor and two motors.
I’ve already asked you to use code tags when you post code. There is also a sticky post in every forum section How to use this forum - please read. that clearly you didn’t bother to read. This shows a lack of respect for this forum
I'm trying to be as polite as I possibly can. I wasn't able to make the links hyper links. I didn't put a direct link to the RedBot library b/c when you click on it, it opens the library immediately. I'm not sure how much more polite I need to be, I promise I'm doing the very best I can to make this easy for those who are willing to help me out. I'm in a bind, I need this for my grade. I need help. I did actually read it, please don't make assumptions. I know how these forums work, there is always someone ready to jump down your throat for making tiny mistakes.
xsamarax:
I know how these forums work, there is always someone ready to jump down your throat for making tiny mistakes.
Read my previous request:
pert:
Please use code tags(</> button on the toolbar) when you post code or error/warning messages.
I think I was very polite and I helped you find the solution to your problem on that post. I understand on someone’s first post they might not understand how to do things right so I don’t get upset about it but when you do the same thing on the next post it makes me think you just don’t care.
Yes! I want to adapt the code but I have been unsuccessful. I tried adding my motors in but they never work. I did run a serial monitor on the sensors though and they do work, thankfully.
The Exp6_2_LineFollowing_IRSensors is closer to what you need than the code you posted above so start with that example and then just replace the lines that are specific to the RedBot motors with the code to control the Wicked Device motor shield:
I’ll give you a little help to get you started, add at the top of the sketch:
#include <Wicked_DCMotor.h>
This allows you to access the functions of the Wicked_DCMotor library. After that you only need to replace the following lines with their Wicked_DCMotor library equivalents: Line 34(of the original example):
Thanks! Well this is what I have so far. It’s getting the line sensors in the if/else statements where I’m having trouble. I’m getting a “exit status 1 expected ‘,’ or ‘;’ before ‘=’ token” error code and it highlights “Wicked_DCMotor motor2(M2) = SPEED;” as the problem line.
#include <RedBot.h>
#define LINETHRESHOLD 800
#define SPEED 60
RedBotSensor left = RedBotSensor(A2); // initialize a sensor object on A2
RedBotSensor center = RedBotSensor(A3); // initialize a sensor object on A3
RedBotSensor right = RedBotSensor(A4); // initialize a sensor object on A4
#include <Wicked_DCMotor.h>
int num_motors = 2;
Wicked_DCMotor motor1(M1);
Wicked_DCMotor motor2(M2);
Wicked_DCMotor *m[] = {&motor1, &motor2};
int sensorPin=0;
void setup()
{
Serial.begin(9600);
Serial.println("Welcome to experiment 6!");
Serial.println("------------------------");
Serial.begin(115200);
Serial.print(F("Wicked Motor Shield Library version "));
Serial.print(WickedMotorShield::version());
Serial.println(F("- DC Motors"));
}
void loop()
{
for(int ii = 0; ii < num_motors; ii++){
Serial.println(F("================================"));
Serial.print("IR Sensor Readings: ");
Serial.print(left.read());
Serial.print("\t"); // tab character
Serial.print(center.read());
Serial.print("\t"); // tab character
Serial.print(right.read());
Serial.println();
delay(100);
if(center.read() > LINETHRESHOLD)
{
Wicked_DCMotor motor1(M1) = -SPEED;
Wicked_DCMotor motor2(M2) = SPEED;
}
}
}
Is this what I'm looking for? m[ii]->setSpeed(255);
I'm really trying. You're right, I don't fully understand it. Due to back orders of kits and wrong parts, I wasn't given much time to do this. And it's 75% of my grade. It took forever just to put it together physically b/c it kept falling apart. I had to splice crap together b/c I still don't have the correct types of wires. It's a huge mess overall. And now...I have a sad story and I'm out of time. I'm working b/t my IR sensor code, the wicked shield DC motor code, and the example line follower code to try to make it fit.
xsamarax:
Is this what I'm looking for? m[ii]->setSpeed(255);
Yes, now the good news is that both libraries use 255 as the maximum speed setting but if you look at the RedBot reference:
.leftMotor(motorPower) – controls the leftMotor independantly. Positive values of motorPower spin the motor CW, and negative values spin the motor CCW.
Whereas if you look at the WickedMotorShield library DC_Motor example you'll see that it controls motor direction like this:
m[ii]->setDirection(DIR_CW); // clockwise
So the range of speed values motors.leftMotor() and motors.rightMotor() will accept is -255 - 255 but the range of speed values setSpeed() will accept is 0 - 255 so you will need to account for that difference in your code.
Ok. I see what you're saying about the motor speeds. m[ii]->setSpeed(255); seems to control both motors and in my code I need them to operate separately, like in the RedBot code. My brain is toast right now, I have the final for this class tomorrow and the robot is due. I'll have to take a look at it tomorrow and try some more. Thanks for your help.
Of course I don’t know which motor is which on your robot so I might have them backwards but you get the idea. After doing that if you want to set the speed of the left motor to 255 you can do:
m[leftMotorID]->setSpeed(255);
instead of
m[0]->setSpeed(255);
It accomplishes the same thing but is more readable.
xsamarax:
I need them to operate separately, like in the RedBot code.
Yes, by controlling the speed of each motor you can steer your robot to make it stay on the line. If the right motor is set to a higher speed than the left motor the robot turns left.
You still have not explained why reading the sensors is dependent on the number of motors. There are some things that need to be in the for loop that iterates over the motor, and some things that do NOT belong in that loop. You need to understand which is which.