Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int vrX = 0;
- int vrY = 1;
- int sw = 10;
- int in1 = 8;
- int in2 = 7;
- int enA = 9;
- int in3 = 13;
- int in4 = 12;
- int enB = 11;
- int motorSpeedA = 0;
- int motorSpeedB = 0;
- void setup() {
- // put your setup code here, to run once:
- pinMode(in3, OUTPUT);
- pinMode(in4, OUTPUT);
- pinMode(enB, OUTPUT);
- pinMode(sw, INPUT);
- pinMode(in1, OUTPUT);
- pinMode(in2, OUTPUT);
- pinMode(enA, OUTPUT);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- int xAxis = analogRead(A0);
- int yAxis = analogRead(A1);
- if (yAxis < 470){
- digitalWrite(in3, HIGH);
- digitalWrite(in4, LOW);
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- motorSpeedA = map(yAxis, 470, 0, 0, 255);
- motorSpeedB = map(yAxis, 470, 0, 0, 255);
- }
- else if (yAxis > 550){
- digitalWrite(in3, LOW);
- digitalWrite(in4, HIGH);
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- motorSpeedA = map(yAxis, 550, 1023, 0, 255);
- motorSpeedB = map(yAxis, 550, 1023, 0, 255);
- }
- else{
- motorSpeedA = 0;
- motorSpeedB = 0;
- }
- if (xAxis < 470){
- int xMapped = map(xAxis, 470, 0, 0, 255);
- motorSpeedA = motorSpeedA - xMapped;
- motorSpeedB = motorSpeedB + xMapped;
- if(motorSpeedA < 0){
- motorSpeedA = 0;
- }
- if(motorSpeedB > 255){
- motorSpeedB = 255;
- }
- }
- else if(xAxis > 550){
- int xMapped = map(xAxis, 550, 1023, 0, 255);
- motorSpeedA = motorSpeedA + xMapped;
- motorSpeedB = motorSpeedB - xMapped;
- if(motorSpeedA > 255){
- motorSpeedA = 255;
- }
- if(motorSpeedB < 0){
- motorSpeedB = 0;
- }
- }
- analogWrite(enA, motorSpeedA);
- analogWrite(enB, motorSpeedB);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement