Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int motorButton = 2;
- int motorOneLeft = 3;
- int motorOneRight = 4;
- int motorTwoLeft = 5;
- int motorTwoRight = 6;
- int motorThreeLeft = 7;
- int motorThreeRight = 8;
- int motorAllDelay = 1000;
- int savedButtonState = 0;
- int savedTime = 0;
- void internal_adjustMotor(int leftAddress, int rightAddress, int leftVal, int RightVal, int speed) {
- if (leftVal == 0) {
- digitalWrite(leftAddress, LOW);
- }
- else if (leftVal == 1) {
- digitalWrite(leftAddress, HIGH);
- }
- if (rightVal == 0) {
- digitalWrite(rightAddress, LOW);
- }
- else if (rightVal == 1) {
- digitalWrite(rightAddress, HIGH);
- }
- // Speed
- analogWrite(leftAddress, speed);
- analogWrite(rightAddress, speed);
- }
- void digitalMotorMode(int motor, int status, int speed) { // status 0 = off, status 1 = left, status 2 = right
- if (motor == 1) {
- if (status == 0) {
- internal_adjustMotor(motorOneLeft, motorOneRight, 0, 0, speed);
- }
- else if (status == 1) {
- internal_adjustMotor(motorOneLeft, motorOneRight, 1, 0, speed);
- }
- else if (status == 2) {
- internal_adjustMotor(motorOneLeft, motorOneRight, 0, 1, speed);
- }
- }
- if (motor == 2) {
- if (status == 0) {
- internal_adjustMotor(motorTwoLeft, motorTwoRight, 0, 0, speed);
- }
- else if (status == 1) {
- internal_adjustMotor(motorTwoLeft, motorTwoRight, 1, 0, speed);
- }
- else if (status == 2) {
- internal_adjustMotor(motorTwoLeft, motorTwoRight, 0, 1, speed);
- }
- }
- if (motor == 3) {
- if (status == 0) {
- internal_adjustMotor(motorThreeLeft, motorThreeRight, 0, 0, speed);
- }
- else if (status == 1) {
- internal_adjustMotor(motorThreeLeft, motorThreeRight, 1, 0, speed);
- }
- else if (status == 2) {
- internal_adjustMotor(motorThreeLeft, motorThreeRight, 0, 1, speed);
- }
- }
- }
- void setup() {
- // initialize serial communication at 9600 bits per second:
- Serial.begin(9600);
- // make the pushbutton's pin an input:
- pinMode(motorButton, INPUT);
- pinMode(motorOneLeft, OUTPUT);
- pinMode(motorOneRight, OUTPUT);
- pinMode(motorTwoLeft, OUTPUT);
- pinMode(motorTwoRight, OUTPUT);
- pinMode(motorThreeLeft, OUTPUT);
- pinMode(motorThreeRight, OUTPUT);
- }
- void loop() {
- int buttonState = digitalRead(motorButton);
- if (savedButtonState == 0 && buttonState == 1) {
- // Wir nehmen an 2 = Ausfahren (right)
- digitalMotorMode(1, 2, 10);
- digitalMotorMode(2, 2, 10);
- digitalMotorMode(3, 2, 10);
- // We got with a sum delay here, it's easier to set the motor speed .. another option would be
- // delaying after each motor starting with the one taking the most time, second most etc.
- delay(motorAllDelay);
- // power off
- digitalMotorMode(1, 0, 0);
- digitalMotorMode(2, 0, 0);
- digitalMotorMode(3, 0, 0);
- }
- else if (savedButtonState == 1 && buttonState == 0) {
- // Wir nehmen an 1 = Einfahren (left)
- digitalMotorMode(1, 1, 10);
- digitalMotorMode(2, 1, 10);
- digitalMotorMode(3, 1, 10);
- delay(motorAllDelay);
- // power off
- digitalMotorMode(1, 0, 0);
- digitalMotorMode(2, 0, 0);
- digitalMotorMode(3, 0, 0);
- savedTime = 0;
- }
- else if (buttonState == 1) {
- savedTime += 1;
- if (savedTime == 10000) { // 10 sec
- digitalMotorMode(1, 1, 10);
- digitalMotorMode(2, 1, 10);
- digitalMotorMode(3, 1, 10);
- delay(motorAllDelay);
- digitalMotorMode(1, 0, 0);
- digitalMotorMode(2, 0, 0);
- digitalMotorMode(3, 0, 0);
- }
- delay(1);
- }
- savedButtonState = buttonState;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement