#include // The Arduino is ready to receive a new command boolean bReadyForCommand = true; // PWM command margin - this is to handle the variablility of the PWM reading int pwmInputVariabilityMargin = 10; // Servos Servo leftServo; // Left Target Servo Servo centerServo; // Center Target Servo Servo rightServo; // Right Targer Servo // Command Values for PWM int commandProgram1Value = 540; // Test program int commandProgram2Value = 640; // Succession Program (left for 2 sec, center for 2 sec, right for 2 sec) int commandProgram3Value = 740; // Random for 2 seconds int commandProgram4Value = 840; // Random 10 second delay between targets int HIDE_TARGET_SERVO_POS = 0; int SHOW_TARGET_SERVO_POS = 90; /* * PIN CONFIGURATION */ // This is the output pin communicating to the ioBridge that the command has been processed int debugioBridgeInputReadyPin = 13; int ioBridgeInputReadyPin = 12; // PWM Servo Outputs - These are the pinouts int targetLeftPWMOutput= 11; int targetCenterPWMOutput= 10; int targetRightPWMOutput= 9; // This is the analog input from which the Arduino will receive commands from the IO Bridge int ioBridgeCommandInputPin = 8; // This is the input pin informing the arduino that there is a command ready to be processed int ioBridgeCommandReady = 7; // Peizo Speaker Pin int peizoOutputPin = 2; /* * initializePinouts() * Initialize the Arduino pins that will be used for communication with the IO Bridge * * @param none * @return none */ void initializePinouts() { // Inputs pinMode(ioBridgeCommandInputPin, INPUT); pinMode(ioBridgeCommandReady, INPUT); // Outputs pinMode(ioBridgeInputReadyPin, OUTPUT); pinMode(debugioBridgeInputReadyPin, OUTPUT); pinMode(targetLeftPWMOutput, OUTPUT); pinMode(targetCenterPWMOutput, OUTPUT); pinMode(targetRightPWMOutput, OUTPUT); pinMode(peizoOutputPin, OUTPUT); } /* * initializeCommands() * Initialize the commands that the Arduino will support coming in from the IO Bridge * * @param none * @return none */ void initializeCommands() { } /* * initializeServoPositions() * Start the servos in a known state * * @param None * @return None */ void initializeServoPositions() { // Set servos to hide state leftServo.write(HIDE_TARGET_SERVO_POS); centerServo.write(HIDE_TARGET_SERVO_POS); rightServo.write(HIDE_TARGET_SERVO_POS); pulseDelay(1000); } /* * pulseDelay() * This method will effectively be a delay method, but should be used instead of the delay because the * software servo library requires a refesh every 50 ms. This should be in increments of 25ms * @param duration - The time in ms to wait for */ void pulseDelay(int duration) { // Calculate how many iterations we need int iterations = duration / 25; // Call refesh every 25 ms (2x what is needed just in case) for (int x=0; x= (commandProgram1Value - pwmInputVariabilityMargin)) && (commandValue <= (commandProgram1Value + pwmInputVariabilityMargin))) { commandProgram1(); } // Check for command value 2 else if ((commandValue >= (commandProgram2Value - pwmInputVariabilityMargin)) && (commandValue <= (commandProgram2Value + pwmInputVariabilityMargin))) { commandProgram2(); } // Check for command value 3 else if ((commandValue >= (commandProgram3Value - pwmInputVariabilityMargin)) && (commandValue <= (commandProgram3Value + pwmInputVariabilityMargin))) { commandProgram3(); } // Check for command value 4 else if ((commandValue >= (commandProgram4Value - pwmInputVariabilityMargin)) && (commandValue <= (commandProgram4Value + pwmInputVariabilityMargin))) { commandProgram4(); } else { // @DEBUG Serial.print("Unknown command: "); Serial.println(commandValue); } } /* * setup() * Setup function for the Arduino board (mandatory function) * * @param none * @return none */ void setup() { // @DEBUG Serial.begin(9600); // Seed the random number generator randomSeed(analogRead(1)); // initialize necessary pins initializePinouts(); // set initial state to "ready for command" updateArduinoReadyState(true); // Set the positions for the servos (550 = 0 degrees, 1450 = center, 2300 = 180) leftServo.attach(targetLeftPWMOutput); //leftServo.setMaximumPulse(2300); //leftServo.setMinimumPulse(550); centerServo.attach(targetCenterPWMOutput); //centerServo.setMaximumPulse(2300); //centerServo.setMinimumPulse(550); rightServo.attach(targetRightPWMOutput); //rightServo.setMaximumPulse(2300); //rightServo.setMinimumPulse(550); // put the Servos in a "not shown" state initializeServoPositions(); } // end setup() /* * updateArduinoReadyState() * Update the value of the Input Ready pin * * @param commandReady - True to set pin high, False to set pin low * @return none */ void updateArduinoReadyState(boolean inputReady) { // Update the arduino state bReadyForCommand = inputReady; if (bReadyForCommand == true) { // @DEBUG Serial.println("Arduino: Ready for command..."); digitalWrite(debugioBridgeInputReadyPin, HIGH); digitalWrite(ioBridgeInputReadyPin, HIGH); } else { // @DEBUG Serial.println("Arduino: Processing command..."); digitalWrite(debugioBridgeInputReadyPin, LOW); digitalWrite(ioBridgeInputReadyPin, LOW); } } // end updateInputReadyPin() /* * loop() * Main execution loop of the Arduino processor (mandatory function) * * @param none * @return none */ void loop() { int commandValue = 0; int commandWaiting; boolean bCommandWaiting = false; Servo::refresh(); // We are ready to process a new command if (bReadyForCommand == true) { // Read the command pin from the ioBridge to see if it wants to send a command if (digitalRead(ioBridgeCommandReady) == HIGH) { // @DEBUG Serial.println("Command waiting..."); bCommandWaiting = true; } // end if else { bCommandWaiting = false; } // end else // There was a command waiting for processing if (bCommandWaiting == true) { // The arduino is processing a command, we will not process another updateArduinoReadyState(false); // Read the command from the input lines from the IO Bridge // @DEBUG Serial.println("Reading PWM value from ioBridge..."); commandValue = pulseIn(ioBridgeCommandInputPin, HIGH); // @DEBUG Serial.print("Read Value from ioBridge: "); Serial.println(commandValue); // Lookup and process the command from the ioBridge decodeCommand(commandValue); } // end if } // end if else { // We will not declare that the arduino is ready for another command until the ioBridge pulls its command bit low if (digitalRead(ioBridgeCommandReady) == LOW) { // Ok, we are ready to receive a new command updateArduinoReadyState(true); } } } // end loop()