Vous êtes sur la page 1sur 3

Arduino Code - Mengontrol Motor Stepper A4988

// Define pin connections & motor's steps per revolution


const int dirPin = 2;
const int stepPin = 3;
const int stepsPerRevolution = 200;

void setup()
{
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop()
{
// Set motor direction clockwise
digitalWrite(dirPin, HIGH);

// Spin motor slowly


for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
delay(1000); // Wait a second

// Set motor direction counterclockwise


digitalWrite(dirPin, LOW);

// Spin motor quickly


for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000); // Wait a second
}

1
PROJECT SMS SIM 800L WITH MOTOR STEPPER <By : Toko Tronik>

#include <SoftwareSerial.h>
SoftwareSerial gsm(2,3); // Rx/Tx wavecom di pin 2 dan 3
String sms = "";
char str;
const int stepPin = 3;
const int dirPin = 4;
int flag = 0;

void setup() {
delay(20000);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
Serial.begin(9600);
gsm.begin(9600); // init wavecom dg baudrate 9600
delay(3000);
gsm.println("AT+CMGF=1"); // Set mode sms
delay(500);
Get_sms(); // Baca respon gsm wavecom
if(sms.indexOf("OK") >= 0) { // Jika respon OK
Serial.println("OK");
sms="";
}
else {
Serial.println("Gagal");
sms="";
while(1);
}
delay(2000);
gsm.println("AT+CNMI=1,2,0,0,0"); // Set mode sms
delay(500);
Get_sms(); // Baca respon gsm wavecom
if(sms.indexOf("OK") >= 0) { // Jika respon OK
Serial.println("OK");
sms="";
}
else {
Serial.println("Gagal");
sms="";
while(1);
}
delay(2000);
}

void loop() {

2
Get_sms();
if(sms.indexOf("kanan") >= 0) {
flag = 1;
Serial.println(sms);
Serial.println("Putar Kanan");
sms = "";
} else if(sms.indexOf("kiri") >= 0) {
flag = 2;
Serial.println(sms);
Serial.println("Putar Kiri");
sms = "";
} else if(sms.indexOf("off") >= 0) {
flag = 0;
Serial.println(sms);
Serial.println("OFF");
sms = "";
}

if(flag==1) {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
} else if(flag==2) {
digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
}
}

void Get_sms() {
while(gsm.available()>0) {
str = (char)gsm.read();
sms += String(str);
Serial.write(str);
}
}

Vous aimerez peut-être aussi