int pulPin = 4;
int dirPin = 3;
int enblPin = 2;
int ledPin = 13;
int pushButton = 6;
int speed =0;
int pushButton1 = 5;
void setup() {
// put your setup code here, to run once:
pinMode(pulPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enblPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pulPin, LOW);
digitalWrite(ledPin, LOW);
digitalWrite(enblPin, LOW);
digitalWrite(dirPin, LOW);
pinMode(pushButton, INPUT);
pinMode(pushButton1, INPUT);
Serial.begin(9600);
Serial.println("init");
digitalWrite(enblPin, HIGH);
delay(100);
digitalWrite(enblPin, LOW);
}
void loop() {
int buttonState = digitalRead(pushButton);
int buttonState1 = digitalRead(pushButton1);
Serial.print(buttonState);
Serial.print(" ");
Serial.println(buttonState1);
int sensorValue = analogRead(A0);
int sensorValue1 = analogRead(A1);
// apply the calibration to the sensor reading
speed = map(sensorValue1, 0, 1022, 100, 1000);
Serial.print(speed);
Serial.print(" ");
if ((sensorValue < 900) && (sensorValue > 100)) {
Serial.print("center");
}
if (sensorValue > 900) {
Serial.print("right");
cw();
}
if (sensorValue < 100) {
Serial.print("left");
cc();
}
}
void cw() {
while (1) {
int buttonState = digitalRead(pushButton);
int buttonState1 = digitalRead(pushButton1);
digitalWrite(dirPin, HIGH);
digitalWrite(pulPin, HIGH);
digitalWrite(pulPin, LOW);
delayMicroseconds(speed);
int sensorValue = analogRead(A0);
if ((sensorValue < 900) && (sensorValue > 100)) {
Serial.print("center");
break;
}
if (buttonState == LOW) {
break;
}
}
}
void cc() {
while (1) {
int buttonState = digitalRead(pushButton);
int buttonState1 = digitalRead(pushButton1);
digitalWrite(dirPin, LOW);
digitalWrite(pulPin, HIGH);
digitalWrite(pulPin, LOW);
delayMicroseconds(speed);
int sensorValue = analogRead(A0);
if ((sensorValue < 900) && (sensorValue > 100)) {
Serial.print("center");
break;
}
if (buttonState1 == LOW) {
break;
}
}
}
Comments