functions within loop

Hello

I’m new to all of this, and of course i have run into some trouble.

I wanted to Control a DC motor with Arduino UNO, ultrasonic sensor and relays. Using my phone as an interface.

So far i have created an app with Five Buttons on it.

Connect - to connect to my Phone with BLE 4.0 (I’ve got that working)

UP - to run motor in one direction (i’ve got that working)

Stop - to stop (i’ve got that working)

Down - to run opposite direction (i’ve got that working)

Load - to control a direction depending of the distance measured by sensor. (Not Working)

I have that working on it own. I have tried to call that function from existing loop i that have for the rest of the buttons but cant get it to work. My knowledge is limited.

I have a code in the file attached. As I said I’m new to all of this. No programming experience. So any help would be much appreciated.

Load Test.ino (2.26 KB)

What does it do when you test?

Can't see your code

SoftwareSerial mySerial(7, 8); // RX, TX

Do you really have a mySerial connected to those pins? Why not use a name that reflects what IS connected?

    unsigned int uS1 = Sonar.ping(); // activates the sensor

That function returns the time that the pulse took to travel out and back, NOT the distance that it traveled. There are other functions that return the distance.

wildbill:
What does it do when you test?

it just toggles relay, changing direction on push of the button

but when i run code on its own than sensor activates relays and changes direction. and it works good.

PaulS:

SoftwareSerial mySerial(7, 8); // RX, TX

Do you really have a mySerial connected to those pins? Why not use a name that reflects what IS connected?

    unsigned int uS1 = Sonar.ping(); // activates the sensor

That function returns the time that the pulse took to travel out and back, NOT the distance that it traveled. There are other functions that return the distance.

That what in numbers they have used in tutorial i had a look at.

Sensor calculates distance and activates relay. I have tested that bit of code and it works on its own.

As i said, i have no programming background, Actually never did this till few days ago. Can some one help me to incorporate this two codes.

wildbill:
What does it do when you test?

Here is the code

#include <NewPing.h>
#include <SoftwareSerial.h>


#define Relay1 2 // define Relay1 as pin#2. This will make the motor 
//spin clockwise.

#define Relay2 4 // define Relay2 as pin#4. This will make the motor 
//spin counter-clockwise.
 
SoftwareSerial mySerial(7, 8); // RX, TX  
// Connect HM10      Arduino Uno
//     Pin 1/TXD          Pin 7
//     Pin 2/RXD          Pin 8

void setup() {  
  Serial.begin(9600);
  // If the baudrate of the HM-10 module has been updated,
  // you may need to change 9600 by another value
  // Once you have found the correct baudrate,
  // you can update it using AT+BAUDx command 
  // e.g. AT+BAUD0 for 9600 bauds
  mySerial.begin(9600);
  pinMode(Relay1, OUTPUT); // setup is easy, just set relays 1 and 2 as
  //an output.
  pinMode(Relay2, OUTPUT);
  

}

void loop() {  
  int c;
  
  if (mySerial.available()) 
  {
    c = mySerial.read();  
    Serial.println("Got input:");
    if(c == 0)
    {      
      sonar();
      // Zero input means "Loading in progress".
      Serial.println("  Loading in progress");
    }
    
    if (c == 1)
    {
      // Input value 1 means "Motor stop".
      Serial.println("  Stop");
       digitalWrite(Relay1, 0);
       digitalWrite(Relay2, 0);
    } 
    if (c == 2)
    {
      // Input value 2 "Motor backwards".
      Serial.println("  UP");
      digitalWrite(Relay1, 1);
      digitalWrite(Relay2, 0);
    } 
    if (c == 3)
    {
      // Non-zero input means "Motor forward".
      Serial.println("  Down");
      digitalWrite(Relay1, 0);
      digitalWrite(Relay2, 1);
    }
  }
}


 void sonar(){
    NewPing Sonar(12, 11, 400);  // Sensor

    unsigned int uS1 = Sonar.ping(); // activates the sensor
    delay(50); // wait 50ms
      // We will declare "if" statements to control the relay module, 
      // which controls the motor. 

    if (uS1 / US_ROUNDTRIP_CM > 20) { //if an object is more than 20 cm away
      //from sensor, 
      Serial.println(" Loading UP ");
      digitalWrite(Relay1, 1);
      digitalWrite(Relay2, 0);
      //the motor will rotate forward.
  } 
    if (uS1 / US_ROUNDTRIP_CM < 20) { //if an object is less than 20 cm away
     //from sensor, 
      Serial.println(" Loading Down ");
      digitalWrite(Relay1, 0);
      digitalWrite(Relay2, 1);
      //the motor will rotate backwards.
  }

Here is the code

Have you run the code? What does it ACTUALLY do?

If you haven't, or can't describe what it actually does, PM me, and I'll send you an address where you can mail the Arduino and any necessary other hardware, and I'll run the damned code and see what it does.

PaulS:
Have you run the code? What does it ACTUALLY do?

If you haven't, or can't describe what it actually does, PM me, and I'll send you an address where you can mail the Arduino and any necessary other hardware, and I'll run the damned code and see what it does.

When I push the button just activates relay and when i push it again just toggles between if statements in function

ignoring
(uS1 /US_ROUNDTRIP_CM > 20)
part of function.

Does this part of function

Serial.println(" Loading UP ");
digitalWrite(Relay1, 1);
digitalWrite(Relay2, 0);

. It seems to me that it does not activate sensor.

SoftwareSerial mySerial(7, 8); // RX, TX

Can you post a picture of your mySerial device? This is STILL a stupid name.

  if (mySerial.available())
  {
    c = mySerial.read(); 
    Serial.println("Got input:");
    if(c == 0)

What is sending this data? Is it REALLY sending binary data?

    unsigned int uS1 = Sonar.ping(); // activates the sensor
    delay(50); // wait 50ms
      // We will declare "if" statements to control the relay module,
      // which controls the motor.

    if (uS1 / US_ROUNDTRIP_CM > 20) { //if an object is more than 20 cm away

You STILL don't know what time the function returned, or what that time corresponds to, distance-wise, but you are CERTAIN that it is greater than 20. I can't figure how you are so sure. Convince me.

PaulS:

SoftwareSerial mySerial(7, 8); // RX, TX

Can you post a picture of your mySerial device? This is STILL a stupid name.

  if (mySerial.available())

{
   c = mySerial.read();
   Serial.println(“Got input:”);
   if(c == 0)



What is sending this data? Is it REALLY sending binary data?



unsigned int uS1 = Sonar.ping(); // activates the sensor
   delay(50); // wait 50ms
     // We will declare “if” statements to control the relay module,
     // which controls the motor.

if (uS1 / US_ROUNDTRIP_CM > 20) { //if an object is more than 20 cm away



You STILL don't know what time the function returned, or what that time corresponds to, distance-wise, but you are CERTAIN that it is greater than 20. I can't figure how you are so sure. Convince me.

Serial is HMSoft BLE 4.0
I connect my phone to it.
Connection works

Void Loop Works
Each button on my Phone App sends a value to Arduino
0
1
2
3

1,2,3 Activate Relays the way they should work

On ‘0’ I call function sonar(). Once i do that function does not work. Relay actuates and on repeat press of the button just changes direction of motor. Toggles… and also everything else stops working. Until I send 0 everything else works. After that nothing.

For sensor I’m using HC-SR04.

When i Upload to Arduino Just this Code it works good. Changes direction of motor depending on distance

#include <NewPing.h>2 // don't forget to include the NewPing library.

#define Relay1 2 // define Relay1 as pin#2. This will make the motor 
//spin clockwise.

#define Relay2 4 // define Relay2 as pin#4. This will make the motor 
//spin counter-clockwise.

NewPing Sonar(12, 11, 400);// Sensor

void setup() {
  pinMode(Relay1, OUTPUT); // setup is easy, just set relays 1 and 2 as
  //an output.
  pinMode(Relay2, OUTPUT);
}
void loop() {

  unsigned int uS1 = Sonar.ping(); // activates the first sensor
  //
 

  // We will declare "if" statements to control the relay module, 
  // which controls the motor. 

  if (uS1 / US_ROUNDTRIP_CM > 20) { //if an object is less than 20 cm away
    //from the front sensor, 
    digitalWrite(Relay1, 1);
    digitalWrite(Relay2, 0);
    //the motor will rotate forward.
  }
  if (uS1 / US_ROUNDTRIP_CM < 20) { //if an object is less than 20 cm away
    //from the back sensor, 
    digitalWrite(Relay1, 0);
    digitalWrite(Relay2, 1);
    //the motor will rotate backwards.
  }
 

}
 void sonar(){
    NewPing Sonar(12, 11, 400);  // Sensor

I missed this earlier. Why are you creating a new instance on every call to this function? There should be one global instance.

If you change the code to read from the serial monitor application, instead, which sends text (so you check for c == '0' instead of c == 0), you can use Serial.print() to tell the Serial Monitor application what time/distance it read, and to see what action(s) it takes.

Once i do that function does not work. Relay actuates and on repeat press of the button just changes direction of motor.

That is not my definition of not working. That would be my definition of not working the way I expect. But, then, I know what to do when code does not do what I expect. That is to eliminate as many variables as I can (the phone and bluetooth are big ones) and to learn as much as I can about what the program is doing, by printing values, etc.