Hi, my question is: i have 2 codes that are working but uses 2 print lines. On the print monitor the value are:
35
21
Is there away they print on one line like: 35, 21?
Thanks
Hi, my question is: i have 2 codes that are working but uses 2 print lines. On the print monitor the value are:
35
21
Is there away they print on one line like: 35, 21?
Thanks
Serial.print (35);
Serial.print (',');
Serial.println (21);
Serial.print(distance);
Serial.print(',');
Serial.println(speed);
Too slow
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int FirstDistance=0;
int SecondDistance=0;
double speed=0;
int distance=1;
float Time = 2.0;
float delayedtime = 1000*Time;
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop()
{
GetSpeed();
}
float GetDistance()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
//calculating distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance in cm : ");
Serial.println(distance);
return distance;
}
void GetSpeed(){
FirstDistance = GetDistance(); //get the first distance
delay(delayedtime); //waits 2 seconds depending on the time declared above ,, feel free to change the value dependng on the resolution of your sensor
SecondDistance = GetDistance(); //gets the second distance
speed = (FirstDistance - SecondDistance)/Time; // now calculating the difference
//printing the speed on the serial monitor
Serial.print("the speed (cm/s) is : ");
Serial.println(speed);
}
float ultrasonicRead ()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
//calculating distance
distance= duration*0.034/2;
Serial.println("Arrows, Distance, 0");
{
// Prints the distance on the Serial Monitor
Serial.print("Arrows, Distance, ");
Serial.println(distance);
}
return distance;
}
so what will the code be to have everything on one line?
See replies #2 and #3
Please remember to use code tags when posting code.
Sorry it is my first time and new with this
Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
Everything? Of what does everything consist? Give us an example of how it should look and identify the parts.
(Uncompiled, untested)
// defines pins numbers
const byte trigPin = 9;
const byte echoPin = 10;
unsigned long Time = 2;
unsigned long delayedtime = 1000*Time;
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
digitalWrite (trigPin, LOW);
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop()
{
GetSpeed();
}
float GetDistance()
{
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
unsigned long duration = pulseIn(echoPin, HIGH);
return duration*0.034/2;
}
void GetSpeed()
{
float FirstDistance = GetDistance(); //get the first distance
Serial.print("First Distance in cm : ");
Serial.print(FirstDistance);
delay(delayedtime);
float SecondDistance = GetDistance(); //gets the second distance
Serial.print(" Second Distance in cm : ");
Serial.print(SecondDistance);
float speed = (FirstDistance - SecondDistance)/Time; // now calculating the difference
//printing the speed on the serial monitor
Serial.print(" the speed (cm/s) is : ");
Serial.println(speed);
}
Yes, thank you, and sorry for the mistake. It was my first time. Thanks again it works!
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.