Author Topic: LED Mod Help  (Read 814 times)

Offline CptChronic

  • Chief squatting Hard
  • *
  • Posts: 12
  • Post quality +1/-0
  • Gender: Male
  • Acidmods User
LED Mod Help
« on: December 08, 2020, 12:17:34 AM »
Hello guys, wondering if anyone can help me.

Im trying to make an led mod for my sons controller for christmas, im trying to read the trigger so when it is held passed half way it does an led combination.

I can not for the life of me, get the ADC to work properly.

Im using MPLAB X MCC and coding in C.

Code: [Select]
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();
    ADC_Initialize();
    ADC_SelectChannel(RT);
   
    while (1)
    {
        ADC_StartConversion();
   
    while (ADC_GetConversion(RT) < 300)
    {
        __delay_us(480);  // wait for voltage to settle
    }
        if (ADC_GetConversion(RT) < 300)
             
        {
        LATA = 0; //clear A pins
        LATA5 = 0;  // led set high
        TRISA5 = 0; // make output
       
        __delay_ms(500);
        TRISA5 = 1; // make input
        __delay_ms(500);
         }
    }
}

For some reason, this: if (ADC_GetConversion(RT) < 300)
Gives the same result as this: if (ADC_GetConversion(RT) > 300)

The led flashes all the time until i pull the trigger in a certain amount (the same amount wether its < or >), i want it to do the opposite but changing < to > gives the exact same result. Which correct me if im wrong is very strange as < is saying if ADC count is below 300 and > is saying if ADC count is above 300. How can that give the same result?
« Last Edit: December 08, 2020, 12:46:54 AM by CptChronic »

Offline wickated

  • King of the Wii-tards
  • *
  • Posts: 176
  • Post quality +0/-1
  • Acidmods User
Re: LED Mod Help
« Reply #1 on: December 08, 2020, 01:42:29 AM »
me  - ok one transistor would fit easily
someone else - i need arduino for this
« Last Edit: December 08, 2020, 01:43:12 AM by wickated »

Offline CptChronic

  • Chief squatting Hard
  • *
  • Posts: 12
  • Post quality +1/-0
  • Gender: Male
  • Acidmods User
Re: LED Mod Help
« Reply #2 on: December 08, 2020, 03:32:33 AM »
 :tup: Im using a pic16f18325 - ill be using many more leds once i get this working, im trying to make it as easy as possible at the moment as the ADC is not working as it should.

For some reason its working the opposite way round, the led constantly flashes when nothing is being touched, when i pull the trigger past half way it stops flashing. I need it to only flash once the trigger is pulled past half way.

If i wire this up to a potentiometer instead of the trigger, it works perfectly. I dont understand what is going on with the trigger but the ADC does not like it.
« Last Edit: December 08, 2020, 03:34:31 AM by CptChronic »

Offline wickated

  • King of the Wii-tards
  • *
  • Posts: 176
  • Post quality +0/-1
  • Acidmods User
Re: LED Mod Help
« Reply #3 on: December 08, 2020, 04:52:36 AM »

For some reason its working the opposite way round
a:=-a

Offline RDC

  • Administrator
  • Around the block
  • *
  • Posts: 2609
  • Post quality +90/-2
  • Gender: Male
  • The CGnome Project
Re: LED Mod Help
« Reply #4 on: December 08, 2020, 04:53:07 AM »
@ wickated - A Transistor isn't going to be able to tell if the Trigger is pulled half way, inverting what it's doing it isn't going to correct it, and some people like to learn new things.


@ CptChronic - The ADC is working fine. Are you familiar with exactly how the Triggers in the XB1 controller work though? They are turned on/off 125 times per second, and the Hall sensors have an output of VDD / 2, so all that needs to be taken into consideration when measuring the RT line, or for the most part the PIC will only catch it while it's off.
« Last Edit: December 08, 2020, 05:03:43 AM by RDC »
Screwing up is one of the best learning tools, so long as the only thing you're not learning is how to screw up.

Offline CptChronic

  • Chief squatting Hard
  • *
  • Posts: 12
  • Post quality +1/-0
  • Gender: Male
  • Acidmods User
Re: LED Mod Help
« Reply #5 on: December 08, 2020, 06:22:41 AM »
Thanks RDC, i have basic knowledge of how the triggers work.
Its not half as easy as i expected then, i guess i need to sync up with the triggers being powered but have no idea how. Still pretty new to this coding.

So i need it to read the trigger every 480us? I read about the 480us timing but for some reason took that as a delay. So with my code i read the trigger and if its gone over 0.05v i then wait 480us before reading again and if its still over 0.05v the code should engage.

Do you know what the voltage is once the trigger is powered up? Im guessing its more than 0.05v? I did think that waiting for it to reach 0.05v would of been enough to indicate a trigger pull without the powerup interfering.

Im using the TP points for the trigger connections but am getting a much lower reading than VDD / 2
The chip will be powered by 3.3v
The trigger not pulled = 0.11v
Trigger pulled = 0.1v

I want the code to engage when the voltage passes 0.05v but for the life of me cannot get it to work the correct way round. Code is always engaged until voltage passes 0.05v, it is then halted.

I have been stuck on this point for a couple of weeks now and have tried everything i can think of. Really appreciate any help you can give. Thank you. Its getting close to christmas and im starting to panic as i promised my lad id have this ready for the big day.


Offline RDC

  • Administrator
  • Around the block
  • *
  • Posts: 2609
  • Post quality +90/-2
  • Gender: Male
  • The CGnome Project
Re: LED Mod Help
« Reply #6 on: December 08, 2020, 07:30:29 AM »
The Hall Sensors are Allegro A1304, and they are off most of the time. They are only turned on for a short period, checked by the MCU, then turned back off. If you want to detect a Trigger pull, you need to check it during the same time the MCU is checking it.

The Hall Sensors are powered by a 3.3v source that is turned on off 125 times per second, and since their output is that 3.3v / 2 the max voltage you would ever measure there could be 1.65v for an unpulled Trigger, and that is ideally. In reality it's a bit less than that, and far less than that if you're only using a DMM to 'see' what's going on there, as they are not turned on all of the time. They are on for ~1.4ms and off for ~6ms. The reason the DMM doesn't show that is because they are being turned on and off so fast. When you connect up an O-scope or Logic Analyzer you can see what is really going on there. If you're not timed up with the same time they are turned on, then all you're going to measure most of the time is them seeming to be fully pulled, but they aren't, they're just off.

The Triggers are sampled at around 125Hz, so 125 times per second they are turned on/off.

RT Not pulled to Full pull. Wasn't done particularly fast, but these 22 samples only took the controller ~170ms to do.



RT Full pull and released, same deal.



This is what is going on during a single pulse, and this happens 125 times per second.




There are a couple of ways to sync things up. You could hammer the RT line with the ADC over and over and measure when the voltage starts to rise (aka the Triggers are being turned on and checked) wait a bit for that voltage to stabilize (that's where the ~480us comes from) then sample the RT again to see if it's pulled or not and how far. You could use another IO of the PIC connected to the VDD source for the Halls and use that as the 'sync signal' to know the Halls are being turned on, wait X amount of time, sample the RT and then do what you want based on the value. The latter uses one more IO on the PIC but is a bit easier to code up.

« Last Edit: March 17, 2022, 06:00:49 PM by RDC »
Screwing up is one of the best learning tools, so long as the only thing you're not learning is how to screw up.

Offline RDC

  • Administrator
  • Around the block
  • *
  • Posts: 2609
  • Post quality +90/-2
  • Gender: Male
  • The CGnome Project
Re: LED Mod Help
« Reply #7 on: December 08, 2020, 09:06:55 AM »
This is really about all it takes to get an LED to blink when the RT is pulled half way. This is the method of hammering the RT line mentioned earlier. Keep in mind this was coded in MikroC here, so you'd need to port it there to however MPLAB X handles things.

Also, run that PIC as fast it can as it'll just make things easier timing wise so the PIC isn't sitting around doing this or that and getting out of sync. This was done on a 24F16KL402 here at 32MHz. It can be done slower for sure, but unless you have a Logic Analyzer to watch what is going on there and when it's exactly happening, just open it up and let it fly. You may also need to mess with the values to get it reliable, but it works here.

Code: [Select]

#define LED LATB6_bit                                                             // LED for feedback

unsigned int RT;
unsigned int RT_ON = 10;
unsigned int RT_PULLED = 300;

void main(){

 INIT();                                                                        // Setup the PIC config bits, OSC, TRIS, etc, etc, (not shown here since that's PIC dependent)
 
while(1){

   while(ADC1_Read(9) <= RT_ON)                                                 // While RT is turned off do nothing.
     {}                                                                         // When the RT is turning on and the voltage rises it will jump out of this while loop

   delay_us(100);                                                               // delay for the RT voltage to settle
   RT = ADC1_Read(9);                                                           // Read the RT line again here to see where it actually is

   if(RT <= RT_PULLED)                                                          // If RT is pulled the amount you want or more..
     {
     LED = ~LED;
     delay_ms(200);                                                             // .. then Blink the LED..
     }else                                                                      // Else If RT is not pulled the amount you want..
       {
       delay_ms(2);                                                             // .. then wait 2ms for the pulse to finish and start all over
       LED = 1;                                                                 // Make sure LED is off if RT is released
       }
  }
}

NOTE: LED = 1; means off there, as I have the LED setup so the PIC sinks the current to the LED, aka it controls the Cathode side of it. It will work either way, just don't overload the IO and you can source or sink it there.
« Last Edit: December 08, 2020, 11:48:23 AM by RDC »
Screwing up is one of the best learning tools, so long as the only thing you're not learning is how to screw up.

 

SMF spam blocked by CleanTalk
SimplePortal 2.3.5 © 2008-2012, SimplePortal