A Minor Step Towards Breakthrough (Jag)

I followed this tutorial to work my stepper motor but something didn't seem right. Lights B and D on the stepper motor pin board was on and the stepper only vibrated.
At first I thought it was a wiring issue but after conversing with Ramya, I learnt that most of the times with these tutorials, the problems would lie in the code itself. In particular, they could encourage the download of libraries that are potentially harmful to your computer. 
Taking a look at  the code again, we realized a less severe problem: that is, the library wasn't included in the first place. 

This is the modified code used in the end:


//original source is http://www.geeetech.com/wiki/index.php/Stepper_Motor_5V_4-Phase_5-Wire_%26_ULN2003_Driver_Board_for_Arduino
// This code is used with a video tutorial for RoboJax.com
// Published on March 27, 2017 from Aajx, ON, Canada.
#include <Stepper.h>
int Pin1 = 10;
int Pin2 = 11;
int Pin3 = 12;
int Pin4 = 13;
int _step = 0;
boolean dir = true;// false=clockwise, true=counter clockwise
int count=0;
void setup()
{
 pinMode(Pin1, OUTPUT); 
 pinMode(Pin2, OUTPUT); 
 pinMode(Pin3, OUTPUT); 
 pinMode(Pin4, OUTPUT); 
}
 void loop()
{
 switch(_step){
   case 0:
     digitalWrite(Pin1, LOW); 
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, LOW);
     digitalWrite(Pin4, HIGH);
   break; 
   case 1:
     digitalWrite(Pin1, LOW); 
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, HIGH);
     digitalWrite(Pin4, HIGH);
   break; 
   case 2:
     digitalWrite(Pin1, LOW); 
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, HIGH);
     digitalWrite(Pin4, LOW);
   break; 
   case 3:
     digitalWrite(Pin1, LOW); 
     digitalWrite(Pin2, HIGH);
     digitalWrite(Pin3, HIGH);
     digitalWrite(Pin4, LOW);
   break; 
   case 4:
     digitalWrite(Pin1, LOW); 
     digitalWrite(Pin2, HIGH);
     digitalWrite(Pin3, LOW);
     digitalWrite(Pin4, LOW);
   break; 
   case 5:
     digitalWrite(Pin1, HIGH); 
     digitalWrite(Pin2, HIGH);
     digitalWrite(Pin3, LOW);
     digitalWrite(Pin4, LOW);
   break; 
     case 6:
     digitalWrite(Pin1, HIGH); 
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, LOW);
     digitalWrite(Pin4, LOW);
   break; 
   case 7:
     digitalWrite(Pin1, HIGH); 
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, LOW);
     digitalWrite(Pin4, HIGH);
   break; 
   default:
     digitalWrite(Pin1, LOW); 
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, LOW);
     digitalWrite(Pin4, LOW);
   break; 
 }
 if(dir){
   _step++;
 }else{
   _step--;
 }
 if(_step>7){
   _step=0;
 }
 if(_step<0){
   _step=7;
 }
 delay(1);
}

And this is the result:


Comments

Popular Posts