Simple Code Correction I cant figure out

I am trying to activate a motor with a proximity sensor based on distance. I can write most of the code, however I am writing something wrong. The Serial.print (inches) prints the distance that I want to activate the motor, but I cannot use this (inches) print. I have tried Serial.read, analogRead, and cannot make the arduino uno respond to the Serial.print (inches) with a analogWrite Command. Any suggestions. All is wired correctly . Here is the Code

const int pingPin = A0; const int motorPin = A3; const int motorPin2 = A5; // int control;

void setup() {

// initialize serial communication: Serial.begin(9600);

}

void loop() { // establish variables for duration of the ping, // and the distance result in inches and centimeters: long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH);

// convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration);

Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println();

//inches = read (); //inches = Serial.read (); //Control =analogRead (inches) delay(100);

if (inches> 1) { analogWrite(motorPin, HIGH); analogWrite(motorPin2, HIGH);

}

}

long microsecondsToInches(long microseconds) { // According to Parallax's datasheet for the PING))), there are // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per // second). This gives the distance travelled by the ping, outbound // and return, so we divide by 2 to get the distance of the obstacle. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf return microseconds / 74 / 2; }

long microsecondsToCentimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. return microseconds / 29 / 2; }

sRedCustom: I am trying to activate a motor with a proximity sensor based on distance. I can write most of the code, however I am writing something wrong. The Serial.print (inches) prints the distance that I want to activate the motor, but I cannot use this (inches) print. I have tried Serial.read, analogRead, and cannot make the arduino uno respond to the Serial.print (inches) with a analogWrite Command. Any suggestions. All is wired correctly .

First off, code goes in CODE tags, secondly, what do you mean by "make the arduino uno respond to the Serial.print". Are there two Arduinos?

First off read the how to use this forum sticky post and edit that first post according to how to post code.

but I cannot use this (inches) print. I have tried Serial.read, analogRead, and cannot make the arduino uno respond to the Serial.print (inches) with a analogWrite Command.

I have no idea what this means or what you actually want to do.
Say what your code does and what you want it to do.

Inches is what you calculated, based on the sensor. So what exactly are you trying to do? Make the motor move to that spot and stop?

You can't do an analogWrite() to pins A3 and A5!.

Mark

When the Serial.Print(inches); reads above 2 inches(just an example), I want the motor to turn on. How do I translate this into a if or while statement?

if(inches >= 2) { // motors on } else // off

That is the statement already in the code. It does not work. I think that the arduino needs a read command or something. The other reply says that analogWrite cannot be used, but I have used it on multiple occasions. Please provide an alternative.

sRedCustom: That is the statement already in the code. It does not work. I think that the arduino needs a read command or something. The other reply says that analogWrite cannot be used, but I have used it on multiple occasions. Please provide an alternative.

Why are you trying to analogWrite a value of HIGH? HIGH is used for digitalWrites. analogWrites expect a value from 0-255. What kind of motor are you trying to drive? Can you get it to drive independently? How is it hooked up?

Question, are these motor pins for two seperate motors or just one motor? If they are for ONE motor, the pins need to be reversed from each other. Also note that the arduino can NOT drive the motor(s) without an actual motor driver or H-Bridge.

if(inches >= 2) { // two seperate motors digitalWrite(motorPin, HIGH); digitalWrite(motorPin2, HIGH); } else { digitalWrite(motorPin, LOW); digitalWrite(motorPin2, LOW);

//-------------------------------------------------------------- if(inches >= 2) { // Single motor digitalWrite(motorPin, HIGH); // swap the HIGH and LOW to change direction if needed. OR add another "else if" statement digitalWrite(motorPin2, LOW); } else { digitalWrite(motorPin, LOW); digitalWrite(motorPin2, LOW);

sRedCustom:
That is the statement already in the code. It does not work. I think that the arduino needs a read command or something. The other reply says that analogWrite cannot be used, but I have used it on multiple occasions. Please provide an alternative.

I see you are not polite enough to edit that first post like you were requested.

The other reply says that analogWrite cannot be used, but I have used it on multiple occasions.

No it did not!! The other reply said you can not use it on the pins you used it on. The pins A3 and A5 are not capable of producing a PWM signal. If you don’t know what PWM is then read this:-
http://www.thebox.myzen.co.uk/Tutorial/PWM.html

Please provide an alternative

A simple digitalWrite(pin, HIGH) will turn a pin on after is is set up as an output in the setup function.

There are two motors, the code is now turning them on. However once the statement begins my serial monitor stops showing active distances and reads 0 inches. I believe this is because I am not including something in the while statement. Any comment is much appreciated.

const int pingPin = 11;
const int motorPin = 9;
const int motorPin2 = 10;
// int control;


void setup() {
 pinMode(motorPin, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);

}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
 
  

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
   

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println(); 
 

  //inches = read ();
  //inches = Serial.read ();
  //Control =analogRead (inches)
  delay(100);
   
  
 while (inches <= 8) { // two seperate motors
digitalWrite(motorPin, HIGH);
digitalWrite(motorPin2, HIGH);

long duration, inches, cm;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println(); 

  delay(100);
  

  
}
if (inches > 8){
digitalWrite(motorPin, LOW);
digitalWrite(motorPin2, LOW);
  
  }

}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

However once the statement begins my serial monitor stops showing active distances and reads 0 inches.

Can you do a test to eliminate any possibility of interference from your motors. Take the same code but just remove the wiring to the motors. Do you still see the same effect of just reading 0 inches? If you do you have a software problem, if you don't then you have a hardware problem.

You can’t do an analogWrite() to pins A3 and A5!.

You can, but it won’t do what you expect.
(Unless you’ve read the code)

when I remove the motors the problem is fixed.

sRedCustom: when I remove the motors the problem is fixed.

So your problem is either:- 1) Electrical noise from the motors - if so add decoupling to them - http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html 2) The supply can not supply enough current to drive both motors - measure the voltage when they are running and make sure the 5V does not drop - cure if it does is to get a better power supply.