To drive a stepper motor you need a source(i used my computer's power supply 12V), an IR receiver(i get one from a bad DVD-player) , a stepper motor, an ULN2003A Darlington Array , IR remote controller (i used an IR of an old Sony amplifier) , a FET(i used IRF 540N), a breadboard and Arduino. You have to hook up like this.
Next thing the program. I learned from arduino.cc and from forums to write this program, and this works.
It looks like this:
int ir_pin = 2;
int led_pin = 13;
int debug = 0;
int start_bit = 2000;
int bin_1 = 1000;
int bin_0 = 400;
int max_count=200;
int count=0;
int min_count=20;
#include <Stepper.h>
#define STEPS 100
Stepper stepper(STEPS, 8, 9, 10, 11);
int val=0;
void setup() {
pinMode(9,OUTPUT);
pinMode(led_pin, OUTPUT);
pinMode(ir_pin, INPUT);
digitalWrite(led_pin, LOW);
pinMode(4,OUTPUT);
Serial.begin(9600);
}
void loop() {
int key = getIRKey();
Serial.print("Key Recieved: ");
Serial.println(key);
Serial.println(count);
stepper.setSpeed(count);
if (key==21){
Serial.println("POWER ON");
digitalWrite(4,HIGH);
delay(90);
}
if (key==184){
Serial.println("POWER OFF");
digitalWrite(4,LOW);
delay(180);
}
if (key==1680){
val=val+500;
digitalWrite(4,HIGH);
}
if (key==1681){
val=val-500;
digitalWrite(4,HIGH);
}
if (key==52){
count=count+20;
delay(90);
if (count > max_count){
count=200;}
}
if (key==51){
count=count-20;
delay(90);
if (count < min_count){
count=20;}
}
if (key==176){
digitalWrite(4,HIGH);
val=-100;
}
if (key==177){
digitalWrite(4,HIGH);
val=100;
}
stepper.step(val);
val=0;
digitalWrite(4,LOW);
}
int getIRKey() {
int data[12];
digitalWrite(led_pin, HIGH);
while(pulseIn(ir_pin, LOW) < 2200) {
}
data[0] = pulseIn(ir_pin, LOW);
data[1] = pulseIn(ir_pin, LOW);
data[2] = pulseIn(ir_pin, LOW);
data[3] = pulseIn(ir_pin, LOW);
data[4] = pulseIn(ir_pin, LOW);
data[5] = pulseIn(ir_pin, LOW);
data[6] = pulseIn(ir_pin, LOW);
data[7] = pulseIn(ir_pin, LOW);
data[8] = pulseIn(ir_pin, LOW);
data[9] = pulseIn(ir_pin, LOW);
data[10] = pulseIn(ir_pin, LOW);
data[11] = pulseIn(ir_pin, LOW);
digitalWrite(led_pin, LOW);
if(debug == 1) {
Serial.println("-----");
}
for(int i=0;i<11;i++) {
if (debug == 1) {
Serial.println(data[i]);
}
if(data[i] > bin_1) {
data[i] = 1;
} else {
if(data[i] > bin_0) {
data[i] = 0;
} else {
data[i] = 2;
}
}
}
for(int i=0;i<11;i++) {
if(data[i] > 1) {
return -1;
}
}
int result = 0;
int lastresult=0;
int seed = 1;
for(int i=0;i<11;i++) {
if(data[i] == 1) {
result += seed;
}
seed = seed * 2;
}
return result;
}
I hope i gave a good alternative of stepper motor controlling.
Nincsenek megjegyzések:
Megjegyzés küldése