Code help needed

Got my Netduino project up and running but something doesn’t seem right to me
anyway
in
Method_6() and 7

I have
while (anyinputfor6.Read() == false && method_state_6 == true)

or should this be

while (anyinputfor6.Read() == true && method_state_6 == true)

I have a pull up resister wired into the input
I want it to do 6 and 7 on a closed switch
true true seems to work but the opposite way
and false true doesn’t seem to do anything
could be a wiring problem too

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication1
{
    public class Program
    {
        public const int SecondMs = 1000;
        public const int MinuteMs = 60 * SecondMs;
        public const int HourMs = 60 * MinuteMs;
        public const int DayMs = 24 * HourMs;
        public const int WeekMs = 7 * DayMs;

        static bool method_state_6, method_state_7;


        static InputPort anyinputfor6 = new InputPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.Disabled);
        static InputPort anyinputfor7 = new InputPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.Disabled);
        static OutputPort ph = new OutputPort(Pins.GPIO_PIN_D2, true);
        static OutputPort b = new OutputPort(Pins.GPIO_PIN_D3, true);
        static OutputPort a = new OutputPort(Pins.GPIO_PIN_D4, true);
        static OutputPort drain = new OutputPort(Pins.GPIO_PIN_D5, true);
        static OutputPort solenoid = new OutputPort(Pins.GPIO_PIN_D6, true);
        static OutputPort heater = new OutputPort(Pins.GPIO_PIN_D7, true);
        static OutputPort phcontroller = new OutputPort(Pins.GPIO_PIN_D8, true);
        static OutputPort tdscontrollerA = new OutputPort(Pins.GPIO_PIN_D9, true);
        static OutputPort tdscontrollerB = new OutputPort(Pins.GPIO_PIN_D10, true);
        static OutputPort led = new OutputPort(Pins.GPIO_PIN_D11, true);


        
        public static void Main()
        {

            try
            {
                Thread led_proc = new Thread(new ThreadStart(turn_led));
                led_proc.Start();
                while (true)
                {

                    method_state_6 = method_state_7 = true;
                    
                       
                    Method_1();
                    call2_5();
                    call6_8();
                    Method_10();
                    Thread.Sleep(10000);

                }
            }
            catch
            {
                //Something has gone wrong; reset to a safe condition
            }
        }


        static void turn_led() //turn led for 16 hr and off for 8hr in loop
        {
            while (true)
            {
                led.Write(false);
                Thread.Sleep(HourMs * 12);
                led.Write(true);
                Thread.Sleep(HourMs * 12);
            }
        }

        public static void call2_5()
        {
            Thread t2 = new Thread(new ThreadStart(Method_2));
            t2.Start();
            Thread t3 = new Thread(new ThreadStart(Method_3));
            t3.Start();
            Thread t4 = new Thread(new ThreadStart(Method_4));
            t4.Start();
            Thread t5 = new Thread(new ThreadStart(Method_5));
            t5.Start();
            #region Wait for finishing 2-5 methods
            t2.Join();
            t3.Join();
            t4.Join();
            t5.Join();
            #endregion
        }
        static void call6_8()
        {
            Thread t6 = new Thread(new ThreadStart(Method_6));
            t6.Start();
            Thread t7 = new Thread(new ThreadStart(Method_7));
            t7.Start();
            Thread t9 = new Thread(new ThreadStart(Method_9));
            t9.Start();

            // wait for Method_9 finishing
            t9.Join();
            method_state_6 = method_state_7 = false;
        }
        public static void Method_1()
        {
            drain.Write(false);
            Thread.Sleep(16 * MinuteMs);
            drain.Write(true);
        }
        public static void Method_2()
        {
            solenoid.Write(false);
            Thread.Sleep(16 * MinuteMs);
            solenoid.Write(true);
        }
        public static void Method_3()
        {
            a.Write(false);
            Thread.Sleep(182 * SecondMs);
            a.Write(true);
        }
        public static void Method_4()
        {
            b.Write(false);
            Thread.Sleep(100 * SecondMs);
            b.Write(true);
        }
        public static void Method_5()
        {
            ph.Write(false);
            Thread.Sleep(5 * SecondMs);
            ph.Write(true);
        }
        public static void Method_6()
        {
            while (anyinputfor6.Read() == false && method_state_6 == true)
            {
                Thread.Sleep(5 * MinuteMs);
                phcontroller.Write(false);
                Thread.Sleep(3 * SecondMs);
                phcontroller.Write(true);
                
            }
        }
        public static void Method_7()
        {
            while (anyinputfor7.Read() == false && method_state_7 == true)
            {
                Thread.Sleep(5 * MinuteMs);
                tdscontrollerA.Write(false);
                Thread.Sleep(5454);
                tdscontrollerA.Write(true);
                tdscontrollerB.Write(false);
                Thread.Sleep(3000);
                tdscontrollerB.Write(true);
                
            }
        }
        public static void Method_9()
        {
            heater.Write(false);
            for (int i = 0; i < 14; i++)
            {
                Thread.Sleep(1 * DayMs);
                solenoid.Write(false);
                Thread.Sleep(20 * SecondMs);
                solenoid.Write(true);


            }
            heater.Write(true); // added this
        }
        public static void Method_10()
        {
            drain.Write(false);
            Thread.Sleep(16 * MinuteMs);
            drain.Write(true);
            solenoid.Write(false);
            Thread.Sleep(16 * MinuteMs);
            solenoid.Write(true);
            Thread.Sleep(1 * MinuteMs);
          
        }

    }
}

oh and its wired like this

This is not RLP code. Try the Netduino forum.

plus, is it just me or does anyone else not like using method_6 or call2_5 as method names? Certainly doesn’t help in trying to understand what those code blocks are meant to do…

it all started with the auto-generated “namespace NetduinoApplication1” :slight_smile:

@ perkunas - as ransomhall pointed out, the “Native Programming” forum is for RLP (C language) questions. However, you’re obviously new to this and you’re here already… So, let’s see if we can make sense of what you’re asking. Help me understand the problem by telling me a little more about what you’re trying to achieve. What is anyinputfor6 and anyinputfor7 connected to and what do you want to happen when they are in specific states? Since this feels like a program written around a truth table, perhaps you could show your table and the states you are trying to produce?

Perhaps an admin can move this to the “General” forum?

Sorry new here, guess I didn’t understand what a RLP (C language) was.
looking at the products here seems like they run C C# or VB.
Anyway I have limited programing skills I’m better with the electronics, even that could be better.
I love the fez and the Gadgeteer maybe net time ill use them instead.
Well actually 6&7 are connected to controllers (dosing controllers) they will dose when they reach the set point.(simple relay closes the switch)
The Idea was for then to go off on close, I needed them to complete the dose (or loop) before checking the input so that I don’t get incomplete doses.
Guess my question is a little more simple then that
I have
while (anyinputfor6.Read() == false && method_state_6 == true)

or should this be

while (anyinputfor6.Read() == true && method_state_6 == true)

when I want it to execute on closed switch seeing as though the way I have it wired.
Tnx

Sorry, but this still isn’t clear to me. I understand what you want to do but I don’t understand how your pins are attached. What is anyinputfor6? Is this what is reading the status of the switch/relay? If not, how do I tell the status of the switch? Your schematic shows a “P2” but I see no reference to that in your code. Have you tried these two scenarios? It should be easy enough to tell which one works through experimentation if there really are only two conditions you’re concerned with.

A closed switch will result in reading false. As the voltage on the input will be 0v.

There are 2 problems with your code that doesn’t help you in debugging, that’s:

  1. Choose better namings and use pascalcasing and camelcasing. One would think that call6_8 calls 6, 7 and 8, but no, it calls 6, 7 and 9?
  2. Get rid of those extremely long Sleeps, you are sleeping for hours and even a day? If you stop the debugger, you don’t even have a clue where your program is at. I would count multiples of seconds (Sleep(1000) in a loop) or use an RTC if you need better precisions.

" Your schematic shows a “P2” but I see no reference to that in your code."
No that’s just a generic schematic, the P2 is tied into the input pin
I have two sets off wiring, as I have 2 input pins and the relay is the switch.
Id love to write a schematic, as soon as I find a free one, that’s good enough to do it.
"Get rid of those extremely long Sleeps"
A real Time Clock with battery is the answer, my program is all about long sleeps.
But I don’t have one, and besides the longs sleeps don’t need to be accurate only the short ones.
"I would count multiples of seconds (Sleep(1000) in a loop)"
Not sure what you mean by that, as I’m a newb and had lots of help getting this far.
I’m going to do some testing today its my day off
Thanks

Ok…I rewired the inputs and still the same problems.
Do I need to turn on the on board pullup resistors too
here’s how its wired as of now
tnx

You already have a manual pull-up so no need to put MCU’s onboard pullups.

Can I confirm that you are using an actual “switch” where you have written “relay” ?

If you simplify your problem, to the point where you’re reading the switch values only and not in that mess of code, can you figure out where the problem lies? It seems to me that your issue is a LOGIC issue not a wiring issue, so changing the switch connectivity like you have seems that it was probably not necessary to move your scenario forward?

Ok. Starting to make a little more sense. You should also have a resistor between GPIO_PIN_D0/1 and your relay. Otherwise, you’ll always read high since that will still be the path of least resistance. If I understand this correctly, then your logic looks correct.

while (anyinputfor6.Read() == false && method_state_6 == true)

however, I would write it like this which means exactly the same thing but has slightly better performance and is a little more professional.

while (method_state_6 && !anyinputfor6.Read())

So, now that I understand your setup and what you want it to do. Now I need some more feedback on what it does not do at this point.

Check out EagleCAD or Kicad. EagleCAD has a free version that is sufficient for most hobby work. Kicad is open source and completely free.

[quote]“I would count multiples of seconds (Sleep(1000) in a loop)”
Not sure what you mean by that, as I’m a newb and had lots of help getting this far.[/quote]

WouterH means to do this…


var startTime = DateTime.Now();
while(true)
{
    Thread.Sleep(1000);
    if(DateTime.Now() >= [some end time pre-calculated]) break;
}
doSomething();