Show Posts
Pages: [1]
1  Using Arduino / Sensors / Re: c# - if else statement in arduino not work if serial.write from c# on: November 23, 2012, 10:15:21 am
thank you very much for your big help smiley
it works  smiley-wink
2  Using Arduino / Sensors / Re: c# - if else statement in arduino not work if serial.write from c# on: November 23, 2012, 05:48:35 am
could u add in my arduino code
im not really understand  smiley-roll
3  Using Arduino / Sensors / Re: c# - if else statement in arduino not work if serial.write from c# on: November 23, 2012, 05:17:11 am
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 :
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;

        }
    }
}
4  Using Arduino / Sensors / Re: c# - if else statement in arduino not work if serial.write from c# on: November 23, 2012, 04:31:51 am
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
5  Using Arduino / Sensors / c# - if else statement in arduino not work if serial.write from c# on: November 23, 2012, 03:39:50 am
hi.
i use leophi for meausure ph, http://www.sparkyswidgets.com/Projects/LeoPhi.aspx
if ph <=7.30, relay will off
and
if ph >=7.31 relay will on

my problem is, when i use code for arduino like :
Code:
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;
 
  if(Serial.available() )
  {

  }
  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); 
}
relay will follow ph condition.

but if i use c# to serial.write to arduino (leophi) :
Code:
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); 
}
relay will on if ph>=7.31, but if ph<=7.30, relay will not off
so, what am i doing wrong in if else statement ?
or anything else i forgot

thanks for your help and advise before  smiley-wink
6  Using Arduino / Sensors / Re: Read Water Level Sensor Data with c# on: November 12, 2012, 11:31:25 pm
hehe .
sorry .
i am new in this forum  smiley-sad

thank you very much for your help.

if i use serial.println in arduino code without input from c# app, i can read via serial monitor the value
but if i use c# app to run the arduino code, when i look at serial monitor, there is no value, empty
why is that ?


7  Using Arduino / Sensors / Read Water Level Sensor Data with c# on: November 12, 2012, 04:52:27 am
hi all .
i want to read analog data from the water level sensor in arduino and in C# application to get data from the analog reading and display to richtextbox in C#

here is the C# code :
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 MenuUtama
{
    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]);
            }
            richTextBox1.Text = " WELCOME ";
        }

        private void button1_Click(object sender, EventArgs e)
        {
                    ser.PortName = comboBox1.Text;
                    ser.Open();
                    ser.Write("1");
                    ser.Close();
         }
          
        private void button2_Click(object sender, EventArgs e)
            {
                ser.PortName = comboBox1.Text;
                ser.Open();
                ser.Write("2");
                ser.Close();
            }
        
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        
    }
}


here is the Arduino code :
Code:
int Relay = 3;

void setup()
{
  pinMode(Relay, OUTPUT);
}

void loop()
{
  while (Serial.available() == 0);
  int nilai = Serial.read()-'0';
  
  if (nilai == 1)
  {
      Serial.println(analogRead(A10));
      delay(1000);
      digitalWrite(Relay, HIGH);
  }
  else if (nilai == 2)
  {
      digitalWrite(Relay, LOW);
  }
  
}

thanks before for your help smiley
Pages: [1]