Hey guys,
My problem is that, I use potentiometer and can get values between 0 - 1023. However, I try to write serial port is that, if potentiometer value < 500 write 1 else write 2.
My arduino codes is below, However on C# it shows me different datas like 49, 13 ,10 etc. However, I try to get only 1 and 2. I code for it serial.write(1) and serial write(2). I will share my c# codes below.
====Arduino codes===
void setup()
{
Serial.begin(9600);
}
void loop()
{
int position = analogRead(A0);
Serial.println(position);
if(position<=500)
{
Serial.write(1);
Serial.flush();
delay(20);
}
else
{
Serial.write(2);
Serial.flush();
delay(20);
}
}
=================C# codes==============
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Text;
public class cubee : MonoBehaviour {
SerialPort sp = new SerialPort("COM3",9600);
// Use this for initialization
void Start () {
sp.Open ();
sp.ReadTimeout = 2;
}
// Update is called once per frame
void Update () {
if (sp.IsOpen) {
try
{
int asd = sp.ReadChar();
Debug.Log(asd);
}
catch(System.Exception)
{
}
}
}
}
asd values should be 1 or 2 but ıt's values always change like 10, 54, 13 49. I do not understand why. Does anybody can help me?
Thank you