Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <IRremote.h>
- struct WheelStruct {
- int mspeed; // Max is 255
- int mForward; // Is forward or not.
- const int speedPinD; // Digital, PWM
- const int directionPinD; // Digital
- const int brakePinD; // Digital (I've got no clue what this is.)
- const int currentSensingPinA; // Analog
- };
- const int ZERO = 0;
- const int MAX = 255;
- const int HALF = 123+40;
- const int QUARTER = 95;
- boolean CLOCKWISE = true;
- WheelStruct motorA = {
- ZERO, true, 3, 12, 9, 0}; // Motor A is left
- WheelStruct motorB = {
- ZERO, true, 11, 13, 8, 1}; // Motor B is right.
- const int ledLampAPin = 4;
- const int ledLampBPin = 5;
- boolean stopped = false;
- boolean toggle = false;
- long previousMillis = 0;
- long interval = 1000;
- int recvPin = 2;
- IRrecv irrecv(recvPin);
- decode_results results;
- void setup(){
- Serial.begin(9600);
- pinMode(motorA.speedPinD, OUTPUT);
- pinMode(motorB.speedPinD, OUTPUT);
- pinMode(motorA.directionPinD, OUTPUT);
- pinMode(motorB.directionPinD, OUTPUT);
- pinMode(motorA.brakePinD, OUTPUT);
- pinMode(motorB.brakePinD, OUTPUT);
- pinMode(motorA.currentSensingPinA, INPUT);
- pinMode(motorB.currentSensingPinA, INPUT);
- irrecv.enableIRIn();
- }
- void loop(){
- if(gotKey()){
- while(getKey() >= 3000){
- softReset();
- }
- switch(getKey()){
- case 5:
- motorTurn(ZERO);
- Serial.println("Stopped");
- break;
- case 2:
- motorTurn(MAX);
- Serial.println("Forwards.");
- break;
- case 4:
- motorTurn(MAX, -MAX); // If it doesn't work, try (MAX, ZERO)
- Serial.println("Left.");
- break;
- case 6:
- motorTurn(-MAX, MAX); // Again, if not, try (ZERO, MAX)
- Serial.println("Right.");
- break;
- case 8:
- motorTurn(-MAX);
- Serial.println("Backwards.");
- break;
- case 1:
- motorTurn(HALF, MAX);
- Serial.println("Left, forward.");
- break;
- case 3:
- motorTurn(MAX, HALF-40);
- Serial.println("Right, forward.");
- break;
- case 7:
- motorTurn(-HALF, -MAX);
- Serial.println("Left, backwards.");
- break;
- case 9:
- motorTurn(-MAX, -HALF);
- Serial.println("Right, backwards.");
- break;
- case 12:
- if(stopped)
- unlock();
- else{
- brake();
- stopped = true;
- }
- delay(100);
- default:
- break;
- }
- irrecv.resume();
- }
- }
- /* FUNCTIONS STARTS HERE */
- /* *
- * switchWheelDirection - toggles the state of mDirPin (Motor Direction Pin) *
- * */
- void switchWheelDirection(int mDirPin){
- if(digitalRead(mDirPin) == HIGH)
- digitalWrite(mDirPin, LOW);
- else
- digitalWrite(mDirPin, HIGH);
- }
- /* *
- * writeMotor - replaces the analogWrite() function, positive values = forward *
- * negative values = backwards, for ex "mspeed = 100" == motorX.mspeed == 100 *
- * */
- void motorWrite(char motor, int mspeed){
- if (mspeed >= 0 && mspeed <= 255){
- if(motor == 'A'){
- digitalWrite(motorA.directionPinD, HIGH);
- analogWrite(motorA.speedPinD, mspeed);
- motorA.mForward = true;
- }
- if(motor == 'B'){
- digitalWrite(motorB.directionPinD, HIGH);
- analogWrite(motorB.speedPinD, mspeed);
- motorB.mForward = true;
- }
- }
- else if(mspeed >= -255 && mspeed <= 0){
- if(motor == 'A'){
- digitalWrite(motorA.directionPinD, LOW);
- analogWrite(motorA.speedPinD, abs(mspeed));
- motorA.mForward = false;
- }
- if(motor == 'B'){
- digitalWrite(motorB.directionPinD, LOW);
- analogWrite(motorB.speedPinD, abs(mspeed));
- motorB.mForward = false;
- }
- }
- }
- /* *
- * motorTurn - change the speed of each wheel idividually. *
- * */
- void motorTurn(int lSpeed, int rSpeed){
- motorWrite('A', lSpeed);
- motorWrite('B', rSpeed);
- }
- void motorTurn(int mspeed){
- motorWrite('A', mspeed);
- motorWrite('B', mspeed);
- }
- /* *
- * spinAround - spins the car clockwise/anti-clockwise depending on bool clockwise *
- * use forward or brake to disable. Use CLOCKWISE/!CLOCKWISE *
- * */
- void spinAround(boolean clockwise){
- if(clockwise == true){
- // Makes the other wheel go forward, the other one backward.
- }
- }
- /* *
- * brake - stops all motors *
- * */
- void brake(){
- digitalWrite(motorA.brakePinD, HIGH);
- digitalWrite(motorB.brakePinD, HIGH);
- }
- /* *
- * unlock - reverses the effects of brake *
- * */
- void unlock(){
- digitalWrite(motorA.brakePinD, LOW);
- digitalWrite(motorB.brakePinD, LOW);
- }
- /* *
- * gotKey - "lazydog" for IRrecv irrecv.decode(decode_results &results) *
- * */
- boolean gotKey(){
- return irrecv.decode(&results);
- }
- /* *
- * getKey - "lazydog" for decode_results results.value & 2047 *
- * */
- int getKey(){
- return results.value & 2047; // ANDing with 2047 for making the 12th bit disappear.
- }
- /* *
- * softReset - macro for soft reseting the arduino, doesn't wipe *
- * */
- void softReset(){
- asm volatile (" jmp 0");
- }
- /* FUNCTIONS ENDS HERE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement