Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [Resolved] Netduino Help With Coding (Ultra-beginner)
New Posts  All Forums:Forum Nav:

[Resolved] Netduino Help With Coding (Ultra-beginner)

post #1 of 10
Thread Starter 
Hi all,

I recently purchased a Netduino plus with the intention of getting it to be a steering and navigation controller for an aerospace project of mine. I'm currently just trying to get a handle on basic coding.

However my knowledge of C# coding is very very basic (as well as coding in general). In fact I cannot figure out why I cannot get this very basic piece of coding to work correctly. My intention with this code is to have a PWM LED change from a 0 dutycyclestate to a 75 PWM cyclestate when the button is pressed and then return to 0 when released. When I debug with this the netduino simply PWMs at 75 and ignores the button. I've gone though at least 20 iterations of this code and it's obviously not working here can I get help from someone?

I started out with a much different set of code and am now trying a if, else. Sorry it's not commented, but I think it's basic enough someone even mildly competent can see what I'm doing.

Code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace NetduinoPlusApplication1
{
    public class Program
    {
        public static void Main()
        {
            // write your code here
            PWM LED = new PWM(Pins.GPIO_PIN_D10);
            InputPort powersw = new InputPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled);
            int on = 100;
            int off = 0;
            bool buttonstate = powersw.Read();
            if (buttonstate == true)
            {
                LED.SetDutyCycle((uint)on);

            }

            else

            {
                LED.SetDutyCycle((uint)off);
            }

        }
                                                                                            
        }

        

    }
Raptor Raper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Something with transistors FOXCONN Boodrage -> eVGA classified e760 GTX 480 / HD6850@1090/1200 3x2 gig G Skill Trident DDR2000 
Hard DriveOptical DriveOSMonitor
2x128GB Crucial C300 Sumsung SATA DVD-RW 64 bit Windows 7 Samsung 245BW 
KeyboardPowerCaseMouse
Microsoft curve Tagan ITZ 800W DD Torture Rack Diamondback 
  hide details  
Reply
Raptor Raper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Something with transistors FOXCONN Boodrage -> eVGA classified e760 GTX 480 / HD6850@1090/1200 3x2 gig G Skill Trident DDR2000 
Hard DriveOptical DriveOSMonitor
2x128GB Crucial C300 Sumsung SATA DVD-RW 64 bit Windows 7 Samsung 245BW 
KeyboardPowerCaseMouse
Microsoft curve Tagan ITZ 800W DD Torture Rack Diamondback 
  hide details  
Reply
post #2 of 10
You need two Boolean not just one. One Boolean to check whether the button was pressed or not, and another Boolean to check whether the button was released or not.
Code:
bool ButtonPressed = false, ButtonRelesed = false;

If(ButtonPressed == true)
{
LED=75;
ButtonRelesed = true;
}

if(ButtonRelesed== true)
{
LED=0;
}
    
CPUMotherboardGraphicsRAM
Intel 3930K 5040MHz @ 1.48 - 1.53v @ VTT/VCCSA ... Asus rampage extreme IV Gigabyte GTX 970 G1 @ 1580/4050MHz @ 1.325V Corsair Vengeance 64GB (8 x 8GB) DDR3 2400MHz. ... 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 Pro 256GB Seagate Barracuda ST3000DM001 3TB WD Black 1TB 64Mb Cache  WD Blue 500GB 16mb Cache 
Hard DriveOptical DriveCoolingCooling
WD Black 4TB 64mb Cache LG DVD EK-FB KIT RE4 - Acetal EK-FC970 GTX WF3 Backplate - Black 
CoolingCoolingCoolingCooling
Laing D5 Vario 12V DC Pump (MCP 655)  EK-BAY SPIN Reservoir - Plexi EK-CoolStream RAD XT (240) EK-CoolStream RAD XTX (120) 
CoolingCoolingCoolingCooling
EK-CoolStream XE 120 (Single) 8x Corsair Air SP120 PWM High Performance Edition 3x Corsair Air AF120 High Performance Edition EK-Supremacy EVO CPU Water Block (Nickel Acetal) 
CoolingOSMonitorMonitor
EK-FC970 GTX WF3 - Acetal+Nickel Windows 10 64-bit v1511 build 10586.14 BenQ RL2755HM LG W2261 
MonitorKeyboardPowerCase
Samsung S23C350 Razer BlackWidow Ultimate Cooler Master Silent Pro Gold 1000W Thermaltake Level 10 GT 
Mouse
Razer Lachesis 5600DPI 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel 3930K 5040MHz @ 1.48 - 1.53v @ VTT/VCCSA ... Asus rampage extreme IV Gigabyte GTX 970 G1 @ 1580/4050MHz @ 1.325V Corsair Vengeance 64GB (8 x 8GB) DDR3 2400MHz. ... 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 Pro 256GB Seagate Barracuda ST3000DM001 3TB WD Black 1TB 64Mb Cache  WD Blue 500GB 16mb Cache 
Hard DriveOptical DriveCoolingCooling
WD Black 4TB 64mb Cache LG DVD EK-FB KIT RE4 - Acetal EK-FC970 GTX WF3 Backplate - Black 
CoolingCoolingCoolingCooling
Laing D5 Vario 12V DC Pump (MCP 655)  EK-BAY SPIN Reservoir - Plexi EK-CoolStream RAD XT (240) EK-CoolStream RAD XTX (120) 
CoolingCoolingCoolingCooling
EK-CoolStream XE 120 (Single) 8x Corsair Air SP120 PWM High Performance Edition 3x Corsair Air AF120 High Performance Edition EK-Supremacy EVO CPU Water Block (Nickel Acetal) 
CoolingOSMonitorMonitor
EK-FC970 GTX WF3 - Acetal+Nickel Windows 10 64-bit v1511 build 10586.14 BenQ RL2755HM LG W2261 
MonitorKeyboardPowerCase
Samsung S23C350 Razer BlackWidow Ultimate Cooler Master Silent Pro Gold 1000W Thermaltake Level 10 GT 
Mouse
Razer Lachesis 5600DPI 
  hide details  
Reply
post #3 of 10
Thread Starter 
Thanks for the reply. That finally cleared up to me why I was getting 'unreachable code' warnings in earlier builds. I did not understand that you had to define both boolean variables right there. I was trying to do it twice once for true, its conditions/output, then then again for false and its conditions/output. However I realized that I never gave my inputport a name. So I gave it 'button'. However this code doesn't work either.
Code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace NetduinoPlusApplication1
{
    public class Program
    {
        public static void Main()
        {
            // write your code here
            PWM LED = new PWM(Pins.GPIO_PIN_D10);
            InputPort button = new InputPort(Pins.ONBOARD_SW1,  false, Port.ResistorMode.Disabled);

            bool buttonstateon = false, buttonstateoff = false;  //defining boolean args
            if (buttonstateon == true)                                               //conditions of buttonbeing pressed
            {

                buttonstateon = button.Read();                                 //telling it to read the button <--I suspect this is the issue. I don't know what to do
                LED.SetDutyCycle(75);                                               //telling it to set the PWM to 75

            }

            if (buttonstateoff == true)                                                //conditions of button released                  

            {   
                
                buttonstateoff = button.Read();                                 //telling it to read the button, same as above
                LED.SetDutyCycle(25);                                               //telling it to set PWM to 25
                
            }

        }
                                                                                            
      }

        

    }

Edited by Ihatethedukes - 11/25/12 at 11:23am
Raptor Raper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Something with transistors FOXCONN Boodrage -> eVGA classified e760 GTX 480 / HD6850@1090/1200 3x2 gig G Skill Trident DDR2000 
Hard DriveOptical DriveOSMonitor
2x128GB Crucial C300 Sumsung SATA DVD-RW 64 bit Windows 7 Samsung 245BW 
KeyboardPowerCaseMouse
Microsoft curve Tagan ITZ 800W DD Torture Rack Diamondback 
  hide details  
Reply
Raptor Raper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Something with transistors FOXCONN Boodrage -> eVGA classified e760 GTX 480 / HD6850@1090/1200 3x2 gig G Skill Trident DDR2000 
Hard DriveOptical DriveOSMonitor
2x128GB Crucial C300 Sumsung SATA DVD-RW 64 bit Windows 7 Samsung 245BW 
KeyboardPowerCaseMouse
Microsoft curve Tagan ITZ 800W DD Torture Rack Diamondback 
  hide details  
Reply
post #4 of 10
ok you are doing something wrong in here. these two booleans will never be true. because they are in an if statement and that if statement will never equal true. not sure if that make seans but I think this should work.
Code:
bool buttonstateon = false, buttonstateoff = false; 

buttonstateon = button.Read(); // this should set the variable to true or false. right?
buttonstateoff = button.Read();

if (buttonstateon == true)                                              
{     
       LED.SetDutyCycle(75);                                      
}

if (buttonstateoff == true)                                                 

{   
        LED.SetDutyCycle(25);          
}

Edited by Fantasy - 11/25/12 at 11:48am
    
CPUMotherboardGraphicsRAM
Intel 3930K 5040MHz @ 1.48 - 1.53v @ VTT/VCCSA ... Asus rampage extreme IV Gigabyte GTX 970 G1 @ 1580/4050MHz @ 1.325V Corsair Vengeance 64GB (8 x 8GB) DDR3 2400MHz. ... 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 Pro 256GB Seagate Barracuda ST3000DM001 3TB WD Black 1TB 64Mb Cache  WD Blue 500GB 16mb Cache 
Hard DriveOptical DriveCoolingCooling
WD Black 4TB 64mb Cache LG DVD EK-FB KIT RE4 - Acetal EK-FC970 GTX WF3 Backplate - Black 
CoolingCoolingCoolingCooling
Laing D5 Vario 12V DC Pump (MCP 655)  EK-BAY SPIN Reservoir - Plexi EK-CoolStream RAD XT (240) EK-CoolStream RAD XTX (120) 
CoolingCoolingCoolingCooling
EK-CoolStream XE 120 (Single) 8x Corsair Air SP120 PWM High Performance Edition 3x Corsair Air AF120 High Performance Edition EK-Supremacy EVO CPU Water Block (Nickel Acetal) 
CoolingOSMonitorMonitor
EK-FC970 GTX WF3 - Acetal+Nickel Windows 10 64-bit v1511 build 10586.14 BenQ RL2755HM LG W2261 
MonitorKeyboardPowerCase
Samsung S23C350 Razer BlackWidow Ultimate Cooler Master Silent Pro Gold 1000W Thermaltake Level 10 GT 
Mouse
Razer Lachesis 5600DPI 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel 3930K 5040MHz @ 1.48 - 1.53v @ VTT/VCCSA ... Asus rampage extreme IV Gigabyte GTX 970 G1 @ 1580/4050MHz @ 1.325V Corsair Vengeance 64GB (8 x 8GB) DDR3 2400MHz. ... 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 Pro 256GB Seagate Barracuda ST3000DM001 3TB WD Black 1TB 64Mb Cache  WD Blue 500GB 16mb Cache 
Hard DriveOptical DriveCoolingCooling
WD Black 4TB 64mb Cache LG DVD EK-FB KIT RE4 - Acetal EK-FC970 GTX WF3 Backplate - Black 
CoolingCoolingCoolingCooling
Laing D5 Vario 12V DC Pump (MCP 655)  EK-BAY SPIN Reservoir - Plexi EK-CoolStream RAD XT (240) EK-CoolStream RAD XTX (120) 
CoolingCoolingCoolingCooling
EK-CoolStream XE 120 (Single) 8x Corsair Air SP120 PWM High Performance Edition 3x Corsair Air AF120 High Performance Edition EK-Supremacy EVO CPU Water Block (Nickel Acetal) 
CoolingOSMonitorMonitor
EK-FC970 GTX WF3 - Acetal+Nickel Windows 10 64-bit v1511 build 10586.14 BenQ RL2755HM LG W2261 
MonitorKeyboardPowerCase
Samsung S23C350 Razer BlackWidow Ultimate Cooler Master Silent Pro Gold 1000W Thermaltake Level 10 GT 
Mouse
Razer Lachesis 5600DPI 
  hide details  
Reply
post #5 of 10
Thread Starter 
Well, that's progress at least. I've learned quite a bit about the correct syntax already.


Now it actually PWMs the LED, however it only ever stays on the buttonstateon=true setting (75) and ignores button input. From looking at it, I thought that the code is actually seeing the button input the same for both buttonstateon/off because the read doesn't specify that it's looking for a specific value from the switch. So, I tried reversing the logic of the buttonstateoff with !button on the read, but it still ignores it and keeps the on 75% PWM setting regardless of the button.

Your question about the button.Read(); is basically to the heart of the issue that I don't really know how to fix. It's supposed to direct the controller to read the state of the switch, on(|) or off(0).
Raptor Raper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Something with transistors FOXCONN Boodrage -> eVGA classified e760 GTX 480 / HD6850@1090/1200 3x2 gig G Skill Trident DDR2000 
Hard DriveOptical DriveOSMonitor
2x128GB Crucial C300 Sumsung SATA DVD-RW 64 bit Windows 7 Samsung 245BW 
KeyboardPowerCaseMouse
Microsoft curve Tagan ITZ 800W DD Torture Rack Diamondback 
  hide details  
Reply
Raptor Raper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Something with transistors FOXCONN Boodrage -> eVGA classified e760 GTX 480 / HD6850@1090/1200 3x2 gig G Skill Trident DDR2000 
Hard DriveOptical DriveOSMonitor
2x128GB Crucial C300 Sumsung SATA DVD-RW 64 bit Windows 7 Samsung 245BW 
KeyboardPowerCaseMouse
Microsoft curve Tagan ITZ 800W DD Torture Rack Diamondback 
  hide details  
Reply
post #6 of 10
Try this:
Code:
bool buttonstate = false

while (true)
{
     buttonstate = button.Read();

     if (buttonstate)                                              
     {     
          LED.SetDutyCycle(75);                                      
     }

     if (!buttonstate)                                                 
    {   
          LED.SetDutyCycle(25);          
    }
}
post #7 of 10
Thread Starter 
Well damn, that worked.

I'm not sure why only that format worked, because it seems like it's doing the same thing.
Raptor Raper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Something with transistors FOXCONN Boodrage -> eVGA classified e760 GTX 480 / HD6850@1090/1200 3x2 gig G Skill Trident DDR2000 
Hard DriveOptical DriveOSMonitor
2x128GB Crucial C300 Sumsung SATA DVD-RW 64 bit Windows 7 Samsung 245BW 
KeyboardPowerCaseMouse
Microsoft curve Tagan ITZ 800W DD Torture Rack Diamondback 
  hide details  
Reply
Raptor Raper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Something with transistors FOXCONN Boodrage -> eVGA classified e760 GTX 480 / HD6850@1090/1200 3x2 gig G Skill Trident DDR2000 
Hard DriveOptical DriveOSMonitor
2x128GB Crucial C300 Sumsung SATA DVD-RW 64 bit Windows 7 Samsung 245BW 
KeyboardPowerCaseMouse
Microsoft curve Tagan ITZ 800W DD Torture Rack Diamondback 
  hide details  
Reply
post #8 of 10
I'll try to explain it.

Your version you asked read only once and even though you write off or on in your boolean name doesn't make the program to understand what your trying to accomplish so if button was pressed it would make both on and off boolean true. So only one boolean is necessary in that function.
Secondly your program only goes throught the program once so I added while loop to it and inside the while loop we want to read the state of the button if it's either true or false. After that we want if statements to so program does what it needs to do in either buttonstate.

Not sure if you understands but feel free to ask more.
post #9 of 10
Thread Starter 
Ah, that does make sense. So the program was basically turning on, running through the script and doing whatever state it saw the first time through. (Which, because of a firmware error that I need to update, reads the button as ON by default rather than off which explained the LED being lit at high PWM.)

You put a while in my while (so that I can while while I while) in order to loop the button read and update as it's pushed.
Raptor Raper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Something with transistors FOXCONN Boodrage -> eVGA classified e760 GTX 480 / HD6850@1090/1200 3x2 gig G Skill Trident DDR2000 
Hard DriveOptical DriveOSMonitor
2x128GB Crucial C300 Sumsung SATA DVD-RW 64 bit Windows 7 Samsung 245BW 
KeyboardPowerCaseMouse
Microsoft curve Tagan ITZ 800W DD Torture Rack Diamondback 
  hide details  
Reply
Raptor Raper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Something with transistors FOXCONN Boodrage -> eVGA classified e760 GTX 480 / HD6850@1090/1200 3x2 gig G Skill Trident DDR2000 
Hard DriveOptical DriveOSMonitor
2x128GB Crucial C300 Sumsung SATA DVD-RW 64 bit Windows 7 Samsung 245BW 
KeyboardPowerCaseMouse
Microsoft curve Tagan ITZ 800W DD Torture Rack Diamondback 
  hide details  
Reply
post #10 of 10
Quote:
Originally Posted by Ihatethedukes View Post

Ah, that does make sense. So the program was basically turning on, running through the script and doing whatever state it saw the first time through. (Which, because of a firmware error that I need to update, reads the button as ON by default rather than off which explained the LED being lit at high PWM.)
You put a while in my while (so that I can while while I while) in order to loop the button read and update as it's pushed.

Well it's techically not an error since pin state is really on if button is't pressed (firmware update just inverts pin state in program) but otherwise that is correct.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [Resolved] Netduino Help With Coding (Ultra-beginner)