I am in the process of writing my code for a roving robot. I am having issues with creating functions within my code.
My goal is to have functions for different actions I would like the robot to perform. I need to get one working correctly then I know I won't have a problem creating additional functions.
myservoA // this servo keeps stopping and starting. I assume my code has set pingPing equal to 7.
// If this is the case how do I create a function based on the data collected from pin 7.
// I have enclosed a copy of my current test code:
#include <Servo.h>
Servo myservoA;
Servo myservoB;
int pos = 90;
int brake = 90;
const int pingPing = 7;
void setup()
{
myservoA.attach(4);
myservoB.attach(3);
Serial.begin(9600);
pinMode(pingPing, INPUT);
}
void loop()
{
myservoA.write(180);
myservoB.write(0);
long duration, inches;
pinMode(pingPing, OUTPUT);
digitalWrite(pingPing, LOW);
delayMicroseconds(2);
digitalWrite(pingPing, HIGH);
delayMicroseconds(5);
digitalWrite(pingPing, LOW);
pinMode(pingPing, INPUT);
duration = pulseIn(pingPing, HIGH);
inches = microsecondsToInches(duration);
Serial.print(inches);
Serial.println();
delay(50);
robotRight();
}
void robotRight()
{
int rightCheck = digitalRead(pingPing);
if (pingPing < 10) {
delay(500);
myservoA.write(90);
delay(2000);
}
else{
myservoA.write(0);
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}