Shop OBEX P1 Docs P2 Docs Learn Events
Code for IR with Ping = help please — Parallax Forums

Code for IR with Ping = help please

iotaherculisiotaherculis Posts: 11
edited 2015-02-08 19:50 in Robotics
I am having issues getting all of this to work. I am a super nub.
#include "simpletools.h"
#include "mstimer.h"
#include "simpletools.h" // Library includes
#include "abdrive.h"
#include "simpletools.h" // Include simpletools header
#include "abdrive.h" // Include abdrive header
#include "ping.h" // Include ping header



int irLeft, irRight;
int frontDist;
int ultraSoundPin = 8;
int irLeftPin = 10;
int irRightPin = 2;
int freqLeftPin = 11;
int freqRightPin = 1;

int main()
{
low(26);
low(27);
int direction=0;

/*
direction values:
0: nothing , turn right
1: walls on both sides, go back
2: wall on right side, go left
3: wall on left side, go right
*/

drive_setRampStep(12);
mstime_start();
frontDist = ping_cm(ultraSoundPin);
while(1) // main while loop
{
direction = directionalcalc(2);
while (frontDist >= 10)
{
drive_speed(55,55);
if(mstime_get() > 6000 && (ping_cm(8) < 10+3 || ping_cm(8) > 10-3))
{
drive_speed(0,0);
pause(50);
drive_speed(-55,-55);
pause(800);
drive_speed(-55,55);
pause(600);
}
}
while (ping_cm(8)<10)
{
turnCalc(direction);
int directionCalc(int pinLeft, int pinRight , int freqLeft, int freqRight)
// Inputs: pin numbers for IR recievers and lights.
/*
This function is used for setting the variable direction,
based on what side the walls are
*/
{
freqout(freqLeft, 1, 38000);
irLeft = input(pinLeft);

freqout(freqRight, 1, 38000);
irRight = input(pinRight);

if(irLeft == 1 && irRight == 1)
{
return 0;
}
else if(irLeft == 0 && irRight == 0)
{
return 1;
}
else if(irLeft == 0 && irRight == 1)
{
return 2;
}
else if(irLeft == 0 && irRight == 1)
{
return 3;
}
}
void turnCalc(int _direction);
/*
This function calculates the direction the robot should turn in a corner,
based on where there is a wall
*/
{
switch(direction)
{
case 0:
drive_speed(55,-55);
pause(80);
break;
case 1:
drive_speed(-55,-55);
pause(900);
break;
case 2:
drive_speed(-55,55);
pause(80);
break;
case 3:
drive_speed(55,-55);
pause(80);
break;
}}}}}

Comments

  • iotaherculisiotaherculis Posts: 11
    edited 2015-02-08 19:50
    This was a fail.
    I think I have it worked out. Do not use the code above. its a turd
Sign In or Register to comment.