I am having trouble with the code for relays. I have defined them under Public Partial Class Program as “Static.GTM.Seeed.Relays LEDs;”
I have 3 Glide screens for UI on the T35 display held in index and I need to have certain relays on and off depending on what screen is showing on the T35.
When I use “LEDs.Relay1 = True;” whithin “if index == 0” there are no errors in the errors box but when I deploy the program to the Spider it throws an unhandled exception relating to the “LEDs.Relay1 = True” line.
Ok I tested the relays in separate solution and they work properly just using “relays.Relay1 = True;” and so on. The problem I having is finding the correct place to put my relay events. The program I am running is mostly related to the touch screens and Glide. I need certain relays to turn on and off depending on which of my 3 screens are up on the T35.
I need to find the correct place and way to say:
if window index = 0
relays.Relay1 = true
Here is my full code:
using System;
using System.Threading;
using Microsoft.SPOT;
//using Microsoft.SPOT.Presentation;
//using Microsoft.SPOT.Presentation.Controls;
//using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using GHIElectronics.NETMF.Glide;
using GHIElectronics.NETMF.Glide.Display;
using UI = GHIElectronics.NETMF.Glide.UI;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Modules.Seeed;
namespace device2
{
public partial class Program
{
// This will hold the windows.
static Window[] windows = new Window[3];
// Indicates the current window index.
static int index = 0;
static int count = 0;
static GT.Interfaces.AnalogInput sensor;
static GT.Interfaces.PWMOutput Piezo;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
// Load the windows
windows[0] = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.Window1));
windows[1] = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.Window2));
windows[2] = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.Window3));
// Activate touch
GlideTouch.Initialize();
// Initialize the windows.
InitWin1(windows[0]);
InitWin2(windows[1]);
InitWin3(windows[2]);
updateNavButtons();
// Assigning a window to MainWindow flushes it to the screen.
// This also starts event handling on the window.
Glide.MainWindow = windows[index];
Piezo = eBlockExpansion1.SetupPWMOutput(GT.Socket.Pin.Nine);
Debug.Print("Program Started");
}
//=============== Navigation handling ===============
void InitNav(Window window)
{
// Find the button and attach an event handler.
UI.Button backBtn = (UI.Button)window.GetChildByName("backBtn");
backBtn.TapEvent += new OnTap(backBtn_TapEvent);
UI.Button nextBtn = (UI.Button)window.GetChildByName("nextBtn");
nextBtn.TapEvent += new OnTap(nextBtn_TapEvent);
}
// Enables/disables navigation buttons appropriately.
static void updateNavButtons()
{
Window window = windows[index];
// First window
if (index == 0)
{
UI.Button backBtn = (UI.Button)window.GetChildByName("backBtn");
backBtn.Enabled = false;
// Disabling a button reduces it's alpha to 1/3 it's current value.
// Simply invalidating a button won't show the alpha change because
// it's redrawing over the current bitmap. To show this change we need
// to clear the region it occupies (fill it with background color).
window.FillRect(backBtn.Rect);
backBtn.Invalidate();
}
// Last window
if (index == windows.Length - 1)
{
UI.Button nextBtn = (UI.Button)window.GetChildByName("nextBtn");
nextBtn.Enabled = false;
window.FillRect(nextBtn.Rect);
nextBtn.Invalidate();
}
}
void nextBtn_TapEvent(object sender)
{
if (index < windows.Length - 1)
{
int lastIndex = index;
index++;
// Update before we slide so the button state(s) are visually correct while we slide.
updateNavButtons();
Tween.SlideWindow(windows[lastIndex], windows[index], Direction.Up);
}
}
void backBtn_TapEvent(object sender)
{
if (index > 0)
{
int lastIndex = index;
index--;
updateNavButtons();
Tween.SlideWindow(windows[lastIndex], windows[index], Direction.Down);
}
}
//=============== Window 1 ===============
void InitWin1(Window window)
{
// "button" is defined in Program.gadgeteer/Program.generated.cs
// (which was generated by the designer)
button.ButtonReleased += new Button.ButtonEventHandler(button_ButtonReleased);
button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
UI.Button clearCountBtn = (UI.Button)window.GetChildByName("clearBtn");
clearCountBtn.TapEvent += new OnTap(clearCountBtn_TapEvent);
InitNav(window);
}
void button_ButtonPressed(Button sender, Button.ButtonState state)
{
Piezo.Set(5000, 50);
}
void button_ButtonReleased(Button sender, Button.ButtonState state)
{
count++;
UI.TextBox textBox = (UI.TextBox)windows[index].GetChildByName("countTxt");
textBox.Text = count.ToString();
textBox.Invalidate();
Piezo.Set(5000, 0);
}
void clearCountBtn_TapEvent(object sender)
{
count = 0;
UI.TextBox textBox = (UI.TextBox)windows[index].GetChildByName("countTxt");
textBox.Text = count.ToString();
textBox.Invalidate();
}
//=============== Window 2 ===============
void InitWin2(Window window)
{
// Note the eblock expansion needs to be plugged into a socket that's labeled "A" for analog.
// In addition, only certain pins on the eblock expansion support analog. I plugged the
// light sensor into "Pin 4".
sensor = eBlockExpansion.SetupAnalogInput(GT.Socket.Pin.Four);
button1.ButtonReleased += new Button.ButtonEventHandler(button1_ButtonReleased);
button1.ButtonPressed += new Button.ButtonEventHandler(button1_ButtonPressed);
UI.Button clearGradeBtn = (UI.Button)window.GetChildByName("clearBtn");
clearGradeBtn.TapEvent += new OnTap(clearGradeBtn_TapEvent);
InitNav(window);
}
void button1_ButtonPressed(Button sender, Button.ButtonState state)
{
Piezo.Set(5000, 50);
}
void clearGradeBtn_TapEvent(object sender)
{
UI.TextBox textBox = (UI.TextBox)windows[index].GetChildByName("gradeTxt");
textBox.Text = "";
textBox.Invalidate();
}
void button1_ButtonReleased(Button sender, Button.ButtonState state)
{
// Get the voltage from the eblock
double voltage = sensor.ReadVoltage();
string text = "...";
Debug.Print("Voltage: " + voltage);
if (voltage <= 1.23)
text = "Dime";
else if (voltage <= 1.30)
text = "Nickel";
else if (voltage <= 1.55)
text = "Quarter";
else if (voltage <= 1.74)
text = "Half Dollar";
else if (voltage > 1.74)
text = "Oversized";
else if (voltage < 0)
text = "Error";
UI.TextBox textBox = (UI.TextBox)windows[index].GetChildByName("gradeTxt");
textBox.Text = text;
textBox.Invalidate();
Piezo.Set(5000, 0);
}
void InitWin3(Window window)
{
InitNav(window);
}
}
I have tried calling the relays within the Windows Index arguments and it red lines them.
I have tried defining the relays as GTM.Seed.Relays LEDs and then calling LEDs.Relay1 = True and it will accept that but then throw an unhandeled exception when I deploy to the Spider.
I don’t have the experience to know anything else to try.
The code I posted is the actual programming that is working without any code or deployment errors. I’m a beginner and I’m sorry but I don’t know to post code using code tags so it appears the same way as in the program cs.
The closest I’ve gotten to getting the relay code to work in this program is by adding this deffinition before “Program Started”;
// First window
if (index == 0)
{
UI.Button backBtn = (UI.Button)window.GetChildByName("backBtn");
backBtn.Enabled = false;
// Disabling a button reduces it's alpha to 1/3 it's current value.
// Simply invalidating a button won't show the alpha change because
// it's redrawing over the current bitmap. To show this change we need
// to clear the region it occupies (fill it with background color).
window.FillRect(backBtn.Rect);
backBtn.Invalidate();
LEDs.Relay1 = true;
I get no errors in the program cs doing this but when I deploy I get an unhandeled exception pop up and it highlights the LEDs.Relay1 = true; line.
but I cannot figure out how or where to put these events in order to get them sinc up with the windows index. I’ve tried if and while index = statements and they don’t produce erros but still once deployed, the relays do not change status when I advance forward and backward through the screens.
Here is what I’ve tried and the relays do not change state when I advance the screens. On boot up relay1 comes on and stays on no matter what screen I advance to.
using System;
using System.Threading;
using Microsoft.SPOT;
//using Microsoft.SPOT.Presentation;
//using Microsoft.SPOT.Presentation.Controls;
//using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using GHIElectronics.NETMF.Glide;
using GHIElectronics.NETMF.Glide.Display;
using UI = GHIElectronics.NETMF.Glide.UI;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Modules.Seeed;
namespace device2
{
public partial class Program
{
// This will hold the windows.
static Window[] windows = new Window[3];
// Indicates the current window index.
static int index = 0;
static int count = 0;
static GT.Interfaces.AnalogInput sensor;
static GT.Interfaces.PWMOutput Piezo;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
// Load the windows
windows[0] = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.Window1));
windows[1] = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.Window2));
windows[2] = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.Window3));
// Activate touch
GlideTouch.Initialize();
// Initialize the windows.
InitWin1(windows[0]);
InitWin2(windows[1]);
InitWin3(windows[2]);
updateNavButtons();
// Assigning a window to MainWindow flushes it to the screen.
// This also starts event handling on the window.
Glide.MainWindow = windows[index];
Piezo = eBlockExpansion1.SetupPWMOutput(GT.Socket.Pin.Nine);
switch(index)
{
case 0:
relays.Relay1 = true;
relays.Relay2 = false;
relays.Relay3 = false;
relays.Relay4 = false;
break;
case 1:
relays.Relay1 = false;
relays.Relay2 = true;
relays.Relay3 = false;
relays.Relay4 = false;
break;
case 2:
relays.Relay1 = false;
relays.Relay2 = false;
relays.Relay3 = true;
relays.Relay4 = true;
break;
}
Debug.Print("Program Started");
}
I will try the second example as well. I wonder if it would be better to attach the relay events to the touch screen “next” and “back” tapOn events?