Unable to read a switch state after Arduino Reset

Since this is my first post to this forum, please be indulgent.

Objective
Control the opening and closing of my astronomical observatory roof (www.fjbastro.webs.com) via my computer (i.e. the applicaiton I am writing in vb.net).

Board
Arduino Uno

Devices being used
2 transistors/relays (i.e. digital pin 2 and 3) to control one motor and 2 limit switches (digital pin 4 and 5) to stop the motor when the roof is fully open or closed (see attached schematic).

Softwares
vb.net application (EasyObs.exe) using firmatavb.dll to firmata 2.2 loaded on Arduino (see attached vb.net code).

Issue
When I power on the arduino board and/or when I connect to it, the observatory roof is normally closed, therefore, one of the limit switch is closed and the associated digital pin (5) should read HIGH. However, at power on or after Arduino reset, the pin state allways show LOW. My vb.net code therefore allways report "WARNING: Unknown Status of Roof" (as per my code) because both pin 4 and 5 read LOW (even though, I have one switch closed).

The only way I found to make sure to have my code read the pin state as High, is to use "firmata_test.exe" to read all of the Arduino pins state and force digital pin 4 and 5 to output mode and then back to input mode. As soon as I switch these pins to input mode, they start reading high. I then, close "firmata_test.exe" and fire up my application where the pin states are ok. After that, my application will work ok until I shut down the power to the Arduino.

Does anyone have an idea of why this is happening and how I can resolve this?

Many thanks,

Francois

easyobs.txt (4.16 KB)

9 volts into the Arduino! Is this wise?

Hint: no.

Can you post your Arduino code? It's hard to debug without seeing it.

According to the Arduino site: http://arduino.cc/en/Main/ArduinoBoardUno

Input Voltage (recommended): 7-12V

Do you see a problem with 9V?

Public Class EasyObs
    Public Const HIGH As Integer = 1
    Public Const LOW As Integer = 0
    Public Const INPUT As Integer = 0
    Public Const OUTPUT As Integer = 1
    Public STOPPED As Boolean
    Public sw As Stopwatch
    Public RoofStatus As String

    Public Sub EasyObs_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Insert here automatic connection to Arduino once ready for ASCOM implementation
        LblRoofStatus.Text = "-"
        LblBoardStatus.Text = "Disconnected"
        cmdCloseRoof.Enabled = False
        cmdOpenRoof.Enabled = False
        CmdDisconnect.Enabled = False
    End Sub

    Public Sub CmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdConnect.Click
        'ComboComPort.Items.AddRange(FirmataVB1.COMPortList())
        'ComboComPort.Text = Firmata.FirmataVB.DEFAULT_COM_PORT
        FirmataVB1.Connect(ComboComPort.Text, ComboBaudRate.Text)
        CmdConnect.Enabled = False
        FirmataVB1.PinMode(2, OUTPUT)
        wait(100)
        FirmataVB1.PinMode(3, OUTPUT)
        wait(100)
        FirmataVB1.PinMode(4, INPUT)
        wait(100)
        FirmataVB1.PinMode(5, INPUT)
        wait(100)
        FirmataVB1.PinMode(4, OUTPUT)
        wait(100)
        FirmataVB1.PinMode(5, OUTPUT)
        wait(100)
        FirmataVB1.PinMode(4, INPUT)
        wait(100)
        FirmataVB1.PinMode(5, INPUT)
        wait(100)
        If FirmataVB1.DigitalRead(5) = HIGH Then
            LblRoofStatus.Text = "Roof Closed"
        Else
            If FirmataVB1.DigitalRead(4) = HIGH Then
                LblRoofStatus.Text = "Roof Opened"
            Else
                LblRoofStatus.Text = "WARNING: Unknown Status of Roof"
            End If
        End If
        LblBoardStatus.Text = "Connected"
        CmdDisconnect.Enabled = True
        cmdOpenRoof.Enabled = True
        cmdCloseRoof.Enabled = True
    End Sub

    Public Sub cmdOpenRoof_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpenRoof.Click
        STOPPED = False
        cmdCloseRoof.Enabled = False
        LblRoofStatus.Text = "OPENING"
        Do Until FirmataVB1.DigitalRead(4) = HIGH Or STOPPED = True
            Application.DoEvents()
            FirmataVB1.DigitalWrite(2, HIGH)
        Loop
        FirmataVB1.DigitalWrite(2, LOW)
        LblRoofStatus.Text = "Roof Opened"
        cmdCloseRoof.Enabled = True
    End Sub

    Public Sub cmdCloseRoof_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCloseRoof.Click
        STOPPED = False
        cmdOpenRoof.Enabled = False
        LblRoofStatus.Text = "CLOSING"
        Do Until FirmataVB1.DigitalRead(5) = HIGH Or STOPPED = True
            Application.DoEvents()
            FirmataVB1.DigitalWrite(3, HIGH)
        Loop
        FirmataVB1.DigitalWrite(3, LOW)
        LblRoofStatus.Text = "Roof Closed"
        cmdOpenRoof.Enabled = True
    End Sub

    Public Sub CmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdStop.Click
        STOPPED = True
        FirmataVB1.DigitalWrite(2, LOW)
        FirmataVB1.DigitalWrite(3, LOW)
        cmdCloseRoof.Enabled = True
        cmdOpenRoof.Enabled = True
    End Sub

    Public Sub CmdDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdDisconnect.Click
        LblBoardStatus.Text = "Disconnected"
        LblRoofStatus.Text = "-"
        If FirmataVB1.PortOpen = True Then
            FirmataVB1.Disconnect()
            CmdConnect.Enabled = True
            cmdCloseRoof.Enabled = False
            cmdOpenRoof.Enabled = False
            CmdDisconnect.Enabled = False
        Else
            LblBoardStatus.Text = "Already Disconnected"
        End If
    End Sub

    ' Loops for a specificied period of time (milliseconds)
    Public Sub wait(ByVal interval As Integer)
        Dim sw As New Stopwatch
        sw.Start()
        Do While sw.ElapsedMilliseconds < interval
            ' Allows UI to remain responsive
            Application.DoEvents()
        Loop
        sw.Stop()
    End Sub
End Class

Do you see a problem with 9V?

Yes I do. That is into the power socket. Which goes through a voltage regulator.

Not into a digital pin. You'll blow it up.

I'm not familiar with VB on the Arduino personally. Perhaps someone else can help with this part.

My bad, I agree with the 9v in the pin statement. My original schematic had 9v in the pin...after blowing a board, I had to purchase a new one and connected the switches using the 5volts pin.

Sorry about the wrong schematic.

Just to be crystal clear, my switches and pins are indeed connected to 5v.

I still have the issue about the pins not reporting HIGH at power on or reset.

Any help will be appreciated.

Thanks,

Francois

Frankjb:
Just to be crystal clear, my switches and pins are indeed connected to 5v.

I still have the issue about the pins not reporting HIGH at power on or reset.

Any help will be appreciated.

Thanks,

Francois

So you are wiring +5vdc to the switches, that means you are using external pull-down resistors at each digital input pin?

Lefty

His circuit appears to show that, although I'm not sure that DC 9V Gnd means. Hopefully this is the same as the Arduino Gnd. Otherwise, trouble brewing.

For more help with the programming side, can you clarify how you are running VB on the Arduino?

Correct. I am using Pull-down resistors (10k) on each pins (i.e. 4 an 5).

Why the delays? And why do it twice?

        FirmataVB1.PinMode(4, INPUT)
        wait(100)
        FirmataVB1.PinMode(5, INPUT)
        wait(100)
        FirmataVB1.PinMode(4, OUTPUT)
        wait(100)
        FirmataVB1.PinMode(5, OUTPUT)
        wait(100)
        FirmataVB1.PinMode(4, INPUT)
        wait(100)
        FirmataVB1.PinMode(5, INPUT)
        wait(100)

And why make them output and then input again? Even though I don't understand how you are running VB here, it looks a bit indecisive what you are doing there.

DC 9V Gnd = Arduino Ground.

I am using the following library Firmatavb.dll as part of my visual studio solution.

I have uploaded "StandardFirmata_2_2_forUNO_0_3.pde" on the board for it to understand the firmatavb.dll calls.

Hope this anwser the question.

I have inserted delays between the pinmode settings otherwise they don't go through...seems to be a timing problem.

I switch from input to output back to input in my code because I wanted to replicate what I was doing manually (i.e. using firmata_test.exe). However, it does not work.

I am suspicious of code that needs delays "to make it work".

Is this IDE 1.0 of the Arduino? They changed the way serial output works.

Try running it under version 0022.

Nick, I have used IDE 0023 to upload the StandardFirmata_2_2_forUNO_0_3.pde on the board.

But I am not using the Arduino IDE to program it. I am using Visual Studio to build my own application. That application is using a library Firmatavb.dll to talk to the above pde on the board.

I don't like the delays either. But it's the only way that I was able to make this work. Furthermore, the point is not about these delays...go back to my description of the issue:

The Arduino board is reading LOW on pin 5 at power on when the switch is closed (therefore when current flows from 5v to pin 5).

Thanks,

François

Frankjb:
Furthermore, the point is not about these delays...go back to my description of the issue:

Oh, sorry. But if you need delays to make pinMode work:

I have inserted delays between the pinmode settings otherwise they don't go through ...

What makes you think you don't need delays to make digitalRead work?

I'm sorry, but if you have to randomly insert delays to make code work, and your code doesn't work in places where you don't have a delay, I don't think you can say "the point is not about these delays".

Frankjb:
The Arduino board is reading LOW on pin 5 at power on when the switch is closed ...

Let's be a little more precise. The Arduino may or may not be reading a LOW. But you aren't directly looking at that. You have code running on the Arduino, which sends data via a serial port, to your program running on your PC, via a DLL, which works it way up through a Visual Basic program, which eventually (at the end of a rather long food chain, if I may say) apparently does not report things correctly.

Try running this sketch on the Arduino.

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

void loop () 
{
  digitalWrite (13, digitalRead (5));
}

Then close and open the switch on pin 5 and see if the LED on pin 13 goes on and off. That will confirm the hardware part. After that, well it's somewhere in Firmata.

Even when I don't use my code...i have the issue.

The issue exist even when I use firmata_test.exe to read the pin state. See the screenshot of that app at:

At power on, all pins are reading LOW (even though I have current flowing from 5v to pin5). The only way I can have it read HIGH, is by using this interface to switch the pinmode to output and then back to input. Then it reads HIGH.