Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int button=2;
- int sw;
- const int x_axis = 4;
- const int y_axis = 5;
- int dir1PinA = 4;
- int speedPinA = 5; // Needs to be a PWM pin to be able to control motor speed
- int dir2PinB = 7;
- int speedPinB = 6; // Needs to be a PWM pin to be able to control motor speed
- int x_hiz=0;
- int y_hiz=0;
- boolean lock;
- void setup() {
- Serial.begin(9600);
- pinMode(button, INPUT);
- digitalWrite(button,HIGH);
- pinMode(dir1PinA,OUTPUT);
- pinMode(speedPinA,OUTPUT);
- pinMode(dir2PinB,OUTPUT);
- pinMode(speedPinB,OUTPUT);
- //Setup Channel A
- pinMode(12, OUTPUT); //Initiates Motor Channel A pin
- pinMode(9, OUTPUT); //Initiates Brake Channel A pin
- //Setup Channel B
- pinMode(13, OUTPUT); //Initiates Motor Channel B pin
- pinMode(8, OUTPUT); //Initiates Brake Channel B pin
- }
- void loop() {
- sw = digitalRead(button);
- if (sw == LOW)
- {
- analogWrite(3, 0);
- analogWrite(11, 0);
- y_hiz= analogRead(y_axis);
- //y_hiz= map(y_hiz,0,1023,255,0);
- if (y_hiz > 600)
- {
- analogWrite(speedPinA, 127);//Sets speed variable via PWM
- analogWrite(speedPinB, 127);//Sets speed variable via PWM
- digitalWrite(dir1PinA, LOW);
- digitalWrite(dir2PinB, HIGH);
- lock = true;
- }
- else if (y_hiz < 424)
- {
- analogWrite(speedPinA, 127);//Sets speed variable via PWM
- analogWrite(speedPinB, 127);//Sets speed variable via PWM
- digitalWrite(dir1PinA, HIGH);
- digitalWrite(dir2PinB, LOW);
- lock = true;
- }
- else
- {
- digitalWrite(12, HIGH);
- digitalWrite(9, LOW);
- analogWrite(3, 0);
- digitalWrite(13, HIGH);
- digitalWrite(8, LOW);
- analogWrite(11, 0);
- analogWrite(speedPinA, 0);
- analogWrite(speedPinB, 0);
- lock = false;
- }
- }
- else if (sw == HIGH)
- {
- analogWrite(speedPinA, 0);
- analogWrite(speedPinB, 0);
- y_hiz= analogRead(y_axis);
- //y_hiz= map(y_hiz,0,1023,255,0);
- x_hiz = analogRead(x_axis);
- if (y_hiz > 324 && y_hiz < 700 || lock)
- {
- analogWrite(3, 0);
- analogWrite(11, 0);
- }
- if (y_hiz > 700)
- {
- if (x_hiz > 700)
- {
- analogWrite(3, 127);//Sets speed variable via PWM
- analogWrite(11, 0);//Sets speed variable via PWM
- }
- else if (x_hiz < 324)
- {
- analogWrite(3, 0);//Sets speed variable via PWM
- analogWrite(11, 127);//Sets speed variable via PWM
- }
- else
- {
- analogWrite(3, 127);//Sets speed variable via PWM
- analogWrite(11, 127);//Sets speed variable via PWM
- }
- digitalWrite(12, HIGH);
- digitalWrite(13, HIGH);
- }
- else if (y_hiz < 324)
- {
- if (x_hiz > 700)
- {
- analogWrite(3, 127);//Sets speed variable via PWM
- analogWrite(11, 0);//Sets speed variable via PWM
- }
- else if (x_hiz < 324)
- {
- analogWrite(3, 0);//Sets speed variable via PWM
- analogWrite(11, 127);//Sets speed variable via PWM
- }
- else
- {
- analogWrite(3, 127);//Sets speed variable via PWM
- analogWrite(11, 127);//Sets speed variable via PWM
- }
- digitalWrite(12, HIGH);
- digitalWrite(13, HIGH);
- }
- else
- {
- analogWrite(3, 0);
- analogWrite(11, 0);
- }
- }
- /* Serial.print("vertical: ");
- Serial.print(y_hiz,DEC);
- Serial.print(" horizontal: ");
- Serial.print(x_hiz,DEC);
- Serial.print(" select: ");
- if(sw == HIGH)
- Serial.println("not pressed");
- else
- Serial.println("PRESSED!");*/
- }
Advertisement
Add Comment
Please, Sign In to add comment