Hello im newbie and I come from indonesia
Hello I am new to arduino but I am interested with how to build robot, .. I have made 2 robot using arduino unoR3 but when I buy an robot chassis car from dagu that uses steering wheels in front of car with two dc motors ( one for steering with skb-220 pot and one more for move forward and move backwards ) with micro magician robot controller using atmega168 I confused to program this robot controller to be able to run well with my chassis car.
I had great difficulty in programming how to control the dc motor with potentiometer especially in controlling the position of dc motors, how dc motors that move to the right, center and left.and then I have asked the Mr Russell Cameron and I get code like this:
car impact and edge detection
#include "Constants.h"
#include "IOpins.h"
// define global variables
unsigned long time,IRtimer;
//int q1,q2,q3,q4,qt;
int qt;
int steer;
int angle;
int impact,impv,impc;
int c,av[avn*2];
int x,xav,X;
int y,yav,Y;
int motor;
byte IR;
void setup()
{
delay(500);
for(int i=0;i<100;i++)
{
impactdetection();
}
pinMode(IRleds,OUTPUT);
Serial.begin(9600);
IRtimer=micros();
}
void loop()
{
//linesensor();
impactdetection();
impactresponse();
steering();
}
void impactdetection()
{
// array "av" use to store values for averaging
// constant "avn" is the number of values to be averaged
// instant angle reading stored as "ang"
// instant impact reading stored as "imp"
//-------------------------------------------------------- Filter sensor data to reduce noise ------------------------------------
av[c]=analogRead(0)-537; // read accelerometer x-axis
av[c+avn]=analogRead(1)-583; // read accelerometer y-axis
c++; // increment averaging array index
if(c>(avn-1)) c=0; // reset index
for(int i=0;i<avn;i++) // average x-axis and y-axis values to filter noise
{
x+=av[i]; // sum x-axis values
y+=av[avn+i]; // sum y-axis values
}
x/=avn; // x=mean x-axis
y/=avn; // y=mean y-axis
int oi=impv; // store previous impact value prior to update
int ang=int(atan2(double(y),double(x))*180/PI); // direction of impact: left=90 degrees - forward=0 degrees - right=-90 degrees
impv=sqrt(sq(x)+sq(y)); // magnitude of impact: hypotenuse of x and y values
//---------------------------------------------------------- due to body vibration only initial impact values useful ----------------
int differential=oi-impact; // differential = old impact value - current impact value
if(differential>imp) // compare previous average to present value
{
digitalWrite(13,1);
if(impc==0) time=millis();
impc++; // increment impact counter if impact detected
if (impc==impsn) // impact=impsn value, later values discarded due to vibration
{
impact=impv;
angle=ang;
X=x;
Y=y;
Serial.print("X:");
Serial.print(x);
Serial.print("\t Y:");
Serial.print(y);
Serial.print("\t Impact:");
Serial.print(impact);
Serial.print("\t Angle:");
Serial.println(angle);
}
}
else
{
if(millis()-time>200)
{
digitalWrite(13,0);
impc=0; // reset impact counter when vibration subsides
}
}
}
void impactresponse()
{
if (impact>0)
{
if(millis()-time<500) // stop for 500mS after impact
{
analogWrite(steerA,0); // turn off steering motor
analogWrite(steerB,0);
analogWrite(motorA,255); // engage electronic braking
analogWrite(motorB,255);
motor=0; // reset motor speed
steer=0;
}
else // time greater than 500mS
{
if(millis()-time<2500) // from 500-2300mS after impact
{
if(X>0) // increment motor speed to move away from object
{
motor--;
if (motor<-220) motor=-220;
}
else
{
motor++;
if(motor>motormax) motor=motormax;
}
if(Y<0) // increment steering motor speed to move away from object
{
steer-=4;
if (steer<-200) steer=-200;
}
else
{
steer+=4;
if(steer>200) steer=200;
}
if(millis()-time>2200 && motor<0) motor=0;
}
else // time from impact greater than 2000mS
{
impact=0; // reset impact response
}
}
}
else
{
if(steer>3) steer-=4;
if(steer<-3) steer+=2;
if(motor<motormax) motor++;
}
if (motor>0)
{
analogWrite(motorA,motor);
analogWrite(motorB,0);
}
else
{
analogWrite(motorA,0);
analogWrite(motorB,-motor);
}
}
void steering()
{
int steering=analogRead(steerP);
int steerspeed=(steer+512-steering)*3;
if (steering<spright && steerspeed<0) steerspeed=0;
if (steering>spleft && steerspeed>0) steerspeed=0;
if (steerspeed>steermax) steerspeed=steermax;
if (steerspeed<-steermax) steerspeed=-steermax;
//Serial.print("Steering pot:"); Serial.print(steering);
//Serial.print("\t Steer speed:"); Serial.println(steerspeed);
if (steerspeed>0)
{
analogWrite(steerA,steerspeed);
analogWrite(steerB,0);
}
else
{
analogWrite(steerA,0);
analogWrite(steerB,-steerspeed);
}
}
int q1,q2,q3,q4,t,m;
void linesensor()
{
if(m==2)
{
digitalWrite(IRleds,1);
IRtimer=millis();
m=0;
return;
}
t=int(millis()-IRtimer);
if(t>2 && m==0)
{
q1=analogRead(sensQ1);
q2=analogRead(sensQ2);
q3=analogRead(sensQ3);
q4=analogRead(sensQ4);
digitalWrite(IRleds,0);
m=1;
return;
}
if(t>4 && m==1)
{
q1-=analogRead(sensQ1);
q2-=analogRead(sensQ2);
q3-=analogRead(sensQ3);
q4-=analogRead(sensQ4);
qt=q1+q2+q3+q4;
m=2;
}
}
with constant.h like this :
#define steermax 250 // maximum PWM value for steering motor
#define motormax 180 // maximum PWM value for drive motor
#define spright 295 // steering position minimum value
#define spleft 750 // steering position maximum value
#define avn 35 // number of samples for averaging
#define imp 35 // minimum impact value
#define impsn 1 // impact sample number
and IOPins.h like this :
#define motorA 3 // PWM output - drive motor pin A
#define motorB 11 // PWM output - drive motor pin B
#define steerA 5 // PWM output - steer motor pin A
#define steerB 6 // PWM output - steer motor pin B
#define steerP 7 // Analog input - steer position pot
#define IRleds 2 // Digital output - IR LEDs
#define sensQ1 6 // Analog input - photo transistor 1
#define sensQ2 5 // Analog input - photo transistor 2
#define sensQ3 4 // Analog input - photo transistor 3
#define sensQ4 3 // Analog input - photo transistor 4
#define Xaxis 0 // Analog input - accellerometer X
#define Yaxis 1 // Analog input - accellerometer Y
#define Zaxis 2 // Analog input - accellerometer Z
robot runs fine but when the robot hit something, the robot is just go ahead and just turn left, actually i want the robot can moves backward and steering front wheels turned to the left or to the right then stop and move forward when he hits something.
Is there anyone here who has ever tried to make this robot, ... can share your knowledge to me and teach me how to make this robot.
I want my robot can moves like this site http://www.youtube.com/watch?v=bd1FIBiKBTY and my robot just move like this http://www.youtube.com/watch?v=5KXwgajWRAc&feature=youtu.be
please help me to programmed this microcontroller
forgive me if my english is not good I hope someone can help me in this forum to solve my problem
THANKSS