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.
}
}