View previous topic :: View next topic |
Author |
Message |
callfraga
Joined: 07 Jul 2013 Posts: 5
|
Lógica de trabalho |
Posted: Tue Oct 30, 2018 5:55 pm |
|
|
I need to complete an exercise. I'm having difficulties. I'm a beginner in programming but studying every day to learn. In my case I need to do the logic described in the code I made of a similar type, but I can not get success. I put it here for anyone who can give me a way and tell me where I am wrong for me to correct.
Which I only saw condition 1, I know is correct to my understanding.
https://uploaddeimagens.com.br/imagens/condicao__1-png
https://uploaddeimagens.com.br/imagens/desenho_eletrico-png
Code: |
/* Code for with PIC16F628 doing the operation according to the table.
TRUTH DESCRIBED BELOW
***************** TRUTH TABLE ***************
CONDITION_1: "BOT1" APPEARS // CONNECTIVES: LED1-LED2-LED-3 - >> WHEN SOLAR BOT1 TURN OFF LED1-LED2-LED-3
CONDITION_2: DELETE "BOT2" // LEAVE: LED1-LED-3 - >> WHEN SOLAR BOT2 DELETE LED1-LED-3
CONDITION_3: "BOT3" APPEARS // LEAD: LED1-LED2 - >> WHEN BOT3 SOLAR SWITCH OFF LED1-LED-2
NOTE: INDEPENDENT OF WHICH TIGHTEN BUTTON TURNED ON WHEN PRECEDED AND TURNED OFF NO SEQUENCE TO TIGHTEN.
*/
//Informa ao compilador qual PIC escolhido para biblioteca PIC16F628A
#include <16F628a.h> // Inserir a biblioteca PIC16F628A
//Informa ao compilador as configurações escolhidas para o PIC16F628A
#fuses INTRC_IO // uso de oscilador interno e pinos de clock como I/O
#fuses NOWDT //Sem Cão de Guarda (Watchdog)
#fuses NOPROTECT //Sem proteção para o código fonte
#fuses NOLVP //Não habilitado gravação com baixo nível de tensão
#fuses MCLR // MASTER CLEAR habilitado
//Informa ao compilador a velocidade do clock escolhido (4Mhz)
#use delay(clock=4000000)
//DEFINIÇÃO PINO DE ENTRADA:
#define BOT1 PIN_A0 //DEFINE PINO RA0 COMO BOT1
#define BOT2 PIN_A1 //DEFINE PINO RA1 COMO BOT2
#define BOT3 PIN_A2 //DEFINE PINO RA2 COMO BOT3
//DEFINIÇÃO PINO DE SAÍDA:
#define LED1 PIN_B0 //DEFINE PINO RB0 COMO LED1
#define LED2 PIN_B1 //DEFINE PINO RB1 COMO LED2
#define LED3 PIN_B2 //DEFINE PINO RB2 COMO LED3
void main(){
while (true){
//QUANDO (PIN_A0) "BOT1" RECEBER SINAL FOR APERTADO
if ( input(pin_A0)==1 || input(pin_A1)==0|| input(pin_A2)==0 ){
output_high(pin_B0); //LIGA PINO RB0 LED1
output_high(pin_B1); //LIGA PINO RB1 LED2
output_high(pin_B2); //LIGA PINO RB2 LED3
} else {
output_low(pin_B0); //DESLIGA PINO RB0 LED1
output_low(pin_B1); //DESLIGA PINO RB1 LED2
output_low(pin_B2); //DESLIGA PINO RB2 LED3
}
//======================================================================
//QUANDO O(PIN_A1) "BOT2" RECEBER SINAL FOR APERTADO
if ( input(pin_A1)==1 || input(pin_A0)==0|| input(pin_A2)==0 ){
output_high(pin_B0); //LIGA PINO RB0 LED1
output_high(pin_B2); //LIGA PINO RB0 LED3
} else {
output_low(pin_B0); //DESLIGA PINO RB0 LED1
output_low(pin_B2); //DESLIGA PINO RB0 LED3
}
//QUANDO O(PIN_A2) "BOT3" RECEBER SINAL FOR APERTADO
if ( input(pin_A2)==1 || input(pin_A0)==0|| input(pin_A1)==0 ){
output_high(pin_B0); //LIGA PINO RB0 LED1
output_high(pin_B1); //LIGA PINO RB0 LED2
} else {
output_low(pin_B0); //DESLIGA PINO RB0 LED1
output_low(pin_B1); //DESLIGA PINO RB0 LED2
}
} // finalize loop infinito While True
} // Finalize my condition main(). I thank you for the attention that can give me a path where I made a mistake or a tip to make a precise and lean source.
|
Obrigado |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9097 Location: Greensville,Ontario
|
|
Posted: Tue Oct 30, 2018 7:20 pm |
|
|
Start with the hardware.
Place a capacitor, say .1mfd, in parallel with the resistor at each button.
Mechanical switches are very 'noisey', they can produce several 'on-off' conditions known as 'contact bounce'.
The resistors can be 1K to 10K, and you may need .47 or 1mfd caps. Ideally you would use an oscilloscope to see the waveform pushing the button creates.
Jay |
|
|
callfraga
Joined: 07 Jul 2013 Posts: 5
|
|
Posted: Tue Oct 30, 2018 7:38 pm |
|
|
Thanks a lot for the help. I understood how to do it, but even so it is my programming logic it does not run 100% like the truth table. I would like to make the truth table work. I will put the deboucing RCs. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19215
|
|
Posted: Wed Oct 31, 2018 2:02 am |
|
|
Think about your logic. You are using '||'. OR.
Condition 1 will occur if BOT1 is off OR BOT2 is on OR BOT3 is on. Any of these conditions
Then Condition 2 will occur if BOT2 is off OR BOT1 is on OR BOT3 is on. Again any of these conditions.
Then Condition 3 will occur if BOT3 is off OR BOT1 ir or or BOT2 is on. Again any of these conditions....
If you want Condition 1 when BOT1 is released _and_ the other buttons are pressed, then you need to be looking at && logic, not OR.... |
|
|
alan
Joined: 12 Nov 2012 Posts: 349 Location: South Africa
|
|
Posted: Wed Oct 31, 2018 2:23 am |
|
|
More on your logic, look at your program flow:
When you start the loop and BOT1 evaluate to FALSE then you switch all outputs LOW.
If BOT3 evaluates to TRUE then you switch B0 and B1 high and a short time later you switch them off again when restarting the loop.
Unless this are the behavior you want. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9097 Location: Greensville,Ontario
|
|
Posted: Wed Oct 31, 2018 5:20 am |
|
|
As Alan points out ..you need a delay in your main loop so that you can see the result of the button presses..something I missed as I look to fix hardware first, then code.
After every test add a simple delay, say 50ms maybe longer.
Also while you have 'named' or defined various pins to be BOTs or LEDs, you don't actually use the 'names', instead you use the PIN_assignments. It'd be better to use the defined names. Easier to understand what should happen, easy to remap or change when hardware is changed and less typing.
Jay |
|
|
callfraga
Joined: 07 Jul 2013 Posts: 5
|
Lógica de trabalho |
Posted: Wed Oct 31, 2018 3:33 pm |
|
|
Hello everyone, thank you for the attention. I made this new code and I thought it got better but I still have one problem:
WHEN I TIGHTEN "BOT1" ---- // ON: LED1-LED2-LED-3 - >> WHEN
SOLAR SHUTDOWN LED1 - LED2 - LED-3
WHEN I TIGHTEN "BOT2" ---- // CONNECT: LED1-LED-3 !!!! IT IS FLASHING 1 AND 3 WHEN IT LEFT SHUT DOWN
WHEN I TIGHTEN "BOT3" ---- // CONNECT: LED1-LED2 - >> WHEN
SOLAR SHUTTER LED1 - LED2
OR BE: BUTTON 1 AND 3 ARE CERTAIN JUST NEED TO CORRECT BOT2
SOMEONE CAN GIVE A TIP OR WRITE A LINE AS IT CAN BE TO STAY ACCESS WHEN IT TAKES AND SHUTTEN WHEN I RELEASE BOT2
Code: |
void main(){
while (true){
//QUANDO (PIN_A0) "BOT1" RECEBER SINAL FOR APERTADO
if ( input(BOT1)==1){
output_high(LED1); //LIGA PINO RB0 LED1
output_high(LED2); //LIGA PINO RB1 LED2
output_high(LED3); //LIGA PINO RB2 LED3
}
delay_ms(100);
//========================================================
if ( input(BOT2)==1){
output_high(LED1); //LIGA PINO RB0 LED1
output_high(LED3); //LIGA PINO RB2 LED3
}
delay_ms(100);
//******************************************************
if ( input(BOT3)==1){
output_high(LED1); //LIGA PINO RB0 LED1
output_high(LED2); //LIGA PINO RB0 LED2
delay_ms(100);
} else {
output_low(LED1); //DESLIGA PINO RB0 LED1
output_low(LED2); //DESLIGA PINO RB1 LED2
output_low(LED3); //DESLIGA PINO RB2 LED3
}
} // finalize loop infinito While True
} //Finalizei minha condição main |
|
|
|
callfraga
Joined: 07 Jul 2013 Posts: 5
|
Re: Lógica de trabalho |
Posted: Wed Oct 31, 2018 5:15 pm |
|
|
callfraga wrote: | Hello everyone, thank you for the attention. I made this new code and I thought it got better but I still have one problem:
WHEN I TIGHTEN "BOT1" ---- // ON: LED1-LED2-LED-3 - >> WHEN
SOLAR SHUTDOWN LED1 - LED2 - LED-3
WHEN I TIGHTEN "BOT2" ---- // CONNECT: LED1-LED-3 !!!! IT IS FLASHING 1 AND 3 WHEN IT LEFT SHUT DOWN
WHEN I TIGHTEN "BOT3" ---- // CONNECT: LED1-LED2 - >> WHEN
SOLAR SHUTTER LED1 - LED2
OR BE: BUTTON 1 AND 3 ARE CERTAIN JUST NEED TO CORRECT BOT2
https://www.youtube.com/watch?v=nmSyBuUBMWc&feature=youtu.be
SOMEONE CAN GIVE A TIP OR WRITE A LINE AS IT CAN BE TO STAY ACCESS WHEN IT TAKES AND SHUTTEN WHEN I RELEASE BOT2
I don't speak English. I am using google translator.
Code: |
void main(){
while (true){
//QUANDO (PIN_A0) "BOT1" RECEBER SINAL FOR APERTADO
if ( input(BOT1)==1){
output_high(LED1); //LIGA PINO RB0 LED1
output_high(LED2); //LIGA PINO RB1 LED2
output_high(LED3); //LIGA PINO RB2 LED3
}
delay_ms(100);
//========================================================
if ( input(BOT2)==1){
output_high(LED1); //LIGA PINO RB0 LED1
output_high(LED3); //LIGA PINO RB2 LED3
}
delay_ms(100);
//******************************************************
if ( input(BOT3)==1){
output_high(LED1); //LIGA PINO RB0 LED1
output_high(LED2); //LIGA PINO RB0 LED2
delay_ms(100);
} else {
output_low(LED1); //DESLIGA PINO RB0 LED1
output_low(LED2); //DESLIGA PINO RB1 LED2
output_low(LED3); //DESLIGA PINO RB2 LED3
}
} // finalize loop infinito While True
} //Finalizei minha condição main |
|
|
|
|
alan
Joined: 12 Nov 2012 Posts: 349 Location: South Africa
|
|
Posted: Thu Nov 01, 2018 1:10 am |
|
|
Actually Button1 are also a problem, you just don't see it.
Look again at your logic.
If Button1 you switch on all LED's, wait 100ms and then carry on with program. Because Button3 are not pressed you switch of all LED's, but almost immediately switch them back on.
What I think you want to achieve will be better suited for a switch statement.
Maybe explain what your program must do. Looks more like you have a switch than a button that just get pressed. |
|
|
callfraga
Joined: 07 Jul 2013 Posts: 5
|
Re: Lógica de trabalho |
Posted: Thu Nov 01, 2018 11:52 am |
|
|
Hello everyone, thank you for the attention. I made this new code and I thought it got better but I still have one problem:
WHEN I PRESS "BOT1" ---- // ON: LED1-LED2-LED-3 - >> WHEN
SOLAR SHUTDOWN LED1 - LED2 - LED-3
WHEN I PRESS "BOT2" ---- // CONNECT: LED1-LED-3 !!!! IT IS FLASHING 1 AND 3 WHEN IT LEFT SHUT DOWN
WHEN I PRESS "BOT3" ---- // CONNECT: LED1-LED2 - >> WHEN
SOLAR SHUTTER LED1 - LED2
OR BE: BUTTON 1 AND 3 ARE CERTAIN JUST NEED TO CORRECT BOT2
SOMEONE CAN GIVE A TIP OR WRITE A LINE AS IT CAN BE TO STAY ACCESS WHEN IT TAKES AND SHUTTEN WHEN I RELEASE BOT2
Code: |
void main(){
while (true){
// WHEN (PIN_A0) "BOT1" IS PRESSED
if ( input(BOT1)==1){
output_high(LED1); //TURN ON PIN RB0 LED1
output_high(LED2); //TURN ON PIN RB1 LED2
output_high(LED3); //TURN ON PIN RB2 LED3
}
delay_ms(100);
//========================================================
if ( input(BOT2)==1){
output_high(LED1); //TURN ON PIN RB0 LED1
output_high(LED3); //TURN ON PIN RB2 LED3
}
delay_ms(100);
//******************************************************
if ( input(BOT3)==1){
output_high(LED1); //TURN ON PIN RB0 LED1
output_high(LED2); //TURN ON PIN RB0 LED2
delay_ms(100);
} else {
output_low(LED1); //TURN OFF PIN RB0 LED1
output_low(LED2); //TURN OFF PIN RB1 LED2
output_low(LED3); //TURN OFF PIN RB2 LED3
}
} //End of infinite loop While True
} //End of main code |
|
|
|
|