Stap 3: De code
/** * Mini Crossbow controlled by joystick shield v2.4 * by RAS IEEE UFABC - 11/07/2015 */
#include <Servo.h><br><br>Servo servo_hor; Servo servo_vert; Servo servo_trig;<br> #define LOG Serial.print #define bt_change 4 #define bt_fire 2 #define led_log 13 #define servo_trig_start 90 #define servo_trig_end 0 #define analog_x 0 #define analog_y 1 #define time_shoot 700 #define time_execution 0 unsigned long running_time = 0;<br>boolean open_fire = 0; int status_ = 2; int bow_pos[2] = {90, 125}; // {servo_hor, servo_vert}
void setup() { pinMode(analog_x, INPUT_PULLUP); <br> pinMode(analog_y, INPUT_PULLUP); pinMode(bt_change, INPUT); pinMode(bt_fire, INPUT); Serial.begin(115200); servo_hor.attach(6); servo_vert.attach(5); servo_trig.attach(3); servo_hor.write(90); delay(300); servo_vert.write(125); delay(300); servo_trig.write(servo_trig_start); delay(300); }
void loop() { if (!digitalRead(bt_change)) { (status_ >= 2) ? status_ = 0 : status_++; digitalWrite(led_log, 1); delay(50); digitalWrite(led_log, 0); delay(50); digitalWrite(led_log, 1); delay(50); digitalWrite(led_log, 0); delay(50); } switch (status_) { case 0: // servo horizontal adjust bow_pos[0] = analogRead(analog_x); bow_pos[0] = map(bow_pos[0], 0, 1023, 0, 179); break; case 1: // servo vertical adjust bow_pos[1] = analogRead(analog_y); bow_pos[1] = map(bow_pos[1], 0, 1023, 100, 150); break; default: // stoped if (!digitalRead(bt_fire)) { open_fire = true; delay(150); } break; } if (millis() - running_time > time_execution) { servo_hor.write(bow_pos[0]); servo_vert.write(bow_pos[1]); if (open_fire) { servo_trig.write(servo_trig_end); delay(time_shoot); servo_trig.write(servo_trig_start); delay(time_shoot); open_fire = !open_fire; } running_time = millis();<br> }<br> LOG("EixoX -> motor: "); LOG(bow_pos[0]); LOG("\n"); LOG("EixoY -> Servo: "); LOG(bow_pos[1]); LOG("\n"); LOG("Button Change: "); LOG(status_); LOG("\n"); LOG ("Button Fire: "); LOG(open_fire); LOG("\n"); }