View previous topic :: View next topic |
Author |
Message |
will
Joined: 28 May 2012 Posts: 24 Location: france
|
pushbutton 16F88 |
Posted: Tue Apr 16, 2013 4:01 am |
|
|
hello
I made a program, when I did nothing a blinking LED orange
and when I press a push button LED lights and the orange light stops flashing (like a traffic light).
But I want that when I press the button pusher, the led orange remains stationary for a few seconds before starting ammuge other 4 led
You have an solution?
Code: | #include <16F88.h>
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP
#use delay(clock=20000000)
#use standard_io (B)
#define pushbouton PIN_B0
#define led_rouge PIN_B1
#define led_orange PIN_B2
#define led_vert PIN_B3
#define led_BLUE PIN_B4
int8 count;
#int_TIMER1
TIMER1_isr()
{
}
void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(1)
{
//clignotant
output_high(PIN_B2);
for (count=0;count<100;count++) {
if (input(PIN_B0)) break;
delay_ms(10); }
output_low(PIN_B2);
for (count=0;count<100;count++) {
if (input(PIN_B0)) break;
delay_ms(10); }
//===================
//RED
output_high(PIN_B1);
for (count=0;count<100;count++) {
if (!input(PIN_B0)) break;
delay_ms(20); }
output_low(PIN_B1);
//===================
//GREEN
output_high(PIN_B3);
for (count=0;count<100;count++) {
if (!input(PIN_B0)) break;
delay_ms(50); }
output_low(PIN_B3);
//===================
//ORANGE
output_high(PIN_B2);
for (count=0;count<100;count++) {
if (!input(PIN_B0)) break;
delay_ms(20); }
output_low(PIN_B2);
//===================
//BLUE
output_high(PIN_B4);
for (count=0;count<100;count++) {
if (!input(PIN_B0)) break;
delay_ms(20); }
output_low(PIN_B4);
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Apr 16, 2013 6:42 am |
|
|
This is a classic example for where the state machine programming model can be used.
http://en.wikipedia.org/wiki/Finite-state_machine
First you create a drawing of all the different states the program can be in, a State Diagram. Then you draw arrows between the states for all events that can cause a state change. Documenting your program like this will create a better understanding, but also helps you a lot to create the program because this is a skeleton to use as a base for further implementation.
Implementation often consists of an endless while-loop with a large switch-case statement. Each case statement equals one state in your State Diagram. In the while-loop you wait for events to happen, for example a button-press or timer tick. On detecting a new event you call the switch-case where the state-logic can do its thing, for example set the conditions for going to the next stage.
Here an example of the basic concept is shown: http://www.ccsinfo.com/forum/viewtopic.php?p=44373
And a thread explaining the use of function pointers versus the nested-switch state machine: http://www.ccsinfo.com/forum/viewtopic.php?t=28656 |
|
![](templates/subSilver/images/spacer.gif) |
will
Joined: 28 May 2012 Posts: 24 Location: france
|
|
Posted: Tue Apr 16, 2013 7:44 am |
|
|
thank you very much, I look at a ca
but there is not no way to turn on an LED that one time? a function that could do that?
when I do nothing the orange LED blinks then I press the pushbutton should be the orange LED remains stationary for a few seconds. And only after I turn the other led successively. |
|
![](templates/subSilver/images/spacer.gif) |
will
Joined: 28 May 2012 Posts: 24 Location: france
|
|
Posted: Sat Apr 20, 2013 4:35 am |
|
|
Hello,
there would be no way to make a statement that will execute a single time?
good day |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Apr 21, 2013 2:32 pm |
|
|
I simply don't understand EXACTLY what you are trying to do.
Please supply a clear diagram.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
will
Joined: 28 May 2012 Posts: 24 Location: france
|
|
Posted: Wed May 01, 2013 11:04 am |
|
|
Hello,
When I do nothing I have orange LED that blinks
I want that when I press a pushbutton.
A blue LED stays on for a few seconds and then I have other LED that lights up without going through the blue LED
So I resume:
- LED blinks orange (boussoir the button is released)
- When the button is pressed: - orange LED goes out cligotante
- Blue LED illuminates for a few seconds.
- And other LED lights one after the other in a loop without going through the blue LED.
So I want to make a statement that executes only once (which will light the blue led only once)
thank you to you |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed May 01, 2013 1:53 pm |
|
|
What you've written here is the bones of your code.
Quote: | - LED blinks orange (boussoir the button is released)
- When the button is pressed: - orange LED goes out cligotante
- Blue LED illuminates for a few seconds.
- And other LED lights one after the other in a loop without going through the blue LED.
|
Part translated to 'C' it looks like this:-
Code: | void main(void)
{
while (button_not_pressed)
{
flash_orange_LED;
}
turn_orange_LED_off;
turn_blue_LED_on;
delay_few_seconds;
turn_blue_LED_off
while(1)
{
light_other_LEDs_in_turn;
}
}
|
You now simply write the code for each of the sub-functions.
Use the delay_ms(xx) function to start with, then progress to a timer if needed.
eg. replace delay_few_seconds with delay_ms(few000)
Mike |
|
![](templates/subSilver/images/spacer.gif) |
will
Joined: 28 May 2012 Posts: 24 Location: france
|
|
Posted: Sun May 12, 2013 7:06 am |
|
|
Hello thank you for your reply,
I did like you.
But the problem is if the final stage (when all the LEDs are lit) if I release the button, it does not fall flashing (all the LEDs remain fixed) ..
You have a solution?
a big thank you |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9173 Location: Greensville,Ontario
|
|
Posted: Sun May 12, 2013 7:33 am |
|
|
Will
you'll have to show us your code before we can help as there are hundreds of ways to do this !
hth
jay |
|
![](templates/subSilver/images/spacer.gif) |
will
Joined: 28 May 2012 Posts: 24 Location: france
|
|
Posted: Sun May 12, 2013 7:55 am |
|
|
I put my code,
when I release the button lA LED remains fixed, it is not good.
The orange light should normally flashes when the button is released
thank you
Code: |
// b0 = push button
void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
// FLASH LED ORANGE ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
while(!input(PIN_B0))
{
output_high(PIN_B2);
for (count=0;count<100;count++) {
if (input(PIN_B0)) break;
delay_ms(10);
}
output_low(PIN_B2);
for (count=0;count<100;count++) {
if (input(PIN_B0)) break;
delay_ms(10); }
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//LED ORANGE FIXED
output_high(PIN_B2);
delay_ms(1000);
output_low(PIN_B2);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//LIGHTING THE OTHER LED successively
while(1)
{
//ROUGE
output_high(PIN_B1); output_HIGH(PIN_B5);
for (count=0;count<100;count++) {
if (!input(PIN_B0)) break;
delay_ms(20); }
output_low(PIN_B1); output_low(PIN_B5);
//VERT
output_high(PIN_B3); output_HIGH(PIN_B5);
for (count=0;count<100;count++) {
if (!input(PIN_B0)) break;
delay_ms(50); }
output_low(PIN_B3); output_low(PIN_B5);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun May 12, 2013 4:37 pm |
|
|
Several things.
1) The system's doing what you are telling it to do.
2) Sensible indenting would make your program flow clearer.
3) With better indenting you should be able to see the fault for yourself.
4) Each closing } needs to be immediately below its opening { with white space between.
5) Why have you got the !input(PIN_B0) test?
6) Try playing at being a computer and follow the process step by step.
7) Your code is not complete and compilable, so we can't copy and paste to test for ourselves.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9173 Location: Greensville,Ontario
|
|
Posted: Sun May 12, 2013 5:27 pm |
|
|
also..
as this is a 'simple' program, get rid of the use of interrupts.
not really needed for a 'pushbutton-LED action/reaction' program.
as Mike says, it's not a complete program, so you could have errors elsewhere
hth
jay |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon May 13, 2013 9:41 am |
|
|
Your logic is flawed.
a) What happens if you press button B0 again, after it's released?
b) What happens if you remove the two lines which test for !input(PIN_B0)?
c) Do these two things tell you anything?
I've changed the layout of your partial code to show what I mean by indenting.
I've not changed the code at all, just the formatting.
Mike
PS Re-formatted code
Code: | // b0 = push button
void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
// FLASH LED ORANGE ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
while(!input(PIN_B0))
{
output_high(PIN_B2);
for (count=0;count<100;count++)
{
if (input(PIN_B0)) break;
delay_ms(10);
}
output_low(PIN_B2);
for (count=0;count<100;count++)
{
if (input(PIN_B0)) break;
delay_ms(10);
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//LED ORANGE FIXED
output_high(PIN_B2);
delay_ms(1000);
output_low(PIN_B2);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//LIGHTING THE OTHER LED successively
while(1)
{
//ROUGE
output_high(PIN_B1); output_HIGH(PIN_B5);
for (count=0;count<100;count++)
{
if (!input(PIN_B0)) break;
delay_ms(20);
}
output_low(PIN_B1); output_low(PIN_B5);
//VERT
output_high(PIN_B3); output_HIGH(PIN_B5);
for (count=0;count<100;count++)
{
if (!input(PIN_B0)) break;
delay_ms(50);
}
output_low(PIN_B3); output_low(PIN_B5);
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
will
Joined: 28 May 2012 Posts: 24 Location: france
|
|
Posted: Thu May 23, 2013 11:04 am |
|
|
Hello
here I tried again but I do not.
I will put my complete code (without passing through the solid orange)
I summarize: when nothing is going on, the LED blinks orange.
Then, when I hold the button down, the LED stops flashing and the password fixed for 2 seconds.
Then the traffic light starts.
a big thank you
Code: | #include <16F88.h> // Inclusion du Header correspondant au µC utilisé
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP // Paramétrage des fusibles du microcontrôleur
#use delay(clock=20000000)
#use standard_io (B)
#define led_rouge PIN_B1
#define led_orange PIN_B2
#define led_vert PIN_B3
#define PUSHBUTTON PIN_B0
int8 count;
#int_TIMER1
TIMER1_isr()
{
}
void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(1)
{
// ORANGE blinks when push button is released
output_high(PIN_B2);
for (count=0;count<100;count++)
{
if (input(PIN_B0)) break;
delay_ms(10);
}
output_low(PIN_B2);
for (count=0;count<100;count++)
{
if (input(PIN_B0)) break;
delay_ms(10);
}
//...................
//????
// ORANGE fixed two seconds (just after the button be pressed)??????????????????????????????????????????
//????
//....................................
//FIRE tricolor starts, just after the extinction of solid orange 2seconde
//Red
output_high(PIN_B1);
for (count=0;count<100;count++)
{
if (!input(PIN_B0)) break;
delay_ms(30);
}
output_low(PIN_B1);
//green
output_high(PIN_B3);
for (count=0;count<100;count++)
{
if (!input(PIN_B0)) break;
delay_ms(60);
}
output_low(PIN_B3);
//orange
output_high(PIN_B2);
for (count=0;count<100;count++)
{
if (!input(PIN_B0)) break;
delay_ms(30);
}
output_low(PIN_B2);
//red
output_high(PIN_B1);
for (count=0;count<100;count++)
{
if (!input(PIN_B0)) break;
delay_ms(50);
}
output_low(PIN_B1);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu May 23, 2013 2:52 pm |
|
|
Several things:-
1) Does your code do what you want it to do?
2) The interrupt routine does nothing, so it's better removed totally.
3) Your code is now almost impossible to follow.
4) The indentation is awful.
If your code does not do what you want then explain clearly:-
a) What the code does do.
b) What you want it to do.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
|