Stepper Motor

/*
—- Transmitter Code —-
*/

#include <SPI.h>
#include “RF24.h”

unsigned long SetTime;
int joystick[7];

RF24 radio(7,8);

const uint64_t pipe = 0x7878787878LL;

void setup(void)
{
radio.begin();
radio.setPALevel (RF24_PA_HIGH);
radio.setChannel(14);
radio.openWritingPipe(pipe);
pinMode(2, INPUT_PULLUP);
//pinMode(10, INPUT_PULLUP); I have some problem with D10, so I turn it off.
pinMode(9, INPUT_PULLUP);
pinMode(6, OUTPUT);

radio.stopListening();
}

void loop(void)
{
joystick[0] = analogRead(A0); // for Stepper 1
joystick[1] = analogRead(A2); // for Servo 1
joystick[2] = analogRead(A3); // for Servo 2
joystick[3] = analogRead(A7); // for Stepper 2
joystick[4] = digitalRead(2); // Do nothing in this project
//joystick[5] = digitalRead(10); I have some problem with D10, so I turn it off.
joystick[6] = digitalRead(9); // Gun fire button

radio.write(joystick, sizeof(joystick) );

if (millis() – SetTime < 50)
{
digitalWrite(6, HIGH);
}
else if (millis() – SetTime < 2500)
{
digitalWrite(6, LOW);
}
else
{
SetTime = millis();
}
}3

 

/*
—- Receiver Code —-
*/
#include <Servo.h>
#include <SPI.h>
#include “RF24.h”

int motorPin1 = 3;
int motorPin2 = 4;
int motorPin3 = 5;
int motorPin4 = 6;
int motorPin5 = 14;
int motorPin6 = 15;
int motorPin7 = 16;
int motorPin8 = 17;

int motorSpeed = 1;
int motorSpeed2 = 1;
int potValue = 0;
int potValue2 = 0;

int position1 = 90;
int position2 = 90;
Servo myServo1;
Servo myServo2;

long previousMillis = 0;
long interval = 15;

unsigned long SetTime;
unsigned long SetTime2;
unsigned long SetTime3;

boolean lastButton1 = HIGH; // or LOW

boolean ledOn1 = false;

int buttonState1 = 0;

int joystick[7];

RF24 radio(7,8);
const uint64_t pipe = 0x7878787878LL;

void setup()
{
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(motorPin5, OUTPUT);
pinMode(motorPin6, OUTPUT);
pinMode(motorPin7, OUTPUT);
pinMode(motorPin8, OUTPUT);
pinMode(2, OUTPUT);
pinMode(1, OUTPUT);
pinMode(0, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);

myServo1.attach(18);
myServo2.attach(19);

radio.begin();
radio.setPALevel (RF24_PA_HIGH);
radio.setChannel(14);
radio.openReadingPipe(1, pipe);
radio.startListening();
}

void loop()
{
if(radio.available())
{
bool done = false;
while (!done)
{
done = radio.read(joystick, sizeof(joystick) );

unsigned long currentMillis = millis();
if(currentMillis – previousMillis > interval)

{
previousMillis = currentMillis;

if (millis() – SetTime < 50)
{
digitalWrite(0, HIGH);
}
else if (millis() – SetTime < 1000)
{
digitalWrite(0, LOW);
}
else
{
SetTime = millis();
}

//// Stepper Motor ////
potValue = joystick[0];
if (potValue < 412)
{
motorSpeed = (potValue/15 + 5);
counterclockwise();
if (millis() – SetTime2 < 200)
{
digitalWrite(2, HIGH);
}
else if (millis() – SetTime2 < 1300)
{
digitalWrite(2, LOW);
}
else
{
SetTime2 = millis();
}
}
else if (potValue > 612)
{
motorSpeed = ((1024-potValue)/15 + 5);
clockwise();
if (millis() – SetTime2 < 200)
{
digitalWrite(2, HIGH);
}
else if (millis() – SetTime2 < 1300)
{
digitalWrite(2, LOW);
}
else
{
SetTime2 = millis();
}
}
else digitalWrite(2, LOW);

potValue2 = joystick[3];
if (potValue2 < 412)
{
motorSpeed2 = (potValue2/15 + 5);
clockwise2();
if (millis() – SetTime3 < 200)
{
digitalWrite(2, HIGH);
}
else if (millis() – SetTime3 < 2000)
{
digitalWrite(2, LOW);
}
else
{
SetTime3 = millis();
}
}
else if (potValue2 > 612)
{
motorSpeed2 = ((1024-potValue2)/15 + 5);
counterclockwise2();
if (millis() – SetTime2 < 200)
{
digitalWrite(2, HIGH);
}
else if (millis() – SetTime2 < 1300)
{
digitalWrite(2, LOW);
}
else
{
SetTime2 = millis();
}
}
else digitalWrite(2, LOW);

//// Servo ////
if(joystick[1] > 612 && position1 < 180)
{
position1+=3;
myServo1.write(position1);
}
if(joystick[1] < 412 && position1 > 0)
{
position1-=3;
myServo1.write(position1);
}

if(joystick[2] < 412 && position2 < 180)
{
position2+=3;
myServo2.write(position2);
}
if(joystick[2] > 612 && position2 > 0)
{
position2-=3;
myServo2.write(position2);
}

//// Button ////
buttonState1 = joystick[4];
if (buttonState1 == HIGH && lastButton1 == LOW)
{
ledOn1 = !ledOn1;
lastButton1 = HIGH;
}
else
{
lastButton1 = buttonState1;
}

digitalWrite(9, joystick[6]);

}
}
}
}

//////////////////////////////////////////////////////////////////////////////

void counterclockwise (){

// 1
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(motorSpeed);

// 2
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay (motorSpeed);

// 3
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(motorSpeed);

// 4

digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(motorSpeed);

// 5
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(motorSpeed);

// 6
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
delay (motorSpeed);

// 7
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(motorSpeed);

// 8
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(motorSpeed);
}

//////////////////////////////////////////////////////////////////////////////

void clockwise(){

// 1
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delay(motorSpeed);

// 2
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delay (motorSpeed);

// 3
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delay(motorSpeed);

// 4
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);
delay(motorSpeed);

// 5
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);

delay(motorSpeed);

// 6
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, HIGH);
delay (motorSpeed);

// 7
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, HIGH);
delay(motorSpeed);

// 8
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, HIGH);
delay(motorSpeed);
}

//////////////////////////////////////////////////////////////////////////////

void counterclockwise2 (){

// 1
digitalWrite(motorPin5, HIGH);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin8, LOW);
delay(motorSpeed2);

// 2
digitalWrite(motorPin5, HIGH);
digitalWrite(motorPin6, HIGH);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin8, LOW);
delay (motorSpeed2);

// 3
digitalWrite(motorPin5, LOW);
digitalWrite(motorPin6, HIGH);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin8, LOW);
delay(motorSpeed2);

// 4

digitalWrite(motorPin5, LOW);
digitalWrite(motorPin6, HIGH);
digitalWrite(motorPin7, HIGH);
digitalWrite(motorPin8, LOW);
delay(motorSpeed2);

// 5
digitalWrite(motorPin5, LOW);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin7, HIGH);
digitalWrite(motorPin8, LOW);
delay(motorSpeed2);

// 6
digitalWrite(motorPin5, LOW);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin7, HIGH);
digitalWrite(motorPin8, HIGH);
delay (motorSpeed2);

// 7
digitalWrite(motorPin5, LOW);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin8, HIGH);
delay(motorSpeed2);

// 8
digitalWrite(motorPin5, HIGH);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin8, HIGH);
delay(motorSpeed2);
}

//////////////////////////////////////////////////////////////////////////////

void clockwise2(){

// 1
digitalWrite(motorPin8, HIGH);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin5, LOW);
delay(motorSpeed2);

// 2
digitalWrite(motorPin8, HIGH);
digitalWrite(motorPin7, HIGH);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin5, LOW);
delay (motorSpeed2);

// 3
digitalWrite(motorPin8, LOW);
digitalWrite(motorPin7, HIGH);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin5, LOW);
delay(motorSpeed2);

// 4
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);
delay(motorSpeed2);

// 5
digitalWrite(motorPin8, LOW);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin6, HIGH);
digitalWrite(motorPin5, LOW);

delay(motorSpeed2);

// 6
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, HIGH);
delay (motorSpeed2);

// 7
digitalWrite(motorPin8, LOW);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin5, HIGH);
delay(motorSpeed2);

// 8
digitalWrite(motorPin8, HIGH);
digitalWrite(motorPin7, LOW);
digitalWrite(motorPin6, LOW);
digitalWrite(motorPin5, HIGH);
delay(motorSpeed2);
}