I know the title could be for anything. but i need help.
OK this is how it should work.
I made a dohicky to lower a ball and turn lights on at Midnight for you know when.
well for the most part it works. the switches work. the status LEDs work.
The motor will not spin. NOW i know you will ack is there a part broken .... NOPE i have a sketch that spins the motor fine.
the problem seems to be in the step().
The serial (DEBUG) shows the for loop not working. it just gives me 15
here is the full code. I need FRESH EYEs. I just cant see the problem.
// This workes.....
// Needs
// Kow many pulses to go 1 inch?:
// GEt MotorOnOffSW & DirectionSW to work together.
// move steper to move camera
// Watch MotorOnOffSW and DirectionSW for HIGH
// IF high change directon of stepper
// Pins
int sleepPin=7;
int dirPin= 8;
int stepperPin=9;
// LED setup
int DirectionLED = 02; // This LED is for displaying the direction.
int MortorStatusOnOff=06; // This LED is to show Motor On/Off
int LED1=13; // DEBUG LED
// Switches
int DirectionSW=12; // This is held low when HIGH it will change the direction of the Motor
int MotorOnOffSW=11; // This is held low when high it will turn off/on the motor
// Vars
int TheDelay= 90; // this delay controls the speed higher delay causes the motor to go slower (MAX 78)
int MotorOnOff = 0; // VAR to show the motor status on/off
int time = 0; //used to track button presses
int oldTime = 0; //used to track button presses
int steps = 0; // set how many pulses to send to the Motor
//int i = 0;
// *************************************************************************************
void setup () {
pinMode (dirPin, OUTPUT); // This pin is used for the direction of the motor
pinMode (stepperPin, OUTPUT); // This pin is used to move the motor in steps
pinMode(sleepPin,OUTPUT); // This pin is used to put the motor in sleep mode. Sleep is no power supplied to the motor. (it can spin by hand)
pinMode (LED1, OUTPUT); // LED to show NOT USED YET
pinMode (MortorStatusOnOff, OUTPUT); // LED to show the motor status on/off
pinMode (MotorOnOffSW, INPUT); // Switch to control on/off of motor. (limit switch for now)
pinMode (DirectionSW, INPUT); // Controls the direction of the motor. (used for testing and resiting position)
pinMode(DirectionLED,OUTPUT); // Led to show motor direction
int MotorOnOff = 1; // Set motor on
digitalWrite(MortorStatusOnOff, HIGH); // Turn on the Status LED
//digitalWrite(dirPin,HIGH); //
oldTime = millis(); // MotorOnOffSW/MotorOnOffSW De-bounce
//CHSW(); // Check Switches
//MotorOnOff = 1; // Turn motor on (
Serial.begin(9600); // DEBUG start the Serial port
} // END setup()
// *************************************************************************************
// *************************************************************************************
void step(boolean dir,int steps){ // spins the motor
//Serial.println("Steping"); // DEBUG
// if (MotorOnOff == 0 ) { // If MotorOnOff = 0 don't run the motor
dir=dir; // BLA
digitalWrite(sleepPin,HIGH); // Sleep pin held high to remove stress on motor
// for testing
Serial.println(steps); // DEBUG
//delay(50); // Pause for .05 Seconds
BlinkLED1(); // For DEBUG to show activity in this function
int i=0; // added this to further set i
for (int i=0; i<=steps; i++){ // start at zero ; keep looping until true ; keep adding 1 to i
// for testing
//Serial.println(i);
//CHSW(); // take time to check the button
digitalWrite(stepperPin, HIGH); // Send blip to motor
delayMicroseconds(TheDelay); // Pause do the motor can see the blip and react
digitalWrite(stepperPin, LOW); // Send blip to motor
delayMicroseconds(TheDelay); // Send blip to motor
digitalWrite(stepperPin, HIGH); // Send blip to motor
delayMicroseconds(TheDelay); // Pause do the motor can see the blip and react
digitalWrite(stepperPin, LOW); // Send blip to motor
delayMicroseconds(TheDelay); // Pause do the motor can see the blip and react
} // END for(int i=0;i = steps;i++)
digitalWrite(sleepPin,LOW); // Sleep pin held high to remove stress on motor
// } // END Motor check
} //END step(boolean dir,int steps)
// *************************************************************************************
// *************************************************************************************
void loop(){
//CHSW(); // take time to check the button
if (MotorOnOff == 1 ) {step(true,15);} // Do stepper in forward direction for 15 times.
CHSW(); // take time to check the button
//BlinkLED1();
} //end loop()
// *************************************************************************************
// *************************************************************************************
void CHSW(){ // Monitor the switches
// Set switch delay
time = millis(); // Set time = current mills
if ( (digitalRead(DirectionSW) == HIGH) && ((time - oldTime) > 500) ){ //******************* Button
oldTime = time; // De-bounce delay to let switch settle.
Serial.print("Pressed DirectionSW "); // DEBUG
// Reverce the motor
int val = digitalRead(8); // Check the direction of the motor
if (val == HIGH) { // if CCW then do this
digitalWrite(8, LOW);
digitalWrite(DirectionLED, LOW); // Turn off the led to show the motor direction
Serial.println(" Foward "); // DEBUG
} else if (val == LOW){ //
digitalWrite(8, HIGH); //
digitalWrite(DirectionLED, HIGH); //
Serial.println(" Backward "); //
} // end else if
} else if ( (digitalRead(MotorOnOffSW) == HIGH) && ((time - oldTime) > 500) ){ //******************* Button
oldTime = time; // no comment it works..... haha
Serial.print("Pressed MotorOnOffSW");
if (MotorOnOff == 1) {
MotorOnOff = 0; // Stop the motor
digitalWrite(MortorStatusOnOff, LOW);
Serial.print(" off ");
Serial.println(MotorOnOff);
digitalWrite(sleepPin,LOW);
//delay(100);
} else if (MotorOnOff == 0) {
MotorOnOff = 1; // Run the motor
digitalWrite(MortorStatusOnOff, HIGH);
Serial.print(" on ");
Serial.println(MotorOnOff);
//delay(100);
} else {
// nothing
}
}
} // END CHSW()
// *************************************************************************************
void BlinkLED1(){ // Used to help find problems by blinking LED
digitalWrite(LED1, HIGH);
delay(100);
digitalWrite(LED1, LOW);
delay(100);
}
OK the motor will move but not spin. blip blip.....
Thanks
Dave