Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Multitasking

Status
Not open for further replies.
Steering is 1 channel

Throttle is another 1 channel

So, it is either going left or right, and forward or brake (when pressed twice backward, the car go reverse).

So that is what I have here. After the braking light I want to do the Reverse Signal light. White LED.

In short, this is what I want to make. YouTube - Nismo Coppermix Silvia (TT-01) TLU-02
 
Code:
declare a variable for the throttle state, tState.

when you detect throttle forward set tState  = 1  // forward
when you detect throttle backward
{
     if tState >= 2 then tState = 3  // backup
     else tState = 2;                      // brake
}

Steering is 1 channel

Throttle is another 1 channel

So, it is either going left or right, and forward or brake (when pressed twice backward, the car go reverse).

So that is what I have here. After the braking light I want to do the Reverse Signal light. White LED.

In short, this is what I want to make. YouTube - Nismo Coppermix Silvia (TT-01) TLU-02
 
Last edited:
Some good suggestions above but, be aware that (each of) the GetPulse routine(s) could wait 20mS before returning. It may be simpler to have the variable LED fed by two pins and use two resistors and only switch 1 pin to output at any time.

Mike.

Mike, that's a great idea for the 50% or 100% duty cycle LED...

We still need a counter mechanism like 3v0's for the different flash or sequence rates.

Mike
 
so u guys suggested here to use 3v0's code? I want to try it anyway tommorow, since I am out of office right now (student working for a company).

@Mike, K8LH:

the thing I didnt really catch is
LATB &= ~Mask;
and
LATB |= Mask;

Explaining those things would relieve some stresses. LOL. Thx.

If you haven't learned how to use the MPLAB simulator yet, now is a great time to learn.

The "~" symbol is the "compliment" operator. This inverts the bits in the value that it's attached to.

You know what the "AND" or "inclusive-OR" operators do, right?

Mike
 
Last edited:
We still need a counter mechanism like 3v0's for the different flash or sequence rates.

Mike

I agree fully, I was just pointing out that trying to PWM the brake lights at the same time complicated things and was not really needed.

Mike.
 
The code I provided only samples when the loop counter is zero, there are 99 loop iterations between each sample. It could be changed to sample the streering at 0, the throttle at 33, and the brake at 66 sort of thing. Maybe he was using same input for throttle and brake, no matter. By sizing the per iteration delay and number of iterations one can get a lot of flexiablity in the spacing between samples.

I am not against using interrups but agree on not using them here.

Forgive me. I wasn't complaining about your method. It will work great. I was "thinking ahead" about some other possible methods.
 
Some good suggestions above but, be aware that (each of) the GetPulse routine(s) could wait 20mS before returning. It may be simpler to have the variable LED fed by two pins and use two resistors and only switch 1 pin to output at any time.

Mike.

Hi Mike(Pommie), If I am trying to catch your idea here. It is like:

1. Output of 2 Pins for a single or double or how many LEDs with two different resistor values before the LEDs.

2. Set it with software approach, which to light up, in between those two for 50% and 100% brightness.

3. By doing so, it reduces the complicated more-software approaches.

Do I get your idea here correct, or I am losing myself here? :D:D:D
 
Hmm after I saw these posts I just realize that later I might reprogram my LEDs flashing modes.

I wonder if I can do a lot of different pulse while:
1. 1/4 forward
2. 1/2 forward
3. full forward
4. brake
5. reverse
6. left
7. right

There are like seven (7) modes that I can play here, I wonder each of them could be programmed nicely like having no delays or lag at all when lighting these leds.

This is kinda interesting, lol because this is my hobby and i love to learn when it is my hobby too. Nice huh.
 
led2-png.19787

Here I am using the PIC to sink current.
Set RB1 as an input and put 0 on RB0 for full bright.
Set RB0 as an input and put 0 on RB1 for dim.
 

Attachments

  • led2.png
    led2.png
    8.5 KB · Views: 203
Yes you can do this using the code I provided. You need to replace this code

Code:
{
  L_LED = 0;                       // turn off left LED
  R_LED = ~R_LED;             // toggle right LED @ 2 Hz
}

But maybe first you should get the code I already gave you working :)

Hmm after I saw these posts I just realize that later I might reprogram my LEDs flashing modes.

I wonder if I can do a lot of different pulse while:
1. 1/4 forward
2. 1/2 forward
3. full forward
4. brake
5. reverse
6. left
7. right

There are like seven (7) modes that I can play here, I wonder each of them could be programmed nicely like having no delays or lag at all when lighting these leds.

This is kinda interesting, lol because this is my hobby and i love to learn when it is my hobby too. Nice huh.
 
Hi, I don't know why but the led is turned off all the time. Do I have changed LATB to 0b11000000;

setting RB6 and RB7 as input port. I have put 1k and 330 Ohms resistor, it doesnt light up. I changed to smaller resistor, also doesnt light up. What happened actually?

I pulled down to 0 to light it up, but nothing happened. Weird...
 
Hi, I don't know why but the led is turned off all the time. Do I have changed LATB to 0b11000000;

setting RB6 and RB7 as input port. I have put 1k and 330 Ohms resistor, it doesnt light up. I changed to smaller resistor, also doesnt light up. What happened actually?

I pulled down to 0 to light it up, but nothing happened. Weird...
Did you set TRISB as needed?
VDD is connected to the annode + side of the LED. When TRISB =11XXXXXX
both RB6 and RB7 are inputs and the led is off. To use the path through RB7 set TRISB=01XXXXXX and LATB=0XXXXXXX. To use the path throubh RB6 use TRISB=10XXXXXX and LATB =X0XXXXXX. Where X is a don't care.

This will not work with a debugger because RB6 and RB7 are used for ICD (In Circuit Debug). What are you using for a programmer ?
 
If you are using B6 and B7 as the two outputs to the LED then you need to do,

For the brake lights,
TRISBbits.TRISB7=1;
TRISBbits.TRISB6=0;
LATBbits.LATB6=1;

For normal lights,
TRISBbits.TRISB7=0;
TRISBbits.TRISB6=1;
LATBbits.LATB7=1;

For none,
TRISBbits.TRISB7=1;
TRISBbits.TRISB6=1;

This assumes the lowest value resistor is on B6 and the LED goes to GND.

Mike.
 
If you are using B6 and B7 as the two outputs to the LED then you need to do,

For the brake lights,
TRISBbits.TRISB7=1;
TRISBbits.TRISB6=0;
LATBbits.LATB6=1;

For normal lights,
TRISBbits.TRISB7=0;
TRISBbits.TRISB6=1;
LATBbits.LATB7=1;

For none,
TRISBbits.TRISB7=1;
TRISBbits.TRISB6=1;

This assumes the lowest value resistor is on B6 and the LED goes to GND.

Mike.

it is weird, it doesnt work. I used 47 Ohms and 220 Ohms low value resistor, but it doesnt work. I connected as 3v0 drawed yesterday. The positive 5V connected to LED+ and the minus parallel to two resistors ended at RB6 and RB7.

It doesnt light up at all.
 
As I said, that code assumed the LED went to GND. If it's connected to Vdd then you want 3v0's code.

I.E.
For the brake lights,
TRISBbits.TRISB7=1;
TRISBbits.TRISB6=0;
LATBbits.LATB6=0;

For normal lights,
TRISBbits.TRISB6=1;
TRISBbits.TRISB7=0;
LATBbits.LATB7=0;

For none,
TRISBbits.TRISB7=1;
TRISBbits.TRISB6=1;

Mike.
 
Last edited:
thanks it is working, now I will try to make the States. State1 = forward, State2 = Braking, and State3 = Reverse/backward.

Thanks Pommie.
 
The problem you had with the LEDs shows that you do not understand sinking and sourceing.

If the PIC provides current you are sourcing the LED current. The annode + side of the LED goes to the PIC pin and the cathode - goes to ground.

If the PIC recieves current you are sinking the LED current. The annode + side of the LED goes to the + (VCC) and the cathode - goes to the PIC pin.

In either case you need a current limiting resistor. I can be on either leg of the LED.
 
The problem you had with the LEDs shows that you do not understand sinking and sourceing.

If the PIC provides current you are sourcing the LED current. The annode + side of the LED goes to the PIC pin and the cathode - goes to ground.

If the PIC recieves current you are sinking the LED current. The annode + side of the LED goes to the + (VCC) and the cathode - goes to the PIC pin.

In either case you need a current limiting resistor. I can be on either leg of the LED.

:) you are correct, I was jumping to this PIC Programming cold water without bringing any swimming specs or even wearing the swimming shorts!!

By making one of pin Low, and the other side (anode) is VDD, it means there is potential difference and the current flows. And making the pin low is by setting the Tris to 0?? or LAT to 0?? Which is which, what are these basic meanings?
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top