Hello, I wrote a program in EasyC, it works fine, but I don’t like the drag and drop interface. So, after much searching, I found this forum thread. The guy wrote a makefile to convert a .c file to a .hex file. I use the IFI Loader to load that into the microcontroller.
Everything works fine… I have it so the car doesn’t start until a bumper is pressed and that works fine. Now, I also have a optical shaft encoder on one of the axles. I am having trouble getting the value of this shaft encoder.
At the top, where all the other stuff is defined, I added
#define SHAFT_ENCODER 2
The #define code for my bumper is
#define BUMPER_BUTTON 1
That works fine. The bumper is in port 1 of the Analog/Digital ports.
I have tried the shaft encoder in the Analog/Digital ports as well as the Interrupts. I think its supposed to be in the interrupts, but I don’t know how to do the #define SHAFT_ENCODER for it.
I have it print out the shaft encoder’s value in a while loop and it always prints out 0.
Below is my full C code.
#include "UserAPI.h"
#define RIGHT_MOTOR 1
#define LEFT_MOTOR 2
#define BUMPER_BUTTON 1
#define SHAFT_ENCODER 2 // WHAT NUMBER SHOULD THAT BE???
// Define IO port configuration - 0 is OUTPUT, 1 is INPUT
void IO_Initialization(void)
{
DefineControllerIO ( 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 ) ;
}
// Symbols for our inputs and outputs.
void setMotorSpeed(int speed, char motor)
{
if(motor == RIGHT_MOTOR)
SetMotor(motor, 127 + speed);
else if(motor == LEFT_MOTOR)
SetMotor(motor, 127 - speed);
}
void main( void )
{
int newSpeed = 127;
int encoderValue = 0;
int currentTime = 0;
while(GetDigitalInput(BUMPER_BUTTON) == 1);
StartTimer(1);
StartEncoder(SHAFT_ENCODER);
setMotorSpeed(127, RIGHT_MOTOR);
setMotorSpeed(127, LEFT_MOTOR);
while(newSpeed > 0)
{
encoderValue = GetEncoder(SHAFT_ENCODER);
PrintToScreen("Encoder: %d\n", encoderValue); // THIS ALWAYS JUST PRINTS OUT ENCODER: 0
currentTime = GetTimer(1) / 1000;
PrintToScreen("TIMER: %d\n", currentTime);
setMotorSpeed(newSpeed, RIGHT_MOTOR);
setMotorSpeed(newSpeed, LEFT_MOTOR);
newSpeed -= 5;
}
setMotorSpeed(0, RIGHT_MOTOR);
setMotorSpeed(0, LEFT_MOTOR);
}