Hi guys! I'm trying to control a mobile robot using my MATLAB GUI and a set of sensors. The thing is, I got the GUI and Arduino Communication part working already however i cant get the sensor inputs to work.
The set-up that I want is like this:
My PC (Matlab GUI) sends a character ('A' for example) to my arduino wirelessly and then the arduino will run the motors continuosly until otherwise turned off by the triggering of the sensor or when I send a character 'B' instead.
Note: my proximity sensor will return "LOW" if it detects anything
int serialData = 0;
int proxpin=6;
void setup()
{
Serial.begin(9600);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(proxpin, INPUT);
}
void loop()
{
if (Serial.available() > 0)
{
serialData = Serial.read();
if (serialData == 'A')
{
do
{
digitalWrite(8, LOW);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(1000);
}
while (digitalRead(proxpin) == LOW);
}
else if (serialData =='B')
{
digitalWrite(8, LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(1000);
}
}
}
Hope someone could help. This is the first time I post something here although I frequently read discussions here.
Have you got a test sketch that does nothing but read proxpin and maybe switch an led off and on, or serial print a "hi" or "lo", to make sure the sensor's doing what you expect?
EDIT... I'm hoping you put the serial prints in your main code (where the original problem is / was) not just the test code to see if the sensor works 8)
it still is when I try to combine it with the communication side.
best description of how i wanted it to be:
void loop()
{
//Listen for any inputs and don't do anything yet until it receives something
//if "A" is received run the motors continuously but stop when the sensor detects anything
//if "B" is received stop the motors
}
This Code works for running it continuously when sending A and Stopping when sending B.
It will run again if I send A again coming from a stop.
However, When I tried to stop it with the 'proxpin' (sensor) its not stopping
int serialData = 0;
int proxpin =6;
void setup()
{
Serial.begin(9600);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(proxpin,INPUT);
}
void loop()
{
if (Serial.available() > 0)
{
Serial.println("listening");
serialData = Serial.read();
switch(serialData)
{
case 'A':
Serial.println("you sent a");
forward();
break;
case 'B':
Serial.println("you sent b");
stopna();
break;
}
}
}
void forward()
{
if (digitalRead(proxpin) == HIGH)
{
Serial.println("running forward");
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
}
else if (digitalRead(proxpin) == LOW)
{
Serial.println("sensor stopped me");
stopna();
}
}
void stopna()
{
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
figured there is something wrong here cause its not printing "sensor stopped me" when I trigger the sensor.