Hey guys ( and gals if there are any on here ). I just wanted to post a short vid of my robot. I am having problems with the photo-resistors telling the robot how to turn ( actually it is probably a problem with my programming) so it only goes straight - but baby steps i guess-
here is my code if anyone can help me. I started to read about programming in c, but as yo know it's not something you can learn in a day. I am using project designer to get me started, and cutting and pasting from the photovore file in the tutorial, so i am sure the code could be cleaned up a bit.
#include "hardware.h"
// Initialise the hardware
void appInitHardware(void) {
initHardware();
}
// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
return 0;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
int threshold=8;//the larger this number, the more likely your robot will drive straight
// -------- Start Switch/Button-------
// Switch/Button - see switch.h
// To test if it is pressed then
if(SWITCH_pressed(&button)){
// pressed
}
// To test if it is released then
if(SWITCH_released(&button)){
// released
}
// -------- End Switch/Button-------
// -------- Start Marquee-------
// Marquee - see 'segled.h'
// Before using the Marquee you need to redirect rprintf to write to it
// This can be done using
Writer old = rprintfInit(marqueeGetWriter(&marquee));
// All rprintf output will then be sent to the marquee but will not
// display until an end-of-line eg "\n" has been sent. Example:-
// rprintf("Hello World\n");
// If the endDelay is non-zero then the marquee will scroll
// forever or until you call: marqueeStop(&marquee);
// If the endDelay is zero then the marquee will stop once
// the entire line has been shown ('one-shot' mode)
// In 'one-shot' mode then you may want to make sure that
// a previous line has finished before you display a second line.
// This can be done as follows:-
marqueeSetEndDelay(&marquee,0); // Make sure we are in one-shot mode
if(marqueeIsActive(&marquee)==FALSE){
if(loopCount==1){
rprintf("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
}else{
rprintf("Loop=%u\n",(unsigned)loopCount); // Put the loop count
}
}
// Restore rprintf back to its previous location
rprintfInit(old);
// -------- End Marquee-------
// -------- Start Analogue Input-------
// Read the Analogue Input and store results
uint16_t photoresistor_rightval = a2dConvert10bit(photoresistor_right);
// Dump out the value
rprintf("photoresistor_right: %d\n",photoresistor_rightval);
// -------- End Analogue Input-------
// -------- Start Analogue Input-------
// Read the Analogue Input and store results
uint16_t Photoresistor_leftval = a2dConvert10bit(Photoresistor_left);
// Dump out the value
rprintf("Photoresistor_left: %d\n",Photoresistor_leftval);
// -------- End Analogue Input-------
// -------- Start Actuators -------
// To control your.motors/servos then see actuators.h in the manual // To retrieve the required speed of servoleft use:
// DRIVE_SPEED speed=act_getSpeed(servoleft);
// To set the required speed of servoleft use:
// act_setSpeed(servoleft,speed);
// This example will move the motors back and forth using the loopStart time:
if(Photoresistor_left > photoresistor_right && (Photoresistor_left - photoresistor_right) > threshold)
{
// go left
act_setSpeed(&servoleft,DRIVE_SPEED_MIN);
act_setSpeed(&servoright,DRIVE_SPEED_MAX);
}
else if(photoresistor_right > Photoresistor_left && (photoresistor_right - Photoresistor_left) > threshold)
{
// go right
act_setSpeed(&servoleft,DRIVE_SPEED_MAX);
act_setSpeed(&servoright,DRIVE_SPEED_MIN);
}
else
{
// Go forwards
act_setSpeed(&servoleft,DRIVE_SPEED_MAX);
act_setSpeed(&servoright,DRIVE_SPEED_MAX);
}
//print out detected difference
rprintf("Sensor Difference: %d\n", Photoresistor_left - photoresistor_right);
delay_ms(30);//slow control loop down to prevent crazy robot oscillation
return 20000; // wait for 20ms to stop crazy oscillations (long story)
}
// -------- End Actuators -------