This is the properly indented version of your code (CTRL+T in the IDE):
int PHIN = A0;
int Relay = 3;
void setup()
{
pinMode(PHIN,INPUT);
Serial.begin(9600);
pinMode(Relay,OUTPUT);
}
void loop()
{
int phRaw;
float phTmp, phOut, phMiliVolts;
while (Serial.available() == 0);
int nilai = Serial.read()-'0';
if (nilai == 1)
{
phRaw = readADC(PHIN, 16666);
phTmp = (5*(float)phRaw)/1023;
phMiliVolts = phTmp * 1000;
phTmp = (2500-phMiliVolts)/5.25;
phOut = 7-(phTmp/59.16);
if (phOut >= 7.31)
{
digitalWrite(Relay, HIGH);
}
else if (phOut <= 7.30)
{
digitalWrite(Relay, LOW);
}
}
}
unsigned int readADC(int channel, unsigned reading_time)
{
double d;
int i;
unsigned long t0_us;
d = 0.0;
i = 0;
t0_us = micros();
while((micros()-t0_us)<reading_time){
i++;
d += analogRead(channel);
}
d /= i;
return (unsigned int)(d);
}
You are reading the adc and testing for the ph level only when you receive the '1' char. So unless you keep sending '1' to arduino, it won't read the probe and drive the relay.
in c# i create button start, when the button start clicked, it will serial.write("1"); to arduino,
and then, i want arduino receive the value 1, and proses the if else statement.
the if else run, but only first statement in relay on.
so what can i do about that ?
thx btw for quick reply
I'm sorry but I don't understand what you're trying to achieve....
Let me guess... When you turn on the Arduino, it doesn't monitor the pH, so the relay is always off. When the user wants to start measuring, he pushes the button. Arduino receives "1" and starts monitoring the pH. Correct ?
Does it have to stop measuring when the user click the buttons a second time ?
yes, it is.
arduino start measuring ph, only if arduino receive value 1 from c# (in this case is from button start AND only one click)
and start monitoring, if arduino detect ph >= 7.31 then relay will on (by the way i using relay to control aquarium pump),
then if ph <= 7.30 then relay will off
the button start when click once, the button will disable, so user only one click, cannot second click, third click, etc
here is my c# code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace Menu
{
public partial class Form1 : Form
{
SerialPort ser = new SerialPort();
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
string[] availablePorts;
availablePorts = SerialPort.GetPortNames();
for (int i = 0; i < availablePorts.Length; i++)
{
comboBox1.Items.Add(availablePorts[i]);
}
}
//button start for ph
private void button1_Click(object sender, EventArgs e)
{
ser.PortName = comboBox1.Text;
ser.Open();
ser.Write("1");
ser.Close();
button1.Enabled = false;
}
}
}
Ok then on Arduino you need to use a state variable that says "monitor the ph" and "don't monitor the ph". It could be a boolean variable, for example.
Let's call it isMonitoringActive. Or something like that.
Then you put the ph monitoring code under if(isMonitoringActive) {} statement.
When you receive "1" from the serial port, you change the value of the boolean flag, from false to true and vice-versa.
This pseudocode should be clear enough for you to translate to actual code:
boolean monitorng = false // this goes outside loop and setup
// this goes in loop
if serial.available
ch = serial.read
if ch == '1'
monitoring = !monitoring // if you receive a '1' character, switch from monitor to not monitor mode
endif
endif
if monitoring // if in monitor mode, read ph and control relay
// the ph and relay control code you already wrote
}
It's my understanding that you have to use threading and Delegates when using
Serial comms and Windows forms in C#. IOW, Serial comms runs in a background
thread, and the Forms in the foreground thread. See .pdf source file here,
You may have looked at some of the uses of the Serial Port Class in the source code for the Simple Terminal from Workshop 18. You may even have had one of those ‘what the…’ moments with some of the delegate stuff. Put simply, the code that is communicating with the serial port isn’t actually running with the code for the GUIs. This concerns ‘threads’ or bits of software that are running separately in Windows (each gets a part of each second to run so that all the threads seem like they are running at the same time to the user). When you want one thread to put stuff in another thread you set a delegate that the first thread can call from the second thread. So we have to create a delegate in our GUI that allows the serial port thread to set the text in our richTextBox. Yeah, its complex, but we don’t really have to go too deep at this point just to use the serial port. This is one of those cases where cookbook coding really comes in handy and you can safely just use the source code and move on till you have time to burn out a few more brain cells and learn the details. If you get to that point you might consider the book: Virtual Serial Port Cookbook available on both Nuts&Volts and Smiley Micros.