int pingPin = 7;
void setup()
{
Serial.begin(9600);
Serial.print("?Bff");
delay (100);
Serial.print("?G420"); //set screen to be 4x20
delay (100);
}
void loop()
{
//Serial.print("?f"); //clear screen
long duration, cm;
// triggers the ping sensor with the sequence
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// Set pinPing to read the time it takes to get a HIGH back
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//Convert to centimeters using the method
cm = microsecondsToCentimeters(duration);
//Prints the RESULT! WOO!!!
Serial.print (cm);
Serial.println (" cm");
delay (500);
}
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;
}
That's my arduino's code....... very simple... when I do serial monitor with The Arduino software, I get a three or two digit number then a space, then a cm.
My code for C#.... this version only run ONCE the reading, but it work. I tried to put while loop in it, but it wont work at all, wont load up the Gui... so I figure this is probably the first step.
The objective is to "display" the correct picture correlating to the amount of water in the pitcher depending on the distance from the water to the U/S sensor. Sorry if my code is a litte messy.
namespace WaterPitcher
{
public partial class WaterMeterGUI : Form
{
public SerialPort com1 = new SerialPort
("COM16", 9600, Parity.None, 8, StopBits.One);
public WaterMeterGUI()
{
InitializeComponent();
}
private void WaterMeterGUI_Load(object sender, EventArgs e)
{
try
{
com1.PortName = "COM16";
com1.BaudRate = 9600;
com1.Parity = Parity.None;
com1.DataBits = 8;
com1.StopBits = StopBits.One;
com1.Handshake = Handshake.None;
com1.ReadTimeout = 1000;
com1.WriteTimeout = 500;
com1.Open();
com1.DiscardInBuffer();
string READ = "";
READ = com1.ReadLine();
string[] Split = READ.Split(new Char[] { ' ' });
//SHOW RESULT
double result = Convert.ToDouble(Convert.ToString(Split[0]));
lblresult.Text = Convert.ToString(result);
if (result < 3)
{
SetVis();
this.pic16.Visible = true;
}
else if (result >= 3 && result < 5)
{
SetVis();
this.pic15.Visible = true;
}
else if (result >= 5 && result < 7)
{
SetVis();
this.pic14.Visible = true;
}
else if (result >= 7 && result < 9)
{
SetVis();
this.pic13.Visible = true;
}
else if (result >= 9 && result < 11)
{
SetVis();
this.pic12.Visible = true;
}
else if (result >= 11 && result < 13)
{
SetVis();
this.pic11.Visible = true;
}
else if (result >= 13 && result < 15)
{
SetVis();
this.pic10.Visible = true;
}
else if (result >= 15 && result < 17)
{
SetVis();
this.pic9.Visible = true;
}
else if (result >= 17 && result < 19)
{
SetVis();
this.pic8.Visible = true;
}
else if (result >= 17 && result < 19)
{
SetVis();
this.pic7.Visible = true;
}
else if (result >= 19 && result < 21)
{
SetVis();
this.pic6.Visible = true;
}
else if (result >= 21 && result < 23)
{
SetVis();
this.pic5.Visible = true;
}
else if (result >= 23 && result < 25)
{
SetVis();
this.pic4.Visible = true;
}
else if (result >= 25 && result < 27)
{
SetVis();
this.pic3.Visible = true;
}
else if (result >= 27 && result < 29)
{
SetVis();
this.pic2.Visible = true;
}
else if (result >= 29 && result < 31)
{
SetVis();
this.pic1.Visible = true;
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message.ToString(), "ERROR");
}
}
void SetVis()
{
this.picempty.Visible = false;
this.pic16.Visible = false;
this.pic15.Visible = false;
this.pic14.Visible = false;
this.pic13.Visible = false;
this.pic12.Visible = false;
this.pic11.Visible = false;
this.pic10.Visible = false;
this.pic9.Visible = false;
this.pic8.Visible = false;
this.pic7.Visible = false;
this.pic6.Visible = false;
this.pic5.Visible = false;
this.pic4.Visible = false;
this.pic3.Visible = false;
this.pic2.Visible = false;
this.pic1.Visible = false;
}
}
}