Помогите нубу написать скетч

tralo2012
Offline
Зарегистрирован: 23.04.2018

Есть кнопка и 2 светлодиода. По нажатию кнопки плавно розгораеся 1 светодиод, а второй разгорается в 2 раза быстрее, и продожают гореть. Когда отпускаешь кнопку плавно тухнут. 

Зарание Спасибо))

andycat
andycat аватар
Offline
Зарегистрирован: 07.09.2017

Классная идея :) - подсветка низа мебели в коридоре при движении, жду датчики как придут,

разбирайтесь на здоровье (код для attiny85), логика именно как вам и нужна:

#include <avr/io.h>
#include <util/delay.h>

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

// PB0 pin 5 - first led wave
#define PB0_OUT sbi(DDRB,PB0)
#define PB0_IN cbi(DDRB,PB0)
#define PB0_HIGH sbi(PORTB,PB0)
#define PB0_LOW cbi(PORTB,PB0)
// PB1 pin 6 - second led wave
#define PB1_OUT sbi(DDRB,PB1)
#define PB1_IN cbi(DDRB,PB1)
#define PB1_HIGH sbi(PORTB,PB1)
#define PB1_LOW cbi(PORTB,PB1)
// PB2 pin 7 - red led
#define PB2_OUT sbi(DDRB,PB2)
#define PB2_IN cbi(DDRB,PB2)
#define PB2_HIGH sbi(PORTB,PB2)
#define PB2_LOW cbi(PORTB,PB2)
// PB3 pin 2 - PIR device
#define PB3_OUT sbi(DDRB,PB3)
#define PB3_IN cbi(DDRB,PB3)
#define PB3_HIGH sbi(PORTB,PB3)
#define PB3_LOW cbi(PORTB,PB3)
#define PB3_READ bitRead(PINB,PB3)
// PB4 pin 3 - not use
#define PB4_OUT sbi(DDRB,PB4)
#define PB4_IN cbi(DDRB,PB4)
#define PB4_HIGH sbi(PORTB,PB4)
#define PB4_LOW cbi(PORTB,PB4)

#define WORK_LEVEL_LED 0
#define WORK_LEVEL_KEY 0

#if (WORK_LEVEL_LED == 0)
#define LED1ON PB0_LOW
#define LED2ON PB1_LOW
#define LED1OFF PB0_HIGH
#define LED2OFF PB1_HIGH
#define BR1LED OCR0A=255-bright_heart_led
#define BR2LED OCR0B=255-bright_heart_led
#else
#define LED1ON PB0_HIGH
#define LED2ON PB1_HIGH
#define LED1OFF PB0_LOW
#define LED2OFF PB1_LOW
#define BR1LED OCR0A=bright_heart_led
#define BR2LED OCR0B=bright_heart_led
#endif

#if (WORK_LEVEL_KEY == 0)
#define KEYINIT PB3_HIGH
#define KEYTEST PB3_READ==0
#else
#define KEYINIT PB3_LOW
#define KEYTEST PB3_READ==1
#endif

byte deviceMode = 0; // 0-off, 1 minimal light 2 full light
byte bright_heart_led;
boolean flag_people;
unsigned long timer_Led;
unsigned long timer_People;

void setup() {
  // put your setup code here, to run once:
  // init pins
  PB0_OUT; LED1OFF;
  PB1_OUT; LED2OFF;
  PB2_OUT; PB2_HIGH; // off test red led
  PB3_IN; KEYINIT;
  PB4_OUT; PB4_LOW; // off unusedpin
  // hard pwm
  cbi(TCCR0B, CS02); sbi(TCCR0B, CS01); cbi(TCCR0B, CS00); // N=8
  sbi(TCCR0A, WGM01); sbi(TCCR0A, WGM01); // pwm mode
  // if need 9600 bod = count clock/N/9600-1  = 8000000/8/9600-1 = 103
  // real clock pwm = clock/(n*256) = 8000000/(8*256) = 3906 Hz
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long current_millis = millis();
  switch (deviceMode) {
    case 1: {
        if ((current_millis - timer_Led) >= 7) { // 2000ms/255 bright level
          timer_Led = current_millis;
          if ((++bright_heart_led) >= 240) {
            if (flag_people) {
              pwm1off();
              LED1ON;
              deviceMode = 3;
              timer_People = current_millis;
              bright_heart_led = 0;
              pwm2on();
              flag_people = false;
            } else {
              deviceMode = 2;
              bright_heart_led = 255;
            }
          } else {
            BR1LED; // pwm level PB0
          }
        }
        break;
      }
    case 2: {
        if ((current_millis - timer_Led) >= 7) { // 2000ms/255 bright level
          timer_Led = current_millis;
          if ((--bright_heart_led) <= 15) {
            pwm1off();
            LED1OFF;
            deviceMode = 0;
          } else {
            BR1LED; // pwm level PB0
          }
        }
        break;
      }
    case 3: {
        if ((current_millis - timer_Led) >= 11) { // 3000ms/255 bright level
          timer_Led = current_millis;
          if ((++bright_heart_led) >= 240) {
            pwm2off();
            LED2ON;
            timer_People = current_millis;
            flag_people = false;
            deviceMode = 4;
          } else {
            BR2LED; // pwm level PB1
          }
        }
        break;
      }
    case 4: {
        if ((current_millis - timer_Led) >= 10000) { // 10 sec max light
          timer_Led = current_millis;
          if (flag_people) {
            timer_People = current_millis;
            flag_people = false;
            PB2_LOW;
          } else {
            bright_heart_led = 255;
            pwm1on();
            pwm2on();
            deviceMode = 5;
          }
        }
        break;
      }
    case 5: {
        if ((current_millis - timer_Led) >= 7) { // 2000ms/255 bright level
          timer_Led = current_millis;
          if ((--bright_heart_led) <= 10) {
            pwm1off();
            LED1OFF;
            pwm2off();
            LED2OFF;
            deviceMode = 0;
            PB2_HIGH;
          } else {
            BR1LED; // pwm level PB0
            BR2LED; // pwm level PB1
          }
        }
        break;
      }
    default: { // mode 0
        if (KEYTEST) {
          deviceMode = 1;
          timer_Led = current_millis;
          timer_People = current_millis;
          bright_heart_led = 0;
          pwm1on();
          flag_people = false;
        }
      }
  }
  if ((current_millis - timer_People) >= 700) { // every 0.7 sec test pir device
    timer_People = current_millis;
    if (KEYTEST) {
      flag_people = true;
    }
  }
}

void pwm1on() {
  sbi(TCCR0A, COM0A1); cbi(TCCR0A, COM0A0); // pwm mode PB0
  //sbi(TCCR0A, WGM01); sbi(TCCR0A, WGM01); // pwm mode PB0
  BR1LED; // pwm level PB0
}

void pwm2on() {
  sbi(TCCR0A, COM0B1); cbi(TCCR0A, COM0B0); // pwm mode PB1
  //sbi(TCCR0A, WGM01); sbi(TCCR0B, WGM01); // pwm mode PB1
  BR2LED; // pwm level PB1
}

void pwm1off() {
  cbi(TCCR0A, COM0A1); cbi(TCCR0A, COM0A0); // no pwm mode PB0
  //sbi(TCCR0A, WGM01); cbi(TCCR0A, WGM01); // no pwm mode PB0
}

void pwm2off() {
  cbi(TCCR0A, COM0B1); cbi(TCCR0A, COM0B0); // no pwm mode PB1
  //sbi(TCCR0A, WGM01); cbi(TCCR0B, WGM01); // no pwm mode PB1
}

 

ЕвгенийП
ЕвгенийП аватар
Offline
Зарегистрирован: 25.05.2015

tralo2012 пишет:

2 светлодиода. 

Шикарная очепятка!!! Предлагаю осветительные светодиоды так и называть! :)))))

tralo2012 пишет:

Когда отпускаешь кнопку плавно тухнут. 

А если совсем протухнут и вонять начнут, тогда чего делать? Вытяжной вентиллятор-то в схеме предусмотрен?

 

Arhat109-2
Offline
Зарегистрирован: 24.09.2015

Нубу для начала надо усвоить что темы "помогите" публикуются в разделе "ищу исполнителя". В посте можно указывать "поможите задаром..". (*тут оно нафиг?!?*)

Green
Offline
Зарегистрирован: 01.10.2015

Нет проблем. Пиво?

Alixan
Offline
Зарегистрирован: 18.06.2018

Убил

ЕвгенийП
ЕвгенийП аватар
Offline
Зарегистрирован: 25.05.2015

Кого?

Alixan
Offline
Зарегистрирован: 18.06.2018

Ответом)