Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <nRF24L01.h>
- #include <RF24.h>
- #include <CheapStepper.h> //Library can be found here: https://github.com/tyhenry/CheapStepper
- const unsigned short int MAIN_DELAY = 1000;
- const unsigned short int requiredStep = 2048;
- const int CE = 6;
- const int CSN = 7;
- int joystickData[3] = {0};
- int leftLimitSwitch = A1;
- int rightLimitSwitch = A2;
- int leftEndSwitchState = 0;
- int rightEndSwitchState = 0;
- const int speedSetpoint1 = 1;
- const int speedSetpoint2 = 2;
- unsigned int speedValue = 1;
- int rotateStepperSetpointSlow = 20; // RPM can be set from 1RPM to 15RPM
- int rotateStepperSetpointFast = 25; // RPM can be set from 1RPM to 15RPM
- bool joystickButtonPressed = 0;
- CheapStepper motor (8,9,10,11); //Create 28BYJ-48 object. Pins might be different. See description on https://github.com/tyhenry/CheapStepper
- RF24 radio(CE, CSN); // CE, CSN
- const byte address[6] = "00001";
- void set_speed()
- {
- if(speedValue == speedSetpoint1)
- {
- motor.setRpm(rotateStepperSetpointSlow);
- speedValue = speedSetpoint2;
- }
- else
- {
- motor.setRpm(rotateStepperSetpointFast);
- speedValue = speedSetpoint1;
- }
- }
- void setup()
- {
- pinMode(leftLimitSwitch, INPUT);
- digitalWrite(leftLimitSwitch, HIGH);
- pinMode(rightLimitSwitch, INPUT);
- digitalWrite(rightLimitSwitch, HIGH);
- Serial.begin(9600);
- motor.setRpm(rotateStepperSetpointSlow); // RPM can be set from 1RPM to 15RPM
- radio.begin();
- radio.openReadingPipe(0, address);
- radio.setPALevel(RF24_PA_MIN);
- radio.startListening();
- }
- void loop()
- {
- leftEndSwitchState = digitalRead(leftLimitSwitch);
- rightEndSwitchState = digitalRead(rightLimitSwitch);
- Serial.println(leftEndSwitchState);
- Serial.println(rightEndSwitchState);
- Serial.println("==================");
- /*
- Start reading from NRF module
- */
- if ( radio.available())
- {
- radio.read(&joystickData, sizeof(joystickData)); //read data
- Serial.println(joystickData[0]); // X-Position
- Serial.println(joystickData[1]); // Y-position
- Serial.println(joystickData[2]); // SW Button
- Serial.println("@@@@@@@@@@@@@");
- /*
- Set speed dependently on SW button state
- */
- if(joystickData[2] == 1 && joystickButtonPressed == 0)
- {
- set_speed();
- joystickButtonPressed = 1;
- }
- if(joystickData[2] == 0)
- {
- joystickButtonPressed = 0;
- }
- /*
- Move rotate stepper
- */
- /*
- if(joystickData[1] > 800)
- {
- Serial.println("Rotating motor to ClockWise");
- motor.moveTo (true, requiredStep);
- } else if(joystickData[1] < 300)
- {
- Serial.println("Rotating motor to CounterClockWise");
- motor.moveTo(false, requiredStep);
- }else
- {
- Serial.print("No motor action. Value of Y-position is:");
- Serial.println(joystickData[1]);
- }
- */
- }
- motor.moveTo(true, 2048);
- if(MAIN_DELAY !=0)
- {delay(MAIN_DELAY);}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement