How to control a loop using a wireless input and a sensor input

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.

Thanks :slight_smile:

cant get the sensor inputs to work

Do they work without all the serial comms stuff?
Maybe consider getting rid of all the delays?

tried to get rid of the delays but its still the same :~

Any ways here is the another code that i tried if it would be of help

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 forward(){
  
  while (digitalRead(proxpin) == HIGH) 
  {
  
  digitalWrite(8, LOW);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);
  digitalWrite(11,LOW);
  }
  
}
void stopna()
{
  digitalWrite(8, LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
}

  
void loop()
{
  if (Serial.available() > 0)
  {
   serialData = Serial.read();
   
  switch(serialData)
{
case 'A':
running();  

case 'B': 
stopna();
break;


default: Serial.println("Invalid"); 
break;
}
}
}

void running()
{
  if (digitalRead(proxpin) == LOW) {
stopna();
  }
else
{
forward();
}
}

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?

Here is what I tried just now to check the sensor input. And yes the sensor input works :~

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 (digitalRead(proxpin) == LOW)
  
  {
  digitalWrite(8, LOW);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);
  digitalWrite(11,LOW);
  delay(3000);
  }
}

the motors will run when I trigger my sensor.

Cool... next try some serial.prints in various parts of the code to deliver messages to say where the flow is taking you. Like here for instance...

void loop()

{
  if (digitalRead(proxpin) == LOW)
  
  {
  Serial.print("in loop, proxpin is low"); //<<<<<<<<<<<<<<<<<<<<<<<<< eg
  digitalWrite(8, LOW);
  digitalWrite(9,HIGH);

Done.

The Software Serial Monitor is returning me my:
Serial.println("proxpin is low")
Serial.println("program good")

right from the setup and the loop

:slight_smile:

So that means the logic is taking you to the right places in the code, under a given set or sequence of inputs?

.... which means that this:

however i cant get the sensor inputs to work.

.... is no longer true?

Or is there still some prob?

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

}

And have you got serial prints in THAT code, so see if the flow is correct? Like maybe here:

void loop()
{
  if (Serial.available() > 0)
  {
 Serial.print("listening");  //<<<<<<<<<<<<<<<<<<<<<<<<<
   serialData = Serial.read();
   
  switch(serialData)
{
case 'A':
Serial.print("in case a- running"); //<<<<<<<<<<<<<<<<<<<<<<<<<<
running();  

case 'B': 
stopna();
break;
int serialData = 0;

void setup()
{
Serial.begin(9600);
}
void loop()
{
  if (Serial.available() > 0)
  {
 Serial.println("listening");  //<<<<<<<<<<<<<<<<<<<<<<<<<
   serialData = Serial.read();
   
  switch(serialData)
{
case 'A':
Serial.println("you sent a"); //<<<<<<<<<<<<<<<<<<<<<<<<<<
break; 

case 'B': 
Serial.println("you sent b");
break;
}
  }
}

uploaded this sketch. the serial monitor returned me:

listening
you sent a
listening
you sent b
listening
you sent a
listening
you sent a
listening
you sent b

It responded when i send A or B :slight_smile:

Ok, and that's presumably correct?

So now add the proxpin stuff, with serial prints stuck in the appropriate places and see where that takes you...

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.

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();
}
}

Finally was able to spot it and It works now. :slight_smile:
Thanks for the help.

I just edited this part:

void forward()
{

while (digitalRead(proxpin) == HIGH)
{

   Serial.println("running forward");
   digitalWrite(8,LOW);
   digitalWrite(9,HIGH);
   digitalWrite(10,HIGH);
   digitalWrite(11,LOW);
       
}

if (digitalRead(proxpin) == LOW)
{
  Serial.println("sensor stopped me");
  stopna();
}
}

how did i forget about 'while' haha :slight_smile: thanks

Yeah... I just realised much the same solution.

Shows how usedful serial prints are though.

Your problem now is, while you're in your loop, you're not checking the serial...

Hehe yep...

Just thinking my solution which I was working on, and deleted, was going to be different.

My plan was to put the logic in the case A part, and onlt send to forward() if proxpin was high, otherwise stop it then.

Thanks. I'm trying to make it work now with the wireless thing. It works with the serial monitor but not when interfaced with my GUI.

Could my problem now be what you guys are saying?
How do I do it then? :slight_smile:

Try this.... it only listens when prox is high. Does that match what you want to do?

In my code I have proxpin pulled up, since I have a simple button. Pullups makes it active low, like your sensor.

int serialData = 0;
int proxpin =6;

void setup()
{
  Serial.begin(9600);
  Serial.print("starting");
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(proxpin,INPUT_PULLUP);
}

void loop()
{
while (digitalRead(proxpin)==HIGH) {

  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;
      } //case
    }//serial available

}//while prox high

stopna();
Serial.println("prox is low");

} //loop

EDIT btw I find it useful to comment the closing }'s so I can see at a glance which is which