Beruflich Dokumente
Kultur Dokumente
h>
/******************************************************************
*
Definition of variables
*
******************************************************************/
/* Pin definition of the board to be used */
#define pinLeftWheel
6
#define pinRightWheel
9
#define pinClaw
11
#define pinSensorIRLeft
2 /* Left infrared sensor */
#define pinSensorIRRight
3 /* Right infrared sensor */
#define pinSensorLDRLeft
A2 /* Left light sensor
*/
#define pinSensorLDRRight
A3 /* Right light sensor
*/
/* Definition of the values that can take continuous rotation servo,
that is, the wheels */
#define wheelStopValue
90
#define leftWheelFordwardValue 0
#define leftWheelBackwardsValue 180
#define rightWheelFordwardValue 180
#define rightWheelBackwardsValue 0
/* Bauderate of the Bluetooth*/
#define MI_PRIMER_KIT_DE_ROBOTICA_BLUETOOTH
#define BQ_ZUM_BLUETOOTH
19200
38400
10
} else {
leftWheel.write(leftWheelFordwardValue);
delay(defaultDelay);
}
}
/******************************************************************
*
Setup
*
******************************************************************/
void setup() {
/* Open the Bluetooth Serial and empty it */
//Serial.begin(BQ_ZUM_BLUETOOTH);
Serial.begin(MI_PRIMER_KIT_DE_ROBOTICA_BLUETOOTH);
Serial.flush();
/* Define the appropiate pin to each object */
leftWheel.attach(pinLeftWheel);
rightWheel.attach(pinRightWheel);
claw.attach(pinClaw);
/* Put the IR sensors as input */
pinMode(pinSensorIRLeft, INPUT);
pinMode(pinSensorIRRight, INPUT);
/* The robot is stopped at the beginning */
stopWheels();
/* Put the claw in a intermediate position at the beginning */
posClaw = 30;
moveClaw();
currentState = MANUAL_CONTROL_STATE;
lightLimitValue = 200;
}
/******************************************************************
*
Main program loop
*
******************************************************************/
void loop() {
/* If there is something in the Bluetooth serial port */
if (Serial.available() > 0) {
/* Reset the iterator and clear the buffer */
i = 0;
memset(dataBuffer, 0, sizeof(dataBuffer));
/* Wait for let the buffer fills up. Depends on the length of
the data, 1 ms for each character more or less */
delay(bufferSize);
/* Number of characters availables in the Bluetooth Serial */
numChar = Serial.available();
/* Limit the number of characters that will be read from the
Serial to avoid reading more than the size of the buffer */
if (numChar > bufferSize) {
numChar = bufferSize;
}
/* Read the Bluetooth Serial and store it in the buffer*/
while (numChar--) {
dataBuffer[i++] = Serial.read();
/* As data trickles in from your serial port you are
grabbing as much as you can, but then when it runs out
(as it will after a few bytes because the processor is
much faster than a 9600 baud device) you exit loop, which
then restarts, and resets i to zero, and someChar to an
empty array.So please be sure to keep this delay */
delay(3);
}
/* Manage the data */
checkData(dataBuffer);
}
if(currentState == LINE_FOLLOWER) {
followTheLine();
} else if(currentState == LIGHT_FOLLOWER) {
followTheLight();
}
}