Hi all,
I'm working on a basic project with my robot. The idea is that the robot moves forward until it hits an object with the bumper, then it takes a 90º turn and stops. <-- This all works as expected. Here is my problem area --> Eventually I'm going to take this concept further for data collection purposes, but right now it's simple (no data collection). After the robot stops, I want to make a serial connection and send messages to the computer screen. The problem that I'm having is that my code only works if the robot is hooked up to the robot via USB through the entire program, rather than just in the serial communication code section of the program. How can I modify my code so that the robot is only required to be hooked up to the computer when it actually needs to be?
Here is my code:
int touchCountThreshold = 1;
int motor = 5;
int motorDirection = 4;
int touch = 2;
int touchVal = 0;
int button = 3;
int buttonVal = digitalRead(button);
int touchCount = 0;
void setup () {
pinMode(touch, INPUT);
pinMode(button, INPUT);
pinMode(motor, OUTPUT);
pinMode(motorDirection, OUTPUT);
while(buttonVal == HIGH) {
buttonVal = digitalRead(button);
if(buttonVal == LOW) {break;}
}
}
void loop () {
touchVal = digitalRead(touch);
if (touchVal == LOW && touchCount < touchCountThreshold) {
analogWrite(motor, 0);
touchCount++;
digitalWrite(motorDirection, HIGH);
analogWrite(motor, 255);
delay(4000);
analogWrite(motor, 0);
}
else if(touchVal == HIGH && touchCount < touchCountThreshold) {
digitalWrite(motorDirection, LOW);
analogWrite(motor, 255);
}
if (touchCount >= touchCountThreshold) {
analogWrite(motor, 0);
buttonVal = digitalRead(button);
if(buttonVal == LOW) {
delay(100);
Serial.begin(9600);
Serial.println("Connected");
Serial.end();
}
}
}