Serial Port or Bluetooth Problem

Hi, firstly sorry my bad english. I work with bluetooth. If i use this code, work greatly and the change reacts instantly.

#include<SoftwareSerial.h>

const int sol_i = 4;
const int sol_g = 5;
const int sag_i = 2;
const int sag_g = 3;


char data;

void setup()
{
  pinMode(sag_i, OUTPUT); 
  pinMode(sag_g, OUTPUT);
  pinMode(sol_i, OUTPUT);
  pinMode(sol_g, OUTPUT);
  Serial.begin(9600);
}

void loop()
{

if (Serial.available())
{
    data=Serial.read();
    Serial.println(data);
    
  if(data=='1'){          // bluetoothdan 1 girildi
    digitalWrite(sag_i, HIGH);
    digitalWrite(sag_g, LOW);
    digitalWrite(sol_i, HIGH);
    digitalWrite(sol_g, LOW);
    }

   else if(data=='0'){          
    digitalWrite(sag_i, LOW);
    digitalWrite(sag_g, LOW);
    digitalWrite(sol_i, LOW);
    digitalWrite(sol_g, LOW);
    }
   
     }
}

When i use this code, data==1 works but data==0 not work or working too late.

#include<SoftwareSerial.h>

const int trig =6;
const int echo= 7;
char data;

const int sol_i = 4; 
const int sol_g = 5;
const int sag_i = 2;
const int sag_g = 3;

const int sol_sensor1 = 13;
const int sol_sensor2 = 12;
const int orta_sensor = 11;
const int sag_sensor1 = 10;
const int sag_sensor2 = 9;

int S1, S2, S3, S4, S5; 


int  sure = 0; 
int mesafe = 0;
int count = 1;


void setup()

{
  pinMode(trig , OUTPUT); 
  pinMode(echo , INPUT );
  pinMode(sol_i, OUTPUT); 
  pinMode(sol_g, OUTPUT);
  pinMode(sag_i, OUTPUT);
  pinMode(sag_g, OUTPUT);

  pinMode(sol_sensor1, INPUT); 
  pinMode(sol_sensor2, INPUT);
  pinMode(orta_sensor, INPUT);
  pinMode(sag_sensor1, INPUT);
  pinMode(sag_sensor2, INPUT);
    Serial.begin(9600);
}



void loop()
{
  if (Serial.available())
{
    data=Serial.read();
    Serial.println(data);
if(data=='1'){     
    
 for(int i=0; i<1; i++)
{
arama();
}

{
  digitalWrite(trig , HIGH); 
  delayMicroseconds(1000);  
  digitalWrite(trig ,  LOW); 

  sure = pulseIn(echo , HIGH);
  mesafe = (sure / 2) / 28.5;
  
  if (mesafe > 30)
{
  
  S1 = digitalRead(sol_sensor1);
  S2 = digitalRead(sol_sensor2);
  S3 = digitalRead(orta_sensor);
  S4 = digitalRead(sag_sensor1);
  S5 = digitalRead(sag_sensor2);
  
  if (S1 == 1 && S2 == 1 && S4 == 1 && S5 == 1 && S3 == 0) 
  {
    digitalWrite(sag_i, HIGH);
    digitalWrite(sag_g, LOW);
    digitalWrite(sol_i, HIGH);
    digitalWrite(sol_g, LOW);
  }
  else if ((S1 == 1 && S2 == 1) || ((S1 == 1) && (S2 == 1)) && S4 == 0 && S5 == 0 && S3 == 0)
  {
    digitalWrite(sag_i, HIGH);
    digitalWrite(sag_g, LOW);
    digitalWrite(sol_i, LOW);
    digitalWrite(sol_g, LOW);
  }
  
  else if ((S4 == 1 && S5 == 1) || ((S4 == 1) && (S5 == 1)) && S1 == 0 && S2 == 0 && S3 == 0) 
  {
    digitalWrite(sag_i, LOW);
    digitalWrite(sag_g, LOW);
    digitalWrite(sol_i, HIGH);
    digitalWrite(sol_g, LOW);
  }
   else if(S1 == 0 && S2 == 0 && S4 == 0 && S5 == 0 && S3 == 0) 
  {
    digitalWrite(sag_i, LOW);
    digitalWrite(sag_g, LOW);
    digitalWrite(sol_i, HIGH);
    digitalWrite(sol_g, LOW);
  }

    else if(S1 == 1 && S2 == 0 && S4 == 0 && S5 == 1 && S3 == 0)
  {
    digitalWrite(sag_i, HIGH);
    digitalWrite(sag_g, LOW);
    digitalWrite(sol_i, HIGH);
    digitalWrite(sol_g, LOW);
  }

  
    else if(S1 == 0 && S2 == 0 && S4 == 0 && S5 == 1 && S3 == 0) 
  {
    digitalWrite(sag_i, HIGH);
    digitalWrite(sag_g, LOW);
    digitalWrite(sol_i, LOW);
    digitalWrite(sol_g, LOW);
  }

      else if(S1 == 1 && S2 == 0 && S4 == 0 && S5 == 0 && S3 == 0) 
  {
    digitalWrite(sag_i, HIGH);
    digitalWrite(sag_g, LOW);
    digitalWrite(sol_i, LOW);
    digitalWrite(sol_g, LOW);
  }
  
  
  delay(25);                     
  digitalWrite(sag_i, LOW); 
  digitalWrite(sag_g, LOW);
  digitalWrite(sol_i, LOW);
  digitalWrite(sol_g, LOW);
  delay(50);
}
 
 else
 {
      digitalWrite(sag_i, LOW);
    digitalWrite(sag_g, LOW);
    digitalWrite(sol_i, LOW);
    digitalWrite(sol_g, LOW);
  }
 }
}
  else if(data=='0'){         
    digitalWrite(sag_i, LOW);
    digitalWrite(sag_g, LOW);
    digitalWrite(sol_i, LOW);
    digitalWrite(sol_g, LOW);
    }
    }
}

void arama()
{
  
  
while(S1 == 1 && S2 == 1 && S4 == 1 && S5 == 1 && S3==1)
  {

   S1 = digitalRead(sol_sensor1); 
  S2 = digitalRead(sol_sensor2);
  S3 = digitalRead(orta_sensor);
  S4 = digitalRead(sag_sensor1);
  S5 = digitalRead(sag_sensor2);

    digitalWrite(trig , HIGH); 
  delayMicroseconds(1000);   
  digitalWrite(trig ,  LOW); 

  sure = pulseIn(echo , HIGH);
  mesafe = (sure / 2) / 28.5;
  
  if (mesafe < 10 ) 
  {
    delay(150);
    digitalWrite(sol_i ,  LOW);
    digitalWrite(sol_g , HIGH);
    digitalWrite(sag_i ,  LOW);
    digitalWrite(sag_g , LOW);
    delay(150);

  }
  else 
  {
    delay(150);
    digitalWrite(sol_i , HIGH);
    digitalWrite(sol_g ,  LOW);
    digitalWrite(sag_i , HIGH);
    digitalWrite(sag_g ,  LOW);
    delay(150);
}
 delay(25);                
  digitalWrite(sag_i, LOW); 
  digitalWrite(sag_g, LOW);
  digitalWrite(sol_i, LOW);
  digitalWrite(sol_g, LOW);
  delay(50);
  }
  }

What is the problem ?

Thanks.

Possibly the use of delay. Get rid of it in favour of a millis based approach (there is a sticky at the top of this section if the forum about the use of millis).

sterretje:
Possibly the use of delay. Get rid of it in favour of a millis based approach (there is a sticky at the top of this section if the forum about the use of millis).

Thanks for answer. I will try. If there is any other opinion other friend, i will wait of your solutions.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

templarx:
When i use this code, data==1 works but data==0 not work or working too late.

#include<SoftwareSerial.h>

void setup()

{
    Serial.begin(9600);
}


What is the problem ?

It might be because you are calling software serial and not using it

sterretje:
Possibly the use of delay. Get rid of it in favour of a millis based approach (there is a sticky at the top of this section if the forum about the use of millis).

i delete all delay function but there was no solution :frowning:

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

I was research it but i didnt find solution.

Nick_Pyner:
It might be because you are calling software serial and not using it

i think i was using this:

 if (Serial.available())
{
    data=Serial.read();
    Serial.println(data);
if(data=='1'){

thanks but the problem continues

templarx:

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

I was research it but i didnt find solution.

Post the code that you tried based on my examples.

...R

Robin2:
I was research it but i didnt find solution.

Post the code that you tried based on my examples.

...R

I didn't understand what i should do :frowning:

templarx:
I didn't understand what i should do :frowning:

Then let's start at the beginning.

What do you want to do?
Something sends data to the Arduino - what is sending the data?
What data do you want to send to the Arduino? - give some examples
What do you want the Arduino to do when it receives the data?

...R

Firstly thank you for interest. I want to start-stop motors with bluetooth. My bluetooth module HC06. I use 0 and 1 pins on arduino. I can send data like "1" "0" "a" "b" etc with my phone (android interface).

void loop()
{
  if (Serial.available())
{
    data=Serial.read();
    Serial.println(data);
if(data=='1'){   
.
.
.
}
  else if(data=='0'){         
    digitalWrite(sag_i, LOW);
    digitalWrite(sag_g, LOW);
    digitalWrite(sol_i, LOW);
    digitalWrite(sol_g, LOW);
    }
    }

I want to send data "1" and then want to start motors. I already do it. After that i cant send data "0" for motors stop. It didnt take data or take data too late.

I want momentary reaction.

templarx:
I want momentary reaction.

I think we are all getting confused here.

You said in your Original Post that you have a program that "work greatly and the change reacts instantly"

And also in your Original Post yo had a program that "not work or working too late"

Then you were told in Reply #1 that the problem is due to your use of delay().

Have a look at how millis() is used to manage timing without blocking in Several things at a time.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

Robin2:
I think we are all getting confused here.

You said in your Original Post that you have a program that "work greatly and the change reacts instantly"

And also in your Original Post yo had a program that "not work or working too late"

Then you were told in Reply #1 that the problem is due to your use of delay().

Have a look at how millis() is used to manage timing without blocking in Several things at a time.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

My first post include 2 codes.

First code for only start-stop motors and work greatly and reacts instantly.

Second code include line-following,distance measurement and bluetooth connection. My problem is in the second code. I was do same things but not work like first code.

Also i remove all delay functions but even so not work

If you send '1', the first thing the code does is call arama(). arama() contains a while loop; you only get out of that while loop if all sensors are HIGH. Are they? Add some serial prints after reading the sensors in the sensors.

You're using pins 0 and 1 for the HC06. Does your Android application allow to display received data? Or do you use serial monitor in parallel to the results of the serial print statements?

templarx:
Also i remove all delay functions but even so not work

You need to post the latest version of your program so we can see exactly what you have done.

...R

sterretje:
If you send '1', the first thing the code does is call arama(). arama() contains a while loop; you only get out of that while loop if all sensors are HIGH. Are they? Add some serial prints after reading the sensors in the sensors.

You're using pins 0 and 1 for the HC06. Does your Android application allow to display received data? Or do you use serial monitor in parallel to the results of the serial print statements?

Thanks i deleted arama() and now works bluetooth but i need use this --> arama()

There is a different problem now. When i send "1", the engine is running according to the current works sensor and if change sensors, engine not change.

Robin2:
You need to post the latest version of your program so we can see exactly what you have done.

...R

i added

semiwork.txt (5.03 KB)

In general do not use WHILE because it blocks until it finishes. Use IF and allow loop() to do the repetition.

delayMicroseconds() causes exactly the same problem as delay()

All those complex IF statements such as

if (S1 == 1 && S2 == 1 && S4 == 1 && S5 == 1 && S3 == 0)

followed by ELSE can very easily be mixed up

I suggest that you cascade the the IF statements like this

if (S1 ==) {
    if(S2 == 1) {
      // etc etc

then an ELSE will only relate to a single IF and the whole lot will also probably be shorter.

Use the AutoFormat tool to indent your code consistently - it will make it much easier to read

And if it was my program I would make much greater use of functions. Have a look at Planning and Implementing a Program

...R

Robin2:
In general do not use WHILE because it blocks until it finishes. Use IF and allow loop() to do the repetition.

thanks so much i will try these