Greetings everyone! I finally decided to sign up and make use of all of your knowledge for my own help. What I need is some input on whether or not there is a way to essentially halt serial monitor or what would be the most logical way of doing so for my program.
To give you an idea of what I am trying to do, I am creating a pan function that will drive a motor either left or right. I am able to drive it with no problems as this isn’t my first rodeo. My trouble is, I will be getting 2 bytes of data sent serially from, in this case, one Arduino to another. I then convert on the receiver side from ASCII to an integer and move the motor positioning to an integer value position with the help of mapping a potentiometer.
int panning = 13;
int panSpeed = 11;
int potValue = A15;
int panBrake = 8;
/***VARIABLES***/
int newVal, currentVal, tempPos;
int serialValue, result;
byte byte1;
char S_input[2];
/***FUNCTIONS***/
void left();
void right();
void brakeOn();
void brakeOff();
void pan();
void setNewValue();
void setCurrentValue(int);
int getNewValue();
int getCurrentValue();
int serialRead(int);
/*********************************************************/
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
pinMode(potValue, INPUT);
pinMode(panning, OUTPUT);
pinMode(panBrake, OUTPUT);
}
void loop()
{
if(Serial1.available()>0)
{
int x, y;
x = setNewValue();
if(Serial1.available())
{
y = setNewValue();
Serial.println(x+y);
delay(1000);
}
}
tempPos = analogRead(potValue);
setCurrentValue(tempPos);
pan();
};
/*********************************************************/
void pan()
{
if(getNewValue() < getCurrentValue())
{
brakeOff();
right();
}
if(getNewValue() > getCurrentValue())
{
brakeOff();
left();
}
else if(getNewValue() == getCurrentValue())
{
brakeOn();
}
}
void setNewValue()
{
S_input[] = x;
delay(5);
int i=0;
while(i<3)
{
S_input[i] = Serial1.read();
i++;
}
Serial1.flush();
result = atoi(S_input);
}
int getNewValue()
{
return result;
}
void setCurrentValue(int x)
{
currentVal = x;
currentVal = map(currentVal, 356, 675, 0, 79);
}
int getCurrentValue()
{
return currentVal;
}
/**********PANNING**********/
void left()
{
digitalWrite(panning, HIGH);
analogWrite(panSpeed, 255);
}
void right()
{
digitalWrite(panning, LOW);
analogWrite(panSpeed, 255);
}
void brakeOn()
{
digitalWrite(panBrake, HIGH);
analogWrite(panSpeed, 0);
}
void brakeOff()
{
digitalWrite(panBrake, LOW);
analogWrite(panSpeed, 0);
}
Forcing an integer value into my “pan” function works 100%. My biggest problem lies either within the conversion from ASCII to integer, or with the serial monitor defaulting a variable to “0” because it hasn’t received any data from one transmission to the next (not sure if this actually happens). These transmissions are more than 30 seconds apart. Using a println statement with my ASCII conversion, I can see that it always prints “0” instead of the value that I send. I have tried numerous things, such as while loops within different functions including the main loop, creating const int variables, and ending the serial monitor, to just name a few, but still no success. I am hoping you bright, strapping individuals could take some time and see if there is a way that you would suggest doing this, and provide me with your input. If you need more information, please feel free to yell at me. By the time I figure this out, I will have no hair left on my head.