The below arduino sketches for stepper motor and servo motor without using libraries puts everything in one code and gives a better and complete understanding of the sketch without the complications of referring to .h, .cpp and .ino files separately. please find attached sketches prepared by me. Hope it helps anyone looking for this so as to get a complete and clear understanding of the sketch without the mess of figuring out from .h header, .cpp files and. ino files
//Sketch of Stepper motor without library
// Code by : Gautam Gundap
// this sketch swings stepper motor 28BYJ-48 with ULN2008 as driver to one full revolution in clockwise direction and one full revolution in counter clockwise direction and keeps doing this. The speed can be varied in delay command(do not try to exceed the maximum speed as per data sheet of this stepper motor. This motor has a gear ratio of 1:64 and a single step angle of 5.625 degrees on the output shaft. Hence in 64 steps of output shaft it completes 360 degrees i.e. one revolution. The motor has actually taken 64x64 steps for one complete shaft revolution which is 4096 steps. The for loop takes 8 steps and multiplied by 512 iterations makes it a total of 4096 steps for one complete shaft revolution.The switchOutput array consists of one complete switching sequence for this stepper as per its datasheet. The Arduino register commands are used here for faster response. The serial monitor displays the completion of a set of cw and ccw revolution.
// Connect the 4 wires from the driver pcb to D2,D3,D4,D5 of arduino uno
int count = 0;
int switchOutput[8] = {B00100000, B00110000, B00010000, B00011000, B00001000, B00001100, B00000100, B00100100};
void setup()
{
DDRD = DDRD | B00111100;
Serial.begin(115200);
}
void loop()
{
if(count < 512 )
cw();
else if (count == 1024)
{
Serial.println("Swing cw and ccw complete");
count = 0;
}
else
ccw();
count++;
}
void ccw()
{
for(int i = 0; i < 8; i++)
{
pinOutput(i);
delay(1);
}
}
void cw()
{
for(int i = 7; i > -1; i--)
{
pinOutput(i);
delay(1);
}
}
void pinOutput(int out)
{
int buffer = B00000000;
buffer=PORTD | switchOutput[out];
PORTD=buffer & switchOutput[out];
}
// Sketch of servo motor without library
// Code by : Gautam Gundap
// sketch to swing servo motor SG90 from 0 to 180 and back to 0 and loop without the use of servo library
// Connect signal wire of servo to pin D9 of arduino uno
float k;
void setup()
{
pinMode(9, OUTPUT );
}
void loop()
{
for ( k =0.5;k<=4;k=k+0.1)
{
digitalWrite(9, HIGH);
delay(k);
digitalWrite(9,LOW);
delay (15-k);
}
for (k=3.9;k>=0.4;k=k-0.1)
{
digitalWrite(9, HIGH);
delay(k);
digitalWrite(9,LOW);
delay (15-k);
}
}
sweep servo sg90.ino (464 Bytes)
stepperwithoutlibrary.ino (1.74 KB)