Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I recently bought a Arduino nano for my personal purpose. I also developed a sketch that can read voltage from nano and can on/off pin D2 and to get the current status of the pin d2(on or off)( status using the variable i). My purpose is to measure voltage and on/off pin d2 manually with same C# programand to know the status of pin at same time . i developed a program now i am printing the result from nano to a label to see whether the pin is on or off(at that time sketch was little bit only for on/off pin d2).

now i added codes to the sketch to measure the voltage. but i dont know how to get 2 different out from this sketch for doing 2 different work in c#. any one help please.

=======
sketch
=======

C#
int LED= 2;
char data;
char x='f';
int analogpin=3;
void setup() {
  {
    Serial.begin(9600);
    pinMode(LED,OUTPUT);
    pinMode(analogpin,INPUT);
    Serial.begin(9600);
  }

}

void loop() {
  ////////////////
   int value = analogRead(analogpin);
  Serial.println(value);
  ////////////////////////////////
 data= Serial.read();

  if  (data=='o')
   {
    digitalWrite(LED,HIGH);
    x = data;
    }
    else if (data=='f')
    {
      digitalWrite(LED,LOW);
      x = data;
      }
///////////////////////////////////
if (data !='o'||data !='f')
  {
    data=x;
    }
//////////////////////////////////////
char i;
  if (data == 'o')
    {
     i='O';
    }
    else if (data == 'f')
      {
        i= 'F';
      }
      else if (data == ' ')
      {
        i='F';
      }
      Serial.println(i);
      
     delay(1000);
}

============================
c# code for on/off pin D2
============================
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public SerialPort myport;
        public Form1()
        {
            InitializeComponent();
            init();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myport.WriteLine("o");
            on.Enabled = false;
            off.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                myport = new SerialPort();
                myport.BaudRate = 9600;
                myport.PortName = "COM5";
                myport.Open();

            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }

            
        }
        private void init()
        {
            
            on.Enabled = true;
            off.Enabled = false;
        }

        private void off_Click(object sender, EventArgs e)
        {
            myport.WriteLine("f");
            on.Enabled = true;
            off.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text =myport.ReadLine();
            
            }
        }
    }
Posted
Updated 12-Aug-15 21:05pm
v3

1 solution

What's wrong in simply adding a 'v' command to your {'o', 'f'} command set just to get the voltage from the arduino?


[update]
Something like
C
if  (data=='o')
 {
   digitalWrite(LED,HIGH);
 }
 else if (data=='f')
 {
   digitalWrite(LED,LOW);
 }
 else if ( data=='v')
 {
   // write voltage measured value on the serial port
 }
 else
 {// handle error (unrecognized command)
 }

Of course you have also to adapt properly the C# side.
[/update]
 
Share this answer
 
v2
Comments
Afzaal Ahmad Zeeshan 13-Aug-15 9:23am    
Then I would suggest that you learn programming first. Suppose you are able to get this one problem to work, what about the next one? And the one that would come later.

Please learn a language and learn the framework. It would be a lot better!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900