Main Site Documentation

Connect Reflective sensor on Spider


#1

I have Fez Mini and Fez Spider and i want to move reflective sensors from mini to spider.
I tried to mimic FEZ_Components_ReflectiveSensor.cs and I wrote mine on class.
But when i call it, i get error that adc is null.

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GTI = Gadgeteer.Interfaces;
using Gadgeteer.Interfaces;
using Gadgeteer;

namespace GHIElectronics.NETMF.FEZ
{
    public static partial class FEZ_Components
    {
        public class ReflectiveSensor : IDisposable
        {
            AnalogIn adc;
            public string SensorValue;
            int trigger_level;
			
            public void Dispose()
            {
                adc.Dispose();
            }

            public ReflectiveSensor(GT.Socket.Pin pin, int socket, int reflection_detection_trigger_percentage)
            {
                GT.Socket mySocket = GT.Socket.GetSocket(socket, true, null, null);



                adc = new AnalogIn((AnalogIn.Pin)mySocket.ReservePin(pin, null));
             
                adc.SetLinearScale(0, 100);
                if (reflection_detection_trigger_percentage < 100 && reflection_detection_trigger_percentage >= 0)
                    trigger_level = reflection_detection_trigger_percentage;
                else
                    throw new Exception("You must enter a percentage value betweeon 0 and 100");
            }

            public enum DetectingState : byte
            {
                ReflectionNotDetected = 0,
                ReflectionDeteced = 1,
            }

            public DetectingState GetState()
            {
                SensorValue = adc.Read().ToString();
                Debug.Print(adc.Read().ToString());
                return (adc.Read() >= trigger_level) ? DetectingState.ReflectionDeteced : DetectingState.ReflectionNotDetected;
            }


        }
    }
}

#2

In your AnalogIn constructor you just need to use the correct AnalogInput member for example mySocket.AnalogInput3.


#3

Then i get message:
“Cannot convert type ‘Gadgeteer.Socket.SocketInterfaces.AnalogInput’ to 'GHIElectronics.NETMF.Hardware.AnalogIn.Pin”.


#4

And if I change from AnalogIn to AnalogInput I can not use SetLinearScale.


#5

Oh I see, you are using 4.1.

Then you need to use one of these:

AnalogIn.Pin.Ain6 (Pin3)
AnalogIn.Pin.Ain1 (Pin4)
AnalogIn.Pin.Ain0 (Pin5)


#6

If you mean like this

adc = new AnalogIn((AnalogIn.Pin)mySocket.[b]AnalogIn.[/b]Pin.Ain1([b]Pin4[/b]));

,
i get 2 errors:
‘Gadgeteer.Socket’ does not contain a definition for ‘AnalogIn’ and no extension method ‘AnalogIn’ accepting a first argument of type ‘Gadgeteer.Socket’ could be found (are you missing a using directive or an assembly reference?)
and
The name ‘Pin4’ does not exist in the current context .


#7

Here is portion of the code that initializes spider sockets inside the mainboard class for Gadgeteer 4.1


            socket = GT.Socket.SocketInterfaces.CreateNumberedSocket(9);
            socket.SupportedTypes = new char[] { 'A', 'O', 'S', 'U', 'Y' };
            socket.CpuPins[3] = EMX.Pin.IO46;
            socket.CpuPins[4] = EMX.Pin.IO6;
            socket.CpuPins[5] = EMX.Pin.IO7;
            socket.CpuPins[6] = EMX.Pin.IO15;
            socket.CpuPins[7] = EMX.Pin.IO24;
            socket.CpuPins[8] = EMX.Pin.IO25;
            socket.CpuPins[9] = EMX.Pin.IO27;
            socket.NativeI2CWriteRead = nativeI2C;
            socket.SerialPortName = "COM4";
            socket.SPIModule = SPI.SPI_module.SPI1;
            socket.AnalogOutput = new EMX_AnalogOut(AnalogOut.Pin.Aout0);
            socket.AnalogInput3 = new EMX_AIN(AnalogIn.Pin.Ain7);
            socket.AnalogInput4 = new EMX_AIN(AnalogIn.Pin.Ain2);
            socket.AnalogInput5 = new EMX_AIN(AnalogIn.Pin.Ain3);
            GT.Socket.SocketInterfaces.RegisterSocket(socket);

            socket = GT.Socket.SocketInterfaces.CreateNumberedSocket(10);
            socket.SupportedTypes = new char[] { 'A', 'I', 'T', 'X' };
            socket.CpuPins[3] = EMX.Pin.IO45;
            socket.CpuPins[4] = EMX.Pin.IO5;
            socket.CpuPins[5] = EMX.Pin.IO8;
            socket.CpuPins[6] = EMX.Pin.IO73;
            socket.CpuPins[7] = EMX.Pin.IO72;
            socket.CpuPins[8] = EMX.Pin.IO12;
            socket.CpuPins[9] = EMX.Pin.IO11;
            socket.NativeI2CWriteRead = nativeI2C;
            socket.AnalogInput3 = new EMX_AIN(AnalogIn.Pin.Ain6);
            socket.AnalogInput4 = new EMX_AIN(AnalogIn.Pin.Ain1);
            socket.AnalogInput5 = new EMX_AIN(AnalogIn.Pin.Ain0);
            GT.Socket.SocketInterfaces.RegisterSocket(socket);

so based on what socket and pin you are using you need something like this:

adc = new AnalogIn(AnalogIn.Pin.Ain6, ...)

this is will use pin3 on socket 10.


#8

OK, tnx a lot, I’ll try it out tomorrow.


#9

You are welcome!


#10

It works like a charm.
Thanks again.