View Single Post
  #16   Spotlight this post!  
Unread 16-01-2013, 20:11
mrklempae mrklempae is offline
Registered User
no team
 
Join Date: Jan 2013
Location: Montana
Posts: 19
mrklempae is an unknown quantity at this point
Re: Programming ultrasonic rangefinders

I tried using the code you provided, and it compiled. My next problem is printing the distance to the Driver Station in real time to prove the code and/or our sensor works. We copied and pasted the .cpp ad .h file into the BuiltInDefaultCode, then copied your code into the main .cpp file. We made sure the robot was correctly wired. We tried changing the code a little to get data prited, but couldn't print any data (or at least not the right data).
Code:
#include "AnalogRangeFinder.h"
#include <WPILib.h>
#define ANALOG_CHANNEL_FOR_SONAR 1

class Robot2012 : public IterativeRobot
{
   AnalogRangeFinder sonar;
   public:
   Robot2012(void):sonar(ANALOG_CHANNEL_FOR_SONAR)
   {
   }

   void AutonomousPeriodic(void)
   {
   }
	void OperatorControl(void)
	{
		DriverStationLCD *dsLCD = DriverStationLCD::GetInstance();
		float distance = sonar.GetRangeInches();
		while (IsOperatorControl())
		{
			dsLCD->PrintfLine(DriverStationLCD::kUser_Line1, "My Value is: %d", distance);
			dsLCD->UpdateLCD();
			Wait(0.1);
		}}}
}

START_ROBOT_CLASS(Robot2012);
I'm really sorry about this, but I just threw together the code, untested. I don't currently have access to the code we used. Does anyone know how to do this? We tried different things for two hours or so to no avail. We returned data one time (something like 22354698), which I'm pretty sure is WAY out of the sensors range. P.S, ScottW, THANKS! Once we get this part working, that information will be perfect!
Reply With Quote