/* * Author: Alberto Machado * Project: Palm Harvester * Year: Fall 2015 - Spring 2016 * Description: This is the code for the cutting mechanism arduino. The ports used for each motor * can be seen in the comments below. The user inputs used to move all these motors can also be found in * the comments and also in the final report. This code controls 3 DC motors and a Stepper Motor. */ #include RCSwitch mySwitch = RCSwitch(); // used for stepper phases const int one = 1; const int two = 2; const int three = 3; const int four = 4; const int five = 5; const int six = 6; const int seven = 7; const int eight = 8; // ports for stepper motor const int A = 11; //in1 -- Blue cable const int B = 5; //in3 -- Black cable const int not_A = 10; //in2 -- Red cable const int not_B = 4; //in4 -- Green cable const int enableA_Stepper = 13; const int enableB_Stepper = 12; const int Stepper_Delay = 100; // Delay between each Stepper motor step const int pitch_up = 6; // value received from transmitter to signal stepper motor to pitch up const int pitch_down = 7; // value received from transmitter to signal stepper motor to pitch down int state = one; // variable to know which state the stepper is in int counter = one; //counts the number of step cycles the stepper has done const int counter_limit = five; //the amount of cycles the stepper should go through in one press of the button // ports for in_out motor const int in_out_pwm = 9; const int in_out_dir = 8; // parameters for in_out motor const int in_out_pwm_cycle = 100; const int in_out_delay = 1000; const int in = 5; // value received from transmitter to signal motor to go in const int out = 3; // value received from transmitter to signal motor to go out // ports for left_right motor const int left_right_pwm = 6; const int left_right_dir = 7; // parameters for left_right motor const int left_right_pwm_cycle = 45; const int left_right_delay = 500; const int left = 2; // value received from transmitter to signal motor to go left const int right = 4; // value received from transmiter to signal motor to go right // ports for saw motor const int saw_pwm = 3; //const int saw_dir = 1; const int saw_pwm_cycle = 200; const int saw_on = 10; // value received from transmitter to signal saw to turn on const int saw_off =11; // value received from transmitter to signal saw to turn off void setup() { Serial.begin(15000); mySwitch.enableReceive(0); // Receiver pin 2 // Set all ports used to OUTPUT pinMode(A, OUTPUT); //blue cable pinMode(B, OUTPUT); // black cable pinMode(not_A, OUTPUT); //red cable pinMode(not_B, OUTPUT); // green cable pinMode(in_out_pwm, OUTPUT); pinMode(in_out_dir, OUTPUT); pinMode(left_right_pwm, OUTPUT); pinMode(left_right_dir, OUTPUT); pinMode(saw_pwm, OUTPUT); // pinMode(saw_dir, OUTPUT); pinMode(enableA_Stepper, OUTPUT); pinMode(enableB_Stepper, OUTPUT); digitalWrite(enableA_Stepper, HIGH); digitalWrite(enableB_Stepper, HIGH); // Set saw_dir to LOW since the direction doesn't matter // digitalWrite(saw_dir, LOW); analogWrite(saw_pwm, 0); // Assures saw is off analogWrite(left_right_pwm, 0); // Assures left_right motor is off analogWrite(in_out_pwm, 0); // Assures in_out motor is off // set Stepper to this certain state to avoid back drive digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(not_A, LOW); digitalWrite(not_B, LOW); } void loop() { if(mySwitch.available()) { int value = mySwitch.getReceivedValue(); if(value == pitch_up) { // Stepper motor rotates CW // Step sequence: A -> AB -> B -> notA_B -> notA -> notA_notB -> notB -> A_notB -> A if(state == one) { digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(not_A, LOW); digitalWrite(not_B, LOW); state = two; delay(Stepper_Delay); } else if(state == two) { digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(not_A, LOW); digitalWrite(not_B, LOW); state = three; delay(Stepper_Delay); } else if(state == three) { digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(not_A, LOW); digitalWrite(not_B, LOW); state = four; delay(Stepper_Delay); } else if(state == four) { digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(not_A, HIGH); digitalWrite(not_B, LOW); state = five; delay(Stepper_Delay); } else if(state == five) { digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(not_A, HIGH); digitalWrite(not_B, LOW); state = six; delay(Stepper_Delay); } else if(state == six) { digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(not_A, HIGH); digitalWrite(not_B, HIGH); state = seven; delay(Stepper_Delay); } else if(state == seven) { digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(not_A, LOW); digitalWrite(not_B, HIGH); state = eight; delay(Stepper_Delay); } else { if(counter >= counter_limit) // If gone through cycle 5 times, then stop state = eight; else { state = one; digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(not_A, LOW); digitalWrite(not_B, HIGH); counter++; } } } else if(value == pitch_down) { //Stepper motor rotates CCW // Step sequence: A -> A_notB -> notB -> notA_notB -> notA -> notA_B -> B -> AB -> A if(state == one) { digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(not_A, LOW); digitalWrite(not_B, LOW); state = two; delay(Stepper_Delay); } else if(state == two) { digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(not_A, LOW); digitalWrite(not_B, HIGH); state = three; delay(Stepper_Delay); } else if(state == three) { digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(not_A, LOW); digitalWrite(not_B, HIGH); state = four; delay(Stepper_Delay); } else if(state == four) { digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(not_A, HIGH); digitalWrite(not_B, HIGH); state = five; delay(Stepper_Delay); } else if(state == five) { digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(not_A, HIGH); digitalWrite(not_B, LOW); state = six; delay(Stepper_Delay); } else if(state == six) { digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(not_A, HIGH); digitalWrite(not_B, LOW); state = seven; delay(Stepper_Delay); } else if(state == seven) { digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(not_A, LOW); digitalWrite(not_B, LOW); state = eight; delay(Stepper_Delay); } else { if(counter >= counter_limit) // If gone through cycle 5 times, then stop state = eight; else { state = one; digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(not_A, LOW); digitalWrite(not_B, LOW); counter++; } } } else if(value == in) // When joystick pressed up { if(counter < two) { digitalWrite(in_out_dir, HIGH); // Direction of motor driver is changed to HIGH analogWrite(in_out_pwm, in_out_pwm_cycle); // Turn on in_out_pwm at given pwm cycle delay(in_out_delay); // Software delay in microseconds analogWrite(in_out_pwm, 0); // Turn off in_out motor counter++; } else analogWrite(in_out_pwm, 0); } else if(value == out) { if(counter < two) { digitalWrite(in_out_dir, LOW); // Direction of motor driver is changed to LOW analogWrite(in_out_pwm, in_out_pwm_cycle); // Turn on in_out_pwm at given pwm cycle delay(in_out_delay); // Software delay in microseconds analogWrite(in_out_pwm, 0); // Turn off in_out motor } else analogWrite(in_out_pwm, 0); } else if(value == left) // When joystick pressed to the left { if(counter < two) { digitalWrite(left_right_dir, HIGH); // Direction of motor driver is changed to HIGH analogWrite(left_right_pwm, left_right_pwm_cycle); // Turn on left_right_pwm at given pwm cycle delay(left_right_delay); // Software delay in microseconds analogWrite(left_right_pwm, 0); // Turn off left_right motor } else analogWrite(left_right_pwm, 0); } else if(value == right) // When joystick pressed to the right { if(counter < two) { digitalWrite(left_right_dir, LOW); // Direction of motor driver is changed to LOW analogWrite(left_right_pwm, left_right_pwm_cycle); // Turn on left_right_pwm at given pwm cycle delay(left_right_delay); // Software delay in microseconds analogWrite(left_right_pwm, 0); // Turn off left_right motor } else analogWrite(left_right_pwm, 0); } else if(value == saw_on) // When button E is pressed { analogWrite(saw_pwm, saw_pwm_cycle); // Turn saw on } else if(value == saw_off) // When button F is pressed { analogWrite(saw_pwm, 0); // Turn saw off } else //if none of these buttons are pressed or joystick is in middle { // Keeps stepper in state A so that it does not back drive digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(not_A, LOW); digitalWrite(not_B, LOW); analogWrite(in_out_pwm, 0); // Turn off in_out motor analogWrite(left_right_pwm, 0); // Turn off left_right motor state = one; // reset state variable to starting state counter = one; // reset counter variable to one // analogWrite(saw_pwm, 75); } } }