ein-tasten-dimmer

OP #2363075
Lesenswert?

Hallo Zusammen

habe versucht ein ein-tasten-dimmer zu bauen.
Code:

 int main(void)
 {
uint8_t direction = 1;
   uint8_t pwm=0;
    PORTB = 0xff;
    DDRB = (1<<PB1);
  DDRC = 0x00;
  PORTC = 0x00;

    TCCR1A = (1<<WGM11)  | (1<<COM1A1);
    TCCR1B = (1<<WGM13)  |(1<<WGM12)  |(0<<CS11) | (1<<CS10);
    ICR1 = 0xFFFF;
    OCR1A = 0x00;
while(1)
 {


   if ((!(PINC & (1<<PINC0)) && (direction==1)))
     {
             if(++pwm==255)
             direction=0;


          }
   else if ((!(PINC & (1<<PINC0)) && (direction==0)))
        {

             if(--pwm==0)
             direction=1;


          }
               OCR1A =  pwm;
               _delay_ms(10);

    }

return 0;
}


Programm läuft aber nicht, sowie ich wollte.
PWM muss immer bis 255 hochgezählt oder bis 0 runtergezählt,erst danach 
tut die direction toggeln.

Wie muss man den Code ändern, damit die direction nach jedem Dimmgang 
toggelt?

Danke für eure Hilfe!
#2363186
Lesenswert?

probier das mal
1
int main(void)
2
{
3
  ...
4

5
  uint8_t pressed = 0;
6
    
7
  while(1)
8
  {
9
    if( !(PINC & (1<<PINC0) ) )
10
    {
11
      pressed = 1;
12
      if( direction )
13
      {
14
        if( pwm < 255 )
15
          ++pwm;
16
      }
17
      else
18
      {
19
        if( pwm > 0 )
20
          --pwm;
21
      }
22

23
      OCR1A =  pwm;
24
      _delay_ms( 10 );
25
    }
26

27
    else if( pressed )
28
    {
29
      pressed = 0;
30
      direction = 1 - direction;
31
    }
32
  }

Taster gedrückt halten -> dimmen

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren