Button change delay..

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone...

 

Please, I need help with a delay time, I need  a button to change the value of the delay, any help, thanks...

 

code:

 

 

 while(x<7){

x++;

PORTD = PORTD | _BV(PD0);

_delay_ms(?);

PORTD &= ~_BV(PD0);

_delay_ms(?);}

 

 

 

Last Edited: Sun. Jun 25, 2017 - 03:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

_delay_ms only accepts a constant value. One common way is to have a for loop to count off calls to _delay_ms. What range of time do you need?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First caution is that _delay_ms() must have a delay that is known at compile time. So, you cannot just change the argument according to the pin state; I would probably approach it like this. By the way, you will have a much friendlier response if you use the <> button to open a code window for entering code. Then it will look like this

 

uint8_t ButtonState;

while(x<7){
    x++;
    ButtonState = BUTTONPORT & (1<<ButtonPin);
    PORTD = PORTD | _BV(PD0);
    if (ButtonState)
        _delay_ms(delayvalue0);
    else
        _delay_ms(delayvalue1);
    PORTD &= ~_BV(PD0);
    if (ButtonState)
        _delay_ms(delayvalue2);
    else
        _delay_ms(delayvalue3);

}

Where ButtonPort is replaced by the name of the port where the button is, eg, PORTC, for example. And ButtonPin is the name of the pin where the button is connected. You will want to turn on the pull-up resistor for ButtonPin. delayvalue0, and so forth, are replaced by constants (eg, 5 for 5ms delay).

 

Kartman's suggestion actually results in smaller code. For example, do _delay_ms(1) five times for 5ms (in a loop). You can change the loop count according to the button state.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Sun. Jun 25, 2017 - 01:04 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks guys, well, my problem is this, I'm recovered a old dj light with 8 step motor, the port A and B controll de direction(pins cd40194), port C controll the programs, port D send the clock to cd40194, reset and programs is ok, but the speed not, I thought what some code would solve that..     

 

#define s0 _delay_ms_(200);

#define s1 _delay_ms_(400);

#define sr  ;


(This code send a clock pulse to cd40194)...

while(x<7){

x++;

PORTD = PORTD | _BV(PD0);

sr;

PORTD &= ~_BV(PD0);

sr;}


if (PIND &(1<<PD7)){


sr s0

}

 

 

 

 

So with a button I coult change this delays..

 

sorry, I'm still half-naked..

Last Edited: Sun. Jun 25, 2017 - 03:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your define "sr" is empty. Thus, sr does nothing within the code.

 

It is much preferable to NOT include the semicolon ( ";") terminator in the macro. Having the semicolon in the macro makes it very difficult to tell when you correctly have a line terminator in code or not.

 

And, finally, PLEASE use the code button "<>" up in the message toolbar (at the end). It will give you properly indented code like I showed in message #3.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Sun. Jun 25, 2017 - 03:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You seem to have a serious misunderstanding about what the preprocessor is for and what it can and can't do. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I already knew that if I had this knowledge I would not come here to ask for help ..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

You seem to have a serious misunderstanding about what the preprocessor is for and what it can and can't do. 

 

 

I already knew that if I had this knowledge I would not come here to ask for help ..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:

Your define "sr" is empty. Thus, sr does nothing within the code.

 

It is much preferable to NOT include the semicolon ( ";") terminator in the macro. Having the semicolon in the macro makes it very difficult to tell when you correctly have a line terminator in code or not.

 

And, finally, PLEASE use the code button "<>" up in the message toolbar (at the end). It will give you properly indented code like I showed in message #3.

 

Jim

 

 

Thanks, and I sorry for this...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Let's not start with the coding just yet. First we need to know precisely what the system must do. Describe this in terms only of how the system behaves as seen from the outside, and what interfaces it has.

 

One interface is the button.

 

The other is to the light. What is the nature of this interface? One signal (i.e. wire)? Several? How does altering the signal affect the light?

 

What signals, and sequences of signals (often used term is "waveform"), do you want to send from the system to the light?

 

What do you want to happen when the button is pressed? Should the same thing happen every time?

 

The more precise and unambiguous you are in these descriptions, the easier it will be for us to help you.

 

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:

Let's not start with the coding just yet. First we need to know precisely what the system must do. Describe this in terms only of how the system behaves as seen from the outside, and what interfaces it has.

 

One interface is the button.

 

The other is to the light. What is the nature of this interface? One signal (i.e. wire)? Several? How does altering the signal affect the light?

 

What signals, and sequences of signals (often used term is "waveform"), do you want to send from the system to the light?

 

What do you want to happen when the button is pressed? Should the same thing happen every time?

 

The more precise and unambiguous you are in these descriptions, the easier it will be for us to help you.

 

 

Thanks Johan, I'll post the diagram to facilitate the understanding that I am intending...thanks again..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

The ports PA2 to the PB7 are for the rest of the motors, control direction(the code is ready and work), and PD0 clock to cd40194(here the problem) controls the speed of the motors, where I need help...(A contribution, instead of the buttons the original design uses an atmega8535 is already ready and working), I'm very grateful for your help..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for the schematic. That is one piece of the puzzle. But..

 

You have not even come close to describing how you want the system to work. Go back and do better.

 

E.g.: There are ten switches. What are they for? What should happen when each of them is pressed?

 

How is the shift register (40194) utilized. You hinted that the motor is turning but that the speed isn't what you wanted. This implies that you have code that controls the 40194 that in turn controls the motor driver (ULN2003A). Show that code.

 

We don't like guesswork, and firing shots into the dark. It's frustrating and often a waste of time. We don't like to play "20 questions" before we understand what it is you want to achieve. 

 

It is up to you to describe the problem as thoroughly and completely as you can. You are in a competition for attention here. The questions that are the nicest to work with will get the most attention. It's totally up to you if you get that attention or not.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:

Thank you for the schematic. That is one piece of the puzzle. But..

 

You have not even come close to describing how you want the system to work. Go back and do better.

 

E.g.: There are ten switches. What are they for? What should happen when each of them is pressed?

 

How is the shift register (40194) utilized. You hinted that the motor is turning but that the speed isn't what you wanted. This implies that you have code that controls the 40194 that in turn controls the motor driver (ULN2003A). Show that code.

 

We don't like guesswork, and firing shots into the dark. It's frustrating and often a waste of time. We don't like to play "20 questions" before we understand what it is you want to achieve. 

 

It is up to you to describe the problem as thoroughly and completely as you can. You are in a competition for attention here. The questions that are the nicest to work with will get the most attention. It's totally up to you if you get that attention or not.

 

Thanks Johan..

 

Each button sends a hex to the ports, each hex containing the way each ports will work, what happens is that each hex should see 7 pulses (limit of the mortor in both directions) and a time (this time varies according to speed) and that is my problem, not getting that time to enter the code. See that the code below has a delay, this is what I would need to be changed as a button pressed, I do not know if I am clear, follows an example of how I intend to do, sorry for the blunders, but I am new in the subject .. .

 

Something like that..

 
 #ifndef F_CPU
 #define F_CPU 1000000UL // 1 MHz clock speed
 #endif
 
 
#include <avr/io.h>
#include <util/delay.h> 
int x; 
#define t0 while(x<7){x++;PORTD = PORTD | _BV(PD0);_delay_ms(200);PORTD &= ~_BV(PD0);_delay_ms(200);}
#define t1 while(x<7){x++;PORTD = PORTD | _BV(PD0);_delay_ms(400);PORTD &= ~_BV(PD0);_delay_ms(400);}
#define clock 

int main(void)
{
	DDRA = 0xff;
	DDRB = 0xff;
	DDRD = 0x03;
	
while(1){	


if (PINC & (1<<PC0))

 (here the code to the clock, time and pulses)

or clock t0


if (PINC & (1<<PC1))

  ( here other code with new time and same pulses)

or clock t1

if (PINC &(1<<PC2))

PORTA = 0x01;
  (here the code already with the configurations above)
 or clock

PORTA = 0x04;
   (here the same)
  or clock

PORTA = 0x08;
  or clock
 
  (here the same)...
  
  Follows the rest....

}

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are using defines like function definitions? This is C. Why are you not using proper functions? If this is because you want the sequence inlined not CALLd then tell the C compiler that:

static inline void t0(void) {
   while(x<7){
      x++;
      PORTD = PORTD | _BV(PD0);
      _delay_ms(200);
      PORTD &= ~_BV(PD0);
      _delay_ms(200);
   }
}

You may have spotted in this that 'x' is never reset. So once it has counted to 7 it will stay there. I imagine you really want something like:

static inline void t0(void) {
   int x = 0;

   while(x<7){
      x++;
      PORTD = PORTD | _BV(PD0);
      _delay_ms(200);
      PORTD &= ~_BV(PD0);
      _delay_ms(200);
   }
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone, I wanted to thank everyone for their interest in trying to help me, and say as I solved the problem, first I created a clock of the atmega8535 with many delay value, and afther I put this code in atmega32.. 

ldi hi = 0x01
ldi lw = 0x00

A0:
sbic PIND, PD7
rjmp A0
out PORTD, hi
nop
out PORTD, lw

A1:
sbis PIND, PD7
rjmp A1
out PORTD, hi
nop
out PORTD, lw

A2:
sbic PIND, PD7
rjmp A2
out PORTD, hi
nop
out PORTD, lw

A3:
sbis PIND, PD7
rjmp A3
out PORTD, hi
nop
out PORTD, lw

A4:
sbic PIND, PD7
rjmp A4
out PORTD, hi
nop
out PORTD, lw

and work, logically I rewrote all code in asm, so no matter what frequency the PD7 receives, it will always count 5 times, which requires the stepper motor to always stop in the programmed place, well thanks again,  and I just thought I should return the posting so it would not be in a vacuum ...

Last Edited: Sat. Jul 1, 2017 - 10:02 PM