Go Down

Topic: AnalogRead error (Read 2410 times) previous topic - next topic

edgardmi

Hello,

Im trying to read a value from a LM35 at PIN A0 and I getting the follow error:

Cannot convert from 'int' to 'string'


Could anyone help me?

Project: Arduino UNO + Raspberry pi 3 + Visual Studio 2017 Pro + Windows 10 iot Core

Thank you very much!

Code: [Select]


using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Microsoft.Maker.Serial;
using Microsoft.Maker.RemoteWiring;
using Microsoft.Maker.Firmata;


namespace Raspberry
{

    public sealed partial class MainPage : Page
    {
        private UsbSerial connection;
        private RemoteDevice arduino;
        private UwpFirmata firmata;
        private const byte led1 = 13;
        private const byte led2 = 12;
        private DispatcherTimer readTemperatureTimer;
        private const int lm35 = 14;

        public MainPage()
        {
            this.InitializeComponent();
            this.Unloaded += MainPage_Unloaded;
       
            InitWRA();
         
        }

        private void InitWRA()
        {
            connection = new UsbSerial("VID_0403", "PID_6001");
            firmata = new UwpFirmata();
            arduino = new RemoteDevice(firmata);

            firmata.begin(connection);

            connection.ConnectionEstablished += Connection_ConnectionEstablished;
           
            connection.begin(57600, SerialConfig.SERIAL_8N1);
        }

        private void Connection_ConnectionEstablished()
        {
            arduino.pinMode(led1, PinMode.OUTPUT);
            arduino.pinMode(led2, PinMode.OUTPUT);
            arduino.pinMode(lm35, PinMode.ANALOG);
            textConnection.Text = "Connected";
            var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() =>
            {
                setup();
            }));
        }

        private void setup()
        {
            //Set the timer to schedule every 500 ms.
            readTemperatureTimer = new DispatcherTimer();
            readTemperatureTimer.Interval = TimeSpan.FromMilliseconds(500);
            readTemperatureTimer.Tick += readTemperature;
            readTemperatureTimer.Start();
        }

        private void Onbutton_Click(object sender, RoutedEventArgs e)
        {
            arduino.digitalWrite(led1, PinState.HIGH);
            Onbutton.Visibility = Visibility.Collapsed;
            Offbutton.Visibility = Visibility.Visible;
        }

        private void MainPage_Unloaded(object sender, RoutedEventArgs e)
        {
            arduino.Dispose();
        }

        private void Offbutton_Click(object sender, RoutedEventArgs e)
        {
            arduino.digitalWrite(led1, PinState.LOW);
            Offbutton.Visibility = Visibility.Collapsed;
            Onbutton.Visibility = Visibility.Visible;
        }

        private void readTemperature(object sender, object e)
        {
            //Read analog value from 0 to 1023, which maps from 0 to 5V
            int temperatureValue = arduino.analogRead(lm35);

            //Convert analog value to temperature.
            double temperature = (500.0 * temperatureValue) / 1024.0;

            //Print temperature to Output.
            textTemp1.Text = temperature.ToString() ;
        }
    }
}

edgardmi

Hello!

I found the solution:


Code: [Select]

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Microsoft.Maker.Serial;
using Microsoft.Maker.RemoteWiring;
using Microsoft.Maker.Firmata;

/
namespace Raspberry
{

    public sealed partial class MainPage : Page
    {
        private UsbSerial usb;
        private RemoteDevice arduino;
        private const byte led1 = 13;
        private DispatcherTimer readTemperatureTimer;

        public MainPage()
        {
            this.InitializeComponent();

            usb = new UsbSerial("VID_0403", "PID_6001");
            arduino = new RemoteDevice(usb);
            arduino.DeviceReady += onDeviceReady;

            usb.begin(57600, SerialConfig.SERIAL_8N1);
        }

       

        private void onDeviceReady()
        {
            var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() =>
            {
                setup();
            }));
        }

        private void setup()
        {
            //Set the pin mode of the led.
            arduino.pinMode(led1, PinMode.OUTPUT);
            arduino.pinMode("A0", PinMode.ANALOG);
            textConnection.Text = "Connected";
            //Set the timer to schedule every 500 ms.
            readTemperatureTimer = new DispatcherTimer();
            readTemperatureTimer.Interval = TimeSpan.FromMilliseconds(1000);
            readTemperatureTimer.Tick += readTemperature;
            readTemperatureTimer.Start();
        }

        private void Onbutton_Click(object sender, RoutedEventArgs e)
        {
            arduino.digitalWrite(led1, PinState.HIGH);
            Onbutton.Visibility = Visibility.Collapsed;
            Offbutton.Visibility = Visibility.Visible;
        }

        private void Offbutton_Click(object sender, RoutedEventArgs e)
        {
            arduino.digitalWrite(led1, PinState.LOW);
            Offbutton.Visibility = Visibility.Collapsed;
            Onbutton.Visibility = Visibility.Visible;
        }

        private void readTemperature(object sender, object e)
        {
            //Read analog value from 0 to 1023, which maps from 0 to 5V
            int temperatureValue = arduino.analogRead("A0");

            //Convert analog value to temperature.
            double temperature = (500.0 * temperatureValue) / 1024.0;

            //Print temperature to Output.
            textTemp1.Text = temperature.ToString() ;
        }
    }
}

Go Up