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

simple code need help


  • Please log in to reply
44 replies to this topic

#1 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 29 March 2012 - 04:44 AM

This is my 1st attempt at writing in C# 1) question how do I define hours minutes weeks ect. in C++ I would use macros some thing like this #define seconds_in_ms(s) ((s)*1000UL) #define minutes_in_ms(m) ((m)*60UL*1000UL) #define hours_in_ms(h) ((h)*60UL*60UL*1000UL) #define days_in_ms(d) ((d)*24UL*60UL*60UL*1000UL) #define weeks_in_ms(w) ((w)*7UL*24UL*60UL*60UL*1000UL) example // Thread.Sleep(16); I want that 16min. or even days or weeks 2) question multi-threading I need to run 4 pumps same time, wait till they are all finished before continuing on (variable times)not sure the best way to tackle that one. any examples

#2 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 29 March 2012 - 08:49 AM

...in C++ I would use macros...

To my knowledge macros are not available in C#. I don't think there is a preprocessor in the same way as in C/C++.

...wait till they are all finished before continuing on (variable times)not sure the best way to tackle that one.

You should be able to fork up the threads and then issue Thread.Join(...) on each of them from your main routine to make sure they're all finished before proceeding. I even think you can pass a timeout parameter to limit the wait.

#3 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 29 March 2012 - 11:41 AM

1) question how do I define hours minutes weeks ect.
in C++ I would use macros some thing like this

Hanzibal is right, there is no direct equivalent of C/C++ preprocessor macros in C#. Probably the closest solution would be something like this:

const int SecondMs = 1000;
const int MinuteMs = 60*SecondsMs;
const int HourMs = 60*MinutesMs;
const int DayMs = 24*HourMs;
const int WeekMs = 7*DayMs;

Thread.Sleep(16*MinuteMs);
Thread.Sleep(2*DayMs + 6*HourMs);
Compiler will replace the constant expression with the resulting value, which is the same as for C/C++ macro. Alternatively, you could also create methods that mimic the macros (i.e. take a parameter and multiply it with 'milliseconds' constant) or use TimeSpan structure (see its constructor, Ticks property and TicksPerXXX fields).

Please note that type of the millisecondsTimeout parameter in Thread.Sleep() is only int (vs. unsigned long in the macros), so it is limited to less than 25 days.

#4 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 29 March 2012 - 12:49 PM

Wow so much useful info. tnx ok so where do I put those const int so my program can use them // const int SecondMs = 1000; const int MinuteMs = 60*SecondsMs; const int HourMs = 60*MinutesMs; const int DayMs = 24*HourMs; const int WeekMs = 7*DayMs; // using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace controller { public class Program { public static void Main() { OutputPort ph = new OutputPort(Pins.GPIO_PIN_D2, true); OutputPort b = new OutputPort(Pins.GPIO_PIN_D3, true); OutputPort a = new OutputPort(Pins.GPIO_PIN_D4, true); OutputPort drain = new OutputPort(Pins.GPIO_PIN_D5, true); OutputPort solenoid = new OutputPort(Pins.GPIO_PIN_D6, true); OutputPort heater = new OutputPort(Pins.GPIO_PIN_D7, true); OutputPort controller = new OutputPort(Pins.GPIO_PIN_D8, true); drain.Write(false); Thread.Sleep(16 * MinuteMs); drain.Write(true); } } }

#5 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 29 March 2012 - 01:11 PM

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

    public static void Main()
    {
   	// ...
    }
  }

EDIT: There are extra 's' on the second and third line, 60*SecondsMs should be SecondMs and similarly, 60*MinutesMs should be MinuteMs. Sorry.

#6 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 29 March 2012 - 02:39 PM

I tried that, and that's why I was asking the question, because it didn't work, got 4 errors. using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace controller { public class Program { const int SecondMs = 1000; const int MinuteMs = 60 * SecondsMs; const int HourMs = 60 * MinutesMs; const int DayMs = 24 * HourMs; const int WeekMs = 7 * DayMs; public static void Main() { OutputPort ph = new OutputPort(Pins.GPIO_PIN_D2, true); OutputPort b = new OutputPort(Pins.GPIO_PIN_D3, true); OutputPort a = new OutputPort(Pins.GPIO_PIN_D4, true); OutputPort drain = new OutputPort(Pins.GPIO_PIN_D5, true); OutputPort solenoid = new OutputPort(Pins.GPIO_PIN_D6, true); OutputPort heater = new OutputPort(Pins.GPIO_PIN_D7, true); OutputPort controller = new OutputPort(Pins.GPIO_PIN_D8, true); drain.Write(false); Thread.Sleep(16 * MinuteMs); drain.Write(true); } } }

#7 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 29 March 2012 - 03:00 PM

Oh, there are two typos in the multiplication, sorry. The correct code is

...
    const int SecondMs = 1000;
    const int MinuteMs = 60 * SecondMs; // <- extra 's' was here
    const int HourMs = 60 * MinuteMs;   // <- extra 's' was here too
    const int DayMs = 24 * HourMs;
    const int WeekMs = 7 * DayMs;
Also, if you want to use the constant from outside the Program class, you'd need to declare them as 'public const ...' (or internal).

#8 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 29 March 2012 - 03:17 PM

thanks so much is this right, it compiles and all and I named it public class constant although that doesn't seem right It should be public constant I think? using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace controller { public class constant { const int SecondMs = 1000; const int MinuteMs = 60 * SecondMs; const int HourMs = 60 * MinuteMs; const int DayMs = 24 * HourMs; const int WeekMs = 7 * DayMs; public static void Main() { OutputPort ph = new OutputPort(Pins.GPIO_PIN_D2, true); OutputPort b = new OutputPort(Pins.GPIO_PIN_D3, true); OutputPort a = new OutputPort(Pins.GPIO_PIN_D4, true); OutputPort drain = new OutputPort(Pins.GPIO_PIN_D5, true); OutputPort solenoid = new OutputPort(Pins.GPIO_PIN_D6, true); OutputPort heater = new OutputPort(Pins.GPIO_PIN_D7, true); OutputPort controller = new OutputPort(Pins.GPIO_PIN_D8, true); drain.Write(false); Thread.Sleep(16 * MinuteMs); drain.Write(true); } } }

#9 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 29 March 2012 - 04:18 PM

is this right, it compiles and all and I named it public class constant although that doesn't seem right

There is no need to rename the Program class, you can just leave it as it was (in your previous post). I was referring to making the constants public, such as public const int SecondMs = 1000;, sorry for the confusion. C# has similar access modifiers like C++, class members are 'private' by default (when no modifier is specified). Unlike C++, there is no notation of global variables or constants in C#, they must belong to a class (or they can be declared as enum etc.).

#10 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 29 March 2012 - 04:45 PM

Right I get it, just leave it the way it was. I'm using optical relays on is off, and off is on, from what I understand they fire on on start up which is fine by me I want them on. also the 21 day stack overflow not a problem, the most a given task running is a day or so. my next part going to be tricky trying to fire 4 on off at the same time and wait till they are dome b4 moving on going to try the Thread.Join(...) but not today I'm done 4 now, going to the bar for a beer, Id buy you one 2 if you were here. once again tnx Peter

#11 h3mp

h3mp

    Member

  • Members
  • PipPip
  • 21 posts

Posted 29 March 2012 - 08:24 PM

..I was just wondering why not simply use the Thread.Sleep("TimeSpan") overloaded method, but then noticed it's missing in .net MF grr! :-)

#12 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 30 March 2012 - 06:19 AM

..I was just wondering why not simply use the Thread.Sleep("TimeSpan") overloaded method, but then noticed it's missing in .net MF grr! :-)

I guess you can use TimeSpan like this

  // 1 day and 30 minutes
  Thread.Sleep((int)((new TimeSpan(1, 0, 30, 0)).Ticks/TimeSpan.TicksPerMillisecond));
It would be a little bit simpler if it had TotalMilliseconds property, like the 'full framework' version has.

#13 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 31 March 2012 - 02:28 PM

ok here's where I'm completely lost in space. I need all of these things to happen at the same time, wait till they are all done then continue. Not even sure I can do it in one thread please help. public static void Thread1() { solenoid.Write(false); Thread.Sleep(16 * MinuteMs); solenoid.Write(true); a.Write(false); Thread.Sleep(3 * SecondMs); a.Write(true); b.Write(false); Thread.Sleep(6 * MinuteMs + 3 * SecondMs ); b.Write(true); ph.Write(false); Thread.Sleep(11 * MinuteMs + 7 * SecondMs ); ph.Write(true); Thread tid1 = new Thread(new ThreadStart(MyThread.Thread1 ) ); tid1.Start(); }

#14 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 31 March 2012 - 02:41 PM

The way your code is written, you'll have a single thread respawning it self indefinitely. If that's what you want, you could use a simple while(true) construct.

I need all of these things to happen at the same time, wait till they are all done then continue.

I guess you mean to do something like below, that way you will have 4 threads running simultaneously (almost) and then wait until they're all finished (16 minutes).

public class Program
{
    public static void Main()
    {
        Thread tid_1 = new Thread(new ThreadStart(Thread_1));
        Thread tid_2 = new Thread(new ThreadStart(Thread_2));
        Thread tid_3 = new Thread(new ThreadStart(Thread_3));
        Thread tid_4 = new Thread(new ThreadStart(Thread_4));

        tid_1.Start();
        tid_2.Start();
        tid_3.Start();
        tid_4.Start();

        tid_1.Join();
        tid_2.Join();
        tid_3.Join();
        tid_4.Join();
    }

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

    public static void Thread_2()
    {
   	a.Write(false);
        Thread.Sleep(3 * SecondMs);
        a.Write(true);
    }

    public static void Thread_3()
    {
        b.Write(false);
        Thread.Sleep(6 * MinuteMs + 3 * SecondMs);
        b.Write(true);
    }

    public static void Thread_4()
    {
        ph.Write(false);
        Thread.Sleep(11 * MinuteMs + 7 * SecondMs);
        ph.Write(true);
    }
}


#15 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 31 March 2012 - 03:00 PM

oh wow, I would never figured that out on my own, the examples out there have little to do with what I'm trying to do your the best need to do a few more tasks til done, nothing complicated and put it all together. Oh ya those pesky variables, love to set up some nuds graphically to change em. Visual basic I could do that, but I assume C# can do the same.? one again thanks

#16 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 31 March 2012 - 05:27 PM

No problem, came to think of much simpler solution below. However, changing intervals would require more work.
public class Program
{
    public static void Main()
    {
        solenoid.Write(false);
        a.Write(false);
        b.Write(false);
        ph.Write(false);

        Thread.Sleep(3 * SecondMs);
        // 3 seconds in total
        a.Write(true);

        Thread.Sleep(6 * MinuteMs);
        // 6 minutes and 3 seconds in total
        b.Write(true);

        Thread.Sleep(5 * MinuteMs + 4 * SecondMs);
        // 11 minutes and 7 seconds in total
        ph.Write(true);

        Thread.Sleep(4 * MinuteMs + 53 * SecondMs);
        // 16 minutes in total
        solenoid.Write(true);
    }
}
Or maybe this was what you had in the first place?

Setting up a graphical interface would require so much more work - don't forget, your on a microcontroller now, not a PC. So you'll need an LCD with touch sensor (or physical knobs) and then write a graphical library to go with it all.

#17 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 31 March 2012 - 08:28 PM

Thats great but.. I need to run this 1st, then the thread join thing drain.Write(false); Thread.Sleep(16 * MinuteMs); drain.Write(true); Also my thoughts, were to build a simple text editor using visual basic nuds ect. output it as a file, that can be uploaded. I did one similar using an arduino but this is the 1st time on C# and netduino might not be the same or even work. The values would be variable so I think I need to have it in a thread join. like the 1st example at least i think so.

#18 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 31 March 2012 - 09:39 PM

tried this, no go, yet so close lol using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace controller { 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; public static void Main() { OutputPort ph = new OutputPort(Pins.GPIO_PIN_D2, true); OutputPort b = new OutputPort(Pins.GPIO_PIN_D3, true); OutputPort a = new OutputPort(Pins.GPIO_PIN_D4, true); OutputPort drain = new OutputPort(Pins.GPIO_PIN_D5, true); OutputPort solenoid = new OutputPort(Pins.GPIO_PIN_D6, true); OutputPort heater = new OutputPort(Pins.GPIO_PIN_D7, true); OutputPort controller = new OutputPort(Pins.GPIO_PIN_D8, true); drain.Write(false); Thread.Sleep(16 * MinuteMs); drain.Write(true); Thread tid_1 = new Thread(new ThreadStart(Thread_1)); Thread tid_2 = new Thread(new ThreadStart(Thread_2)); Thread tid_3 = new Thread(new ThreadStart(Thread_3)); Thread tid_4 = new Thread(new ThreadStart(Thread_4)); tid_1.Start(); tid_2.Start(); tid_3.Start(); tid_4.Start(); tid_1.Join(); tid_2.Join(); tid_3.Join(); tid_4.Join(); } public static void Thread_1() { solenoid.Write(false); Thread.Sleep(16 * MinuteMs); solenoid.Write(true); } public static void Thread_2() { a.Write(false); Thread.Sleep(3 * SecondMs); a.Write(true); } public static void Thread_3() { b.Write(false); Thread.Sleep(6 * MinuteMs + 3 * SecondMs); b.Write(true); } public static void Thread_4() { ph.Write(false); Thread.Sleep(11 * MinuteMs + 7 * SecondMs); ph.Write(true); } } }

#19 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 31 March 2012 - 11:12 PM

Ok, so now the threads will start when drain is completed after 16 minutes - it's that correct or how should it work?

#20 perkunas

perkunas

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts

Posted 31 March 2012 - 11:23 PM

right drain runs 16min then the treads start this wont compile, get error cant find this solenoid or any of the other ones.




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.