Problem solved
Hi,
Im new to Arduino and would really appreciate some guidance. I have searched high and low, but with my limited knowledge the answers keeps eluding me^^
Board: Arduino Uno
RF receiver: X D-YK04 (SC2272) http://dx.com/p/4-key-wireless-rf-module-w-remote-controller-152284
Step motor: 28BYJ-48
Goal for project: By clicking the transmitter i want the receiver to tell the step motor to rotate 90 degrees and lock into a mechanism ive built.
So far i have managed to connect and code both the RF receiver and the step motor separately, but i need some guidance to get them to work together.
My current code for the receiver only sends a signal which for instance can activate a LED-light just fine, but in order to get the step motor to run i have to activate the 4 different coils individually. I also have code that activate the coils and rotates my motor the way i want.
How do i proceed so that when i press the transmitter, the receiver runs the code that rotates my motor? (Not just sends a signal as it does now...)
I realise that this is probably a stupid/easy question, but still i would love a simplified explanation from anyone who knows
////////////////////////////////////
Update:
Got it working... sort of!
My only problem now is that the motor isnt running smoothly, its bumping along little by little.
(BTW i know the code is messy :P)
Also the bumping might not be a result of the code, should i have used any resistors on this "project-build"?
Picture:
Both the motor and the RF receiver get 5v from the arduino via the white breadboard. This is my first ever project and i have no idea if I should have used a resistor/resistors anywhere...?
const int buttonPin = 2;// the number of the pushbutton pin
const int buttonPin2 = 4;
int buttonState = 0; // variable for reading the pushbutton status
int Pin0 = 10; //coil 1
int Pin1 = 11; //coil 2
int Pin2 = 12; //coil 3
int Pin3 = 13; //coil 4
int _step = 0;
int i = 0;
boolean dir = true; //direction CW or CCW
void setup() {
pinMode(buttonPin, INPUT);
/////////////////////////////////////
Serial.begin(9600);
pinMode(Pin0, OUTPUT);
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
/////////////////////////////////////
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
if (i == 125){
dir = false;
delay(100);
}else if (i == 0){
dir = true;
delay(100);
}
switch(_step){
case 0:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
if (dir == false){
i--;}else{
i++;
}
break;
case 1:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, HIGH);
break;
case 2:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
break;
case 3:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
break;
case 4:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 5:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 6:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 7:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
break;
default:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
}
if(dir == true){
_step++;
}else{
_step--;
}
if(_step>7){
_step=0;
}
if(_step<0){
_step=7;
}
delay(1);
}
}
Thanks in advance