Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

I give up here


  • Please log in to reply
8 replies to this topic

#1 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 04 December 2012 - 02:19 PM

Ive been going round and round, with the same problem almost a year now, Ive tried everything being new that doesn't help either. 1st off got some bad code, that through me for a loop for the longest time.
Anyway the program is simple, check the input (its a relay contact) when it calls, do X wait 5 min and loop.
The problem is it wont work, I get it doing crazy things, running in reverse firing out of turn ect.
Whats even more frustrating is that If i write the program long hand it works fine with an occasional out of turn. I suspect it could be the controllers fault, occasional faulty signal (need to write it in a way to check it multiple times) short hand the thing goes nuts. Yet if you do a simple test program a basic switch it works fine so how can it be hardware?. I was told I probably need an offamp that makes no sense, its either on or off its not that complicated. Maybe the analog pins are more stable IDK

this runs nuts

public static void Method_6()
          
            {
            
            while((method_state_6) && (!phcontroller.Read())) //phcontroller is just a relay that closes when needed
   {
                        ph.Write(false);
                        Thread.Sleep(2000);
                        ph.Write(true);
                        Thread.Sleep(5 * MinuteMs);
   }
}

this is almost fine
public static void Method_6()
        {
            bool phState = true;
            while (method_state_6 == true)
            {
                    phState = phcontroller.Read();
                    if (phState == false)
                    {
                        ph.Write(false);
                        Thread.Sleep(2000);
                        ph.Write(true);
                        Thread.Sleep(5 * MinuteMs);
                    }
                    else
                        Thread.Sleep(1000);

                }
            }


#2 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 04 December 2012 - 03:43 PM

You need to provide more information in order to receive the proper type of help you need. For example, wiring schematic and more code as to how phController is defined. There are numerous reasons this could fail least likely is the Controller as you have mentioned.

#3 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 04 December 2012 - 04:38 PM

honest your code looks like it just cant work. maybe you join the chat and i help you a little when you are there?

while (method_state_6 == true)
            {
                    phState = phcontroller.Read();
                    if (phState == false)
                    {
                        ph.Write(false);
                        Thread.Sleep(2000);
                        ph.Write(true);
                        Thread.Sleep(5 * MinuteMs);
                    }
                    else
                        Thread.Sleep(1000);

                }

like you loop that forever, it will just hang there till method_state_6 changes (wich i dont think it ever does)
or not get executed cos its fals when you call it. ive seen your bigger codeblock and i havent found a thread there that changes it.

#4 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 04 December 2012 - 05:11 PM

Method_6 and 7 are both controllers and have the same problem.
They turn on and off later in the program
Here's the code and wiring its a bit long.



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

namespace Hydro
{
    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 phcontroller = new InputPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.Disabled);
        static InputPort nutcontroller = 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 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_1();
                    call2_5();
                    call6_8();
                    Method_9();
                    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()
        {
            method_state_6 = method_state_7 = true;
            Thread t6 = new Thread(new ThreadStart(Method_6));
            t6.Start();
            Thread t7 = new Thread(new ThreadStart(Method_7));
            t7.Start();
            Thread t8 = new Thread(new ThreadStart(Method_8));
            t8.Start();

            // wait for Method_8 finishing
            t8.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()
          
            {
                Thread.Sleep(2 * HourMs);
            while((method_state_6) && (!phcontroller.Read())) //phcontroller is just a relay that closes when needed
   {
                        ph.Write(false);
                        Thread.Sleep(2000);
                        ph.Write(true);
                        Thread.Sleep(5 * MinuteMs);
   }
}
        
        public static void Method_7()
              {
                  Thread.Sleep(2 * HourMs);
              while((method_state_7) && (!nutcontroller.Read()))  //nutcontroller is just a relay that closes when needed
   {
                        a.Write(false);
                        Thread.Sleep(5454);
                        a.Write(true);
                        b.Write(false);
                        Thread.Sleep(3000);
                        b.Write(true);
                        Thread.Sleep(5 * MinuteMs);                  
   }
}
        
                     
        public static void Method_8()
        {
            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); 
        }

        public static void Method_9()
        {
            drain.Write(false);
            Thread.Sleep(16 * MinuteMs);
            drain.Write(true);
            solenoid.Write(false);
            Thread.Sleep(16 * MinuteMs);
            solenoid.Write(true);
            Thread.Sleep(1 * MinuteMs);
          
        }

    }
}

Attached Files



#5 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 04 December 2012 - 07:15 PM

Honestly I think that with while((method_state_6) && (!phcontroller.Read())) it polls all the time, I think I get faulty reading from the controller, every now and then its just the way it is you get fluctuations which sets this off, written the other way works better as its reading intermittently. In my thinking i need to write this so it checks the controller like 4 times over a couple minutes before doing anything. I suppose I could use some help with that too, I don't know if that's the answer but its all I got. Thanx

#6 Lunddahl

Lunddahl

    Advanced Member

  • Members
  • PipPipPip
  • 152 posts
  • LocationEurope, Denmark

Posted 04 December 2012 - 11:15 PM

One of your problems is that mechanical switches bounce, and you are using a micro controller that is so fast that is reads those bounces as a lot of state changes. see: http://en.wikipedia....#Contact_bounce You will have to change your program so it only accepts so many changes over so much time, some large relays can take more than 10ms to settle, and that's en eternity for a microprocessor. I would look into using interrupt ports and time stored in variables instead of thread.Sleep() that lasts 16 hours. You can see an example on using interupt ports to debounce a contact here: http://forums.netdui...tedgelevelhigh/ - Ulrik Lunddahl

#7 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 05 December 2012 - 12:14 AM

Relay bounce I buy that, not sure how interrupts fix them.I need to look into it some more
I was thinking in my head something like this

int i = 0;
bool flag = true;
while (i < 4 && flag)
{
flag = inputSource;
}
 
if (flag)
do X

Have it check the input like 4 times instead of just once over a period time.It only fires if it gets 4 in a row, so The occasional faulty read wont happen.(i do get them I don't know how many or often) Need to put this together some more.
Sounds like you didn't get your interrupts working either or did you IDk
Beer thing sounds cool.

#8 Lunddahl

Lunddahl

    Advanced Member

  • Members
  • PipPipPip
  • 152 posts
  • LocationEurope, Denmark

Posted 05 December 2012 - 08:10 PM

Relay bounce I buy that, not sure how interrupts fix them.I need to look into it some more
I was thinking in my head something like this

int i = 0;
bool flag = true;
while (i < 4 && flag)
{
flag = inputSource;
}
 
if (flag)
do X

Have it check the input like 4 times instead of just once over a period time.It only fires if it gets 4 in a row, so The occasional faulty read wont happen.(i do get them I don't know how many or often) Need to put this together some more.
Sounds like you didn't get your interrupts working either or did you IDk
Beer thing sounds cool.


The relay is bouncing, and that's a behavior you can't use, and yet you have decided to count how many time is bounces, why ?

The interrupt way is working exactly as expected, and because i only want exactly one event in my program, i have chosen to ignore all interupts for x milliseconds after the first one is received. To me that is the most logical way to handle bounces.

There is a special hardware function that can help me with that, but the netduino firmware have not implemented it yet, so my code can not be simplified more than it already is, before that is done.

- Ulrik Lunddahl

#9 Dave M.

Dave M.

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts

Posted 06 December 2012 - 12:46 AM

I agree with Lunddahl, but as a simpler test first, you can use
System.Threading.Thread.Sleep( 20)
to wait 20ms after you detect contact. After the 20ms elapsed, check again. If it's still making contact, then proceed. Otherwise, bail out and start over, or poll the next relay.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.