Main Site Documentation

Using a fez cerberus to trigger circuits and controlling a servo


#1

Hello I need some help this is my first time using the .NET Gadgeteer framework and I am using it with a Fez Cerberus microcontroller. The intent of my project is to playback edison wax cylinder records with lasers. For this I am currently trying to use the cerberus to turn the individual circuits on and off when a push button module is pressed. i would like to use a push button module as a toggle switch for turning on my amplifier, detector, and laser driver circuits either simultaneously or one after another which ever proves better. therefore i simply need help with getting the boards code correct for creating digital outputs on three of the pins on a breakout module similar to the extender module. I would then be using these digital outputs to activate transistors or relays to turn on the individual circuits but keep them isolated from the cerberus. I have created the code already but although there are no errors according to Microsoft visual the code still throws this error when it is run.
this happens only once the ssbutton has been pushed

“A first chance exception of type ‘Gadgeteer.Socket.InvalidSocketException’ occurred in Gadgeteer.dll
Error invoking method “Gadgeteer.Interfaces.InterruptInput” (check arguments to Program.BeginInvoke are correct)”

Again I am aware of the fact i could simply use a plain toggle switch or three to do this job. However I would like to do it this way because it should not be very complicated but particularly because it will introduce me to how i can use a microcontroller to accomplish these tasks. Also i could then expand my project later so that it is remote controlled, or perhaps uses timers in the code or additional sensors on the phonograph to provide automatic start and stop capabilities.

Furthermore I have decided to use the cerberus as a way to control the volume I plan to use two pushbutton modules as momentary switches, one for volume increase and the other to decrease it.
For this the cerberus could use as many additional pins on the breakout board as are necessary, probably two or three. However I have two full breakout boards so the number of pins isn’t really a big concern.

The cerberus would be expected to either run a servo motor backwards or forwards to turn the volume up or down respectively in order to turn a potentiometer and change the level. however I have looked at the example code for analog input and output and was thinking the cerberus could also be directly used to scale the voltage. perhaps with the setlinearscale code?however this would probably pose the same speed issue mentioned in another topic of mine in that the .NET Gadgeteer framework wouldn’t be fast enough to allow the board to sample at 40kHz already mentioned therefore the motor would be fine too. Also when doing this I would like to be able to control the speed of the servo.

I have seen the following videos on youtube so I don’t think controlling the servo would be hard but i still need help with the code:



obviously i would want the servo to run at a steady speed but be able to change that speed with the program. furthermore i want to run it by using one of the two push-button modules pushing one would drive the servo forward. Then pushing the other button would run it backwards. Finally it would only run while the buttons are held down.

If there needs to be additional circuitry to accomplish these tasks please let me know what it would be.

Like I said this may seem to be overcomplicating something simple because i could just use basic switches and a potentiometer. However the point is to introduce myself to mechatronic control systems and embedded coding. therefore i need help with creating some basic digital outputs and driving the motor/changing volume based on pushbutton inputs. Then if I later want to I can expand on this by making it remote controlled or based on other sensors such as motion. Thank you again also here is the code i have

using Microsoft.SPOT;
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Interfaces;
 
namespace PhonographReader
{
    public partial class Program : Gadgeteer.Program
    {  
 
        void ProgramStarted()
        {
            //create boolean for if start stop pushbutton is pressed
            bool SSPressed = SSbutton.IsPressed;

            SSbutton.TurnLEDOff();//turn off led on SSButton
            VolumeUp.TurnLEDOff();//turn off led on Volume Up Button
            VolumeDown.TurnLEDOff();//turn off led on Volume down Button
 

            SSbutton.TurnLEDOff();
            VolumeUp.TurnLEDOff();
            VolumeDown.TurnLEDOff();
            Mainboard.SetDebugLED(true);

            SSbutton.ButtonPressed += new Button.ButtonEventHandler(SSbutton_ButtonPressed);

            VolumeDown.ButtonPressed += new Button.ButtonEventHandler(VolumeDown_ButtonPressed);

            VolumeDown.ButtonReleased += new Button.ButtonEventHandler(VolumeDown_ButtonReleased);

            VolumeUp.ButtonPressed += new Button.ButtonEventHandler(VolumeUp_ButtonPressed);

            VolumeUp.ButtonReleased += new Button.ButtonEventHandler(VolumeUp_ButtonReleased);
        }


        void SSbutton_ButtonPressed(Button sender, Button.ButtonState state)
        {    
           //declare breakout sockets
            GT.Socket TrigSocket;
            GT.Socket LDSocket;
             //when start stop button is pushed toggle led on and off
            SSbutton.ToggleLED();
 

            //socket for trigger circuit breakout
            TrigSocket = GT.Socket.GetSocket(5, false, TriggerBO, "y");
            //socket for laser and detector circuits breakout
            LDSocket = GT.Socket.GetSocket(6, false, LaserBO, "y");
 
            //declare digital outputs for trigger circuits
            //declare  digital output for Laser 
            DigitalOutput LaserTrig = new DigitalOutput(TrigSocket, GT.Socket.Pin.Three, false, TriggerBO);
            //declare  digital output for Audio Amplifier    
            DigitalOutput AmpTrig = new DigitalOutput(TrigSocket, GT.Socket.Pin.Four, false, TriggerBO);
            //declare  digital output for PhotoDetector    
            DigitalOutput DetectTrig = new DigitalOutput(TrigSocket, GT.Socket.Pin.Five, false, TriggerBO);
 
            if (SSbutton.IsLedOn == true)//if ssbutton led is on
            {
                //activate digital outputs
               LaserTrig.Write(true); // Turn ON Laser 
                AmpTrig.Write(true); // Turn ON Audio Amplifier
               DetectTrig.Write(true); // Turn ON detector
            }
 
            if (SSbutton.IsLedOn == false)//if ssbutton led is off
            {

                //deactivate digital outputs
                AmpTrig.Write(false); // Turn OFF Audio Amplifier
                LaserTrig.Write(false); // Turn OFF Laser 
                DetectTrig.Write(false); // Turn OFF detector
            }
        }
        //call this function when Volume Up button is pressed
        void VolumeUp_ButtonPressed(Button sender, Button.ButtonState state)
        {
            VolumeUp.TurnLEDOn();//when button is pressed toggle led on or off
            \\insert code to run servo forwards
        }
        //call this function once the Volume Up button is released
        void VolumeUp_ButtonReleased(Button sender, Button.ButtonState state)
        {
            VolumeUp.TurnLEDOff();//when button is pressed toggle led on or off
             //insert code to stop servo     
 }
        //call this function once the Volume Down button is pressed
        void VolumeDown_ButtonPressed(Button sender, Button.ButtonState state)
        {
            
            VolumeDown.TurnLEDOn();//when button is pressed toggle led on or off
             \\insert code to run servo backwards 
        }
        //call this function once the Volume Down button is released
        void VolumeDown_ButtonReleased(Button sender, Button.ButtonState state)
        {
           
            VolumeDown.TurnLEDOff();//when button is pressed toggle led on or off
             //insert code to stop servo   
        }
    }
}

and here is the current generated code

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by the Gadgeteer Designer.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
 
using Gadgeteer;
using GTM = Gadgeteer.Modules;
 
namespace PhonographReader
{
    public partial class Program : Gadgeteer.Program
    {
        // GTM.Module definitions
        Gadgeteer.Modules.GHIElectronics.Breakout LaserBO;
        Gadgeteer.Modules.GHIElectronics.Button SSbutton;
        Gadgeteer.Modules.GHIElectronics.UsbClientSP usbClientSP;
        Gadgeteer.Modules.GHIElectronics.Button VolumeUp;
        Gadgeteer.Modules.GHIElectronics.Button VolumeDown;
        Gadgeteer.Modules.GHIElectronics.Breakout TriggerBO;
 
        public static void Main()
        {
            //Important to initialize the Mainboard first
            Mainboard = new GHIElectronics.Gadgeteer.FEZCerberus();			
 
            Program program = new Program();
            program.InitializeModules();
            program.ProgramStarted();
            program.Run(); // Starts Dispatcher
        }
 
        private void InitializeModules()
        {   
            // Initialize GTM.Modules and event handlers here.		
            VolumeDown = new GTM.GHIElectronics.Button(2);
 
            SSbutton = new GTM.GHIElectronics.Button(3);
 
            VolumeUp = new GTM.GHIElectronics.Button(4);
 
            LaserBO = new GTM.GHIElectronics.Breakout(5);
 
            TriggerBO = new GTM.GHIElectronics.Breakout(6);
 
            usbClientSP = new GTM.GHIElectronics.UsbClientSP(8);
 
        }
    }
}

The trigger breakout would be where the triggering circuits would be connected and I would also like to preferabbly attach the servo to the same breakout.

and finally the display in microsoft studio where I used the graphics to connect the modules (sorry not sure what the right term is.) is attached

THANK YOU again.


#2

@ ryans92 - A servo is controlled using PWM, so you will probably want to have one of your breakout modules connected to socket 3 or 4 which have PWM support. Alternatively you could use the IO60P16 module or the Pulse/IO module which can be controlled from any X socket.

I will need to look closer at your code to see what the problem is with the interrupt exception, but in the meantime you can probably find some sample code on codeshare for driving servos.

While you could probably drive the servo from the 5v, it might be better to use external power to drive the servo and just have the PWM signal sourced from the mainboard. I will leave it to the EE experts to comment on that however. I would hate to give you bad advice that damages your board.


#3

Step 1. Move all your initialisation code out of the button handler !

           //declare breakout sockets
            GT.Socket TrigSocket;
            GT.Socket LDSocket;
             //when start stop button is pushed toggle led on and off
            SSbutton.ToggleLED();
 
 
            //socket for trigger circuit breakout
            TrigSocket = GT.Socket.GetSocket(5, false, TriggerBO, "y");
            //socket for laser and detector circuits breakout
            LDSocket = GT.Socket.GetSocket(6, false, LaserBO, "y");
 
            //declare digital outputs for trigger circuits
            //declare  digital output for Laser 
            DigitalOutput LaserTrig = new DigitalOutput(TrigSocket, GT.Socket.Pin.Three, false, TriggerBO);
            //declare  digital output for Audio Amplifier    
            DigitalOutput AmpTrig = new DigitalOutput(TrigSocket, GT.Socket.Pin.Four, false, TriggerBO);
            //declare  digital output for PhotoDetector    
            DigitalOutput DetectTrig = new DigitalOutput(TrigSocket, GT.Socket.Pin.Five, false, TriggerBO);
 

That code needs to be set up once, not every time you press a button.


#4

ok well I moved my intialization code into the main class but it then when I would try to set it true or false in the button classes, it gave the error that the variables didn’t exist in the current context. so i put further code to send the LaserTrig, DetectTrig, and AmpTrig variables to the buttonPressed class by putting them in the declaration at which point the code gave the error that the class definitions didn’t match. heres my modiified version of the code.
Please help!

using Microsoft.SPOT;
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Interfaces;

namespace PhonographReader
{
    public partial class Program : Gadgeteer.Program
    {

        void ProgramStarted()
        {
            //create boolean for if start stop pushbutton is pressed
            bool SSPressed = SSbutton.IsPressed;
            SSbutton.TurnLEDOff();//turn off led on SSButton
            VolumeUp.TurnLEDOff();//turn off led on Volume Up Button
            VolumeDown.TurnLEDOff();//turn off led on Volume down Button

            // if (SSCounter == 0)//when SSCounter is 0 execute
            //turn off led on SSButton
            SSbutton.TurnLEDOff();
            //turn off led on Volume Up Button
            VolumeUp.TurnLEDOff();
            //turn off led on Volume down Button
            VolumeDown.TurnLEDOff();
            //activate cerberus mainboard led
            Mainboard.SetDebugLED(true);
          
            //create breakout socket variables
            GT.Socket TrigSocket;
            GT.Socket LDSocket;

            //when start stop button is pressed toggle led on or off
            SSbutton.ToggleLED();

            //declare sockets for Breakout boards on cerberus
            //socket for trigger circuit breakout
            TrigSocket = GT.Socket.GetSocket(5, false, TriggerBO, "y");
            //socket for laser and detector circuits breakout
            LDSocket = GT.Socket.GetSocket(6, false, LaserBO, "y");

            //declare digital outputs for trigger circuits
            //declare  digital output for Laser 
            DigitalOutput LaserTrig = new DigitalOutput(TrigSocket, GT.Socket.Pin.Three, false, TriggerBO);
            //declare  digital output for Audio Amplifier    
            DigitalOutput AmpTrig = new DigitalOutput(TrigSocket, GT.Socket.Pin.Four, false, TriggerBO);
            //declare  digital output for PhotoDetector    
            DigitalOutput DetectTrig = new DigitalOutput(TrigSocket, GT.Socket.Pin.Five, false, TriggerBO);
            //create event handler for when start stop button is pressed
            SSbutton.ButtonPressed += new Button.ButtonEventHandler(SSbutton_ButtonPressed);
            //create event handler for when volume down button is pressed
            VolumeDown.ButtonPressed += new Button.ButtonEventHandler(VolumeDown_ButtonPressed);
            //create event handler for when volume down button is released
            VolumeDown.ButtonReleased += new Button.ButtonEventHandler(VolumeDown_ButtonReleased);
            //create event handler for when volume up button is pressed
            VolumeUp.ButtonPressed += new Button.ButtonEventHandler(VolumeUp_ButtonPressed);
            //create event handler for when volume up button is released
            VolumeUp.ButtonReleased += new Button.ButtonEventHandler(VolumeUp_ButtonReleased);
        }

        // This method is run when the mainboard is powered up or reset.   

        //*************************************************   INDIVIDUAL CLASSES   ********************************************************
        //call this function when start stop button is pressed
        void SSbutton_ButtonPressed(Button sender, Button.ButtonState state, DigitalOutput DetectTrig, DigitalOutput LaserTrig, DigitalOutput AmpTrig)
        {
            //when start stop button is pressed toggle led on or off
            SSbutton.ToggleLED();

            if (SSbutton.IsLedOn == true)//if ssbutton led is on
            {
                //activate digital outputs
                LaserTrig.Write(true); // Turn ON Laser 
                AmpTrig.Write(true); // Turn ON Audio Amplifier
                DetectTrig.Write(true); // Turn ON detector
            }

            if (SSbutton.IsLedOn == false)//if ssbutton led is off
            {
               
                //deactivate digital outputs
                AmpTrig.Write(false); // Turn OFF Audio Amplifier
                LaserTrig.Write(false); // Turn OFF Laser 
                DetectTrig.Write(false); // Turn OFF detector
            }
        }
        //call this function when Volume Up button is pressed
        void VolumeUp_ButtonPressed(Button sender, Button.ButtonState state)
        {
          
            VolumeUp.TurnLEDOn();//when button is pressed toggle led on or off
        }
        //call this function once the Volume Up button is released
        void VolumeUp_ButtonReleased(Button sender, Button.ButtonState state)
        {
           
            VolumeUp.TurnLEDOff();//when button is pressed toggle led on or off
        }
        //call this function once the Volume Down button is pressed
        void VolumeDown_ButtonPressed(Button sender, Button.ButtonState state)
        {
           
            VolumeDown.TurnLEDOn();//when button is pressed toggle led on or off
        }
        //call this function once the Volume Down button is released
        void VolumeDown_ButtonReleased(Button sender, Button.ButtonState state)
        {
   
            VolumeDown.TurnLEDOff();//when button is pressed toggle led on or off
        }
    }
}



#5

If you declare a variable inside of a function, you won’t be able to access it from other functions. Look up “variable scope”. This is elementary knowledge that you should most definitely possess before trying to write any software.

You’ve got to learn to walk before you run!


// This is a class named "Program" that derives from a base class 
// called "Program" that's located in the "Gadgeteer" namespace.
// If you don't understand what I'm talking about, stop what you're doing
// and Google "object oriented programming" -- you'll need to know
// object-oriented design to be able to program your project.
public partial class Program : Gadgeteer.Program
{
    // class variables are called "members" and are accessible 
    // from any function inside this class
    // this only *declares* the variable -- we have to call the 
    // constructor before we use it
    GT.Socket TrigSocket; 
    
    // this one is also accessible from outside of the class, since it's "public"
    public GT.Socket LDSocket;

    int someInt; // we can use this variable without initializing it because it's a basic type.

    void ProgramStarted()
    {
        // We have to call the constructor for the previously-declared TrigSocket member.
        TrigSocket = GT.Socket.GetSocket(5, false, TriggerBO, "y");

        int a = 5;  // this variable is only accessible from inside of this function
    }

    void SomeOtherFunction()
    {
        someInt = 0;
    }

}


#6

Ok Thanks Jay! yeah Ive worked with having different classes before and object oriented programing but that was with C++ not C# and it was not gadgeteer framework so its just been a bit confusing but its making more sense now!can you just help with trying to program the pwm code for the servo?
here’s my current code and i am trying to make the servo turn about 1/16 rev for every second the volume up or down button is held (probably two while loops) in the respective classes. and depending on which one is pushed i want to run the servo forwards or backwards.
Thanks

using Microsoft.SPOT;
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Interfaces;

namespace PhonographReader
{
    public partial class Program : Gadgeteer.Program
    {
        //create breakout socket variables
        public GT.Socket TrigSocket;
        public GT.Socket LDSocket;
        public DigitalOutput DetectTrig;
        public DigitalOutput LaserTrig;
        public DigitalOutput AmpTrig;

        void ProgramStarted()
        {
            //create boolean for if start stop pushbutton is pressed
            bool SSPressed = SSbutton.IsPressed;

            SSbutton.TurnLEDOff();//turn off led on SSButton
            VolumeUp.TurnLEDOff();//turn off led on Volume Up Button
            VolumeDown.TurnLEDOff();//turn off led on Volume down Button

            //turn off led on SSButton
            SSbutton.TurnLEDOff();
            //turn off led on Volume Up Button
            VolumeUp.TurnLEDOff();
            //turn off led on Volume down Button
            VolumeDown.TurnLEDOff();
            //activate cerberus mainboard led
            Mainboard.SetDebugLED(true);

            //when start stop button is pressed toggle led on or off
            SSbutton.ToggleLED();

            //declare sockets for Breakout boards on cerberus
            //socket for trigger circuit breakout
            TrigSocket = GT.Socket.GetSocket(5, false, TriggerBO, "y");
            //socket for laser and detector circuits breakout
            LDSocket = GT.Socket.GetSocket(6, false, LaserBO, "y");

            //declare digital outputs for trigger circuits
            //declare  digital output for Laser 
            DigitalOutput LaserTrig = new DigitalOutput(TrigSocket, GT.Socket.Pin.Three, false, TriggerBO);
            //declare  digital output for Audio Amplifier    
            DigitalOutput AmpTrig = new DigitalOutput(TrigSocket, GT.Socket.Pin.Four, false, TriggerBO);
            //declare  digital output for PhotoDetector    
            DigitalOutput DetectTrig = new DigitalOutput(TrigSocket, GT.Socket.Pin.Five, false, TriggerBO);

            //create event handler for when start stop button is pressed
            SSbutton.ButtonPressed += new Button.ButtonEventHandler(SSbutton_ButtonPressed);
            //create event handler for when volume down button is pressed
            VolumeDown.ButtonPressed += new Button.ButtonEventHandler(VolumeDown_ButtonPressed);
            //create event handler for when volume down button is released
            VolumeDown.ButtonReleased += new Button.ButtonEventHandler(VolumeDown_ButtonReleased);
            //create event handler for when volume up button is pressed
            VolumeUp.ButtonPressed += new Button.ButtonEventHandler(VolumeUp_ButtonPressed);
            //create event handler for when volume up button is released
            VolumeUp.ButtonReleased += new Button.ButtonEventHandler(VolumeUp_ButtonReleased);


        }

        //call this function when start stop button is pressed
        void SSbutton_ButtonPressed(Button sender, Button.ButtonState state)
        {
            //when start stop button is pressed toggle led on or off
            SSbutton.ToggleLED();

            if (SSbutton.IsLedOn == true)//if ssbutton led is on
            {
                //activate digital outputs
                LaserTrig.Write(true); // Turn ON Laser 
                AmpTrig.Write(true); // Turn ON Audio Amplifier
                DetectTrig.Write(true); // Turn ON detector
            }

            if (SSbutton.IsLedOn == false)//if ssbutton led is off
            {
                //deactivate digital outputs
                AmpTrig.Write(false); // Turn OFF Audio Amplifier
                LaserTrig.Write(false); // Turn OFF Laser 
                DetectTrig.Write(false); // Turn OFF detector
            }
        }
        //call this function when Volume Up button is pressed
        void VolumeUp_ButtonPressed(Button sender, Button.ButtonState state)
        {
            VolumeUp.TurnLEDOn();//when button is pressed toggle led on or off
        }
        //call this function once the Volume Up button is released
        void VolumeUp_ButtonReleased(Button sender, Button.ButtonState state)
        {

            VolumeUp.TurnLEDOff();//when button is pressed toggle led on or off
        }
        //call this function once the Volume Down button is pressed
        void VolumeDown_ButtonPressed(Button sender, Button.ButtonState state)
        {

            VolumeDown.TurnLEDOn();//when button is pressed toggle led on or off
        }
        //call this function once the Volume Down button is released
        void VolumeDown_ButtonReleased(Button sender, Button.ButtonState state)
        {

            VolumeDown.TurnLEDOff();//when button is pressed toggle led on or off
        }
    }
}