added button & different led configurations
This commit is contained in:
53
src/main.cpp
53
src/main.cpp
@@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
static uint8_t led_duration = 8;
|
static uint8_t led_duration = 8;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Function: configureTimer
|
* Function: configureTimer
|
||||||
* ----------------------------
|
* ----------------------------
|
||||||
* Configures both 16bit timer for 25 kHz pwm signals.
|
*
|
||||||
*/
|
*/
|
||||||
void configureTimer()
|
void configureTimer()
|
||||||
{
|
{
|
||||||
@@ -30,7 +31,7 @@ void configureTimer()
|
|||||||
// non-inverted PWM on ch. A, B and C, mode 10: ph. correct PWM, TOP = ICR1
|
// non-inverted PWM on ch. A, B and C, mode 10: ph. correct PWM, TOP = ICR1
|
||||||
// TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1C1) | _BV(WGM11);
|
// TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1C1) | _BV(WGM11);
|
||||||
|
|
||||||
ICR1 = 640; // TOP = 320, @16Mhz CPU -> 25kHz PWM
|
ICR1 = 320; // TOP = 320, @16Mhz CPU -> 25kHz PWM
|
||||||
OCR1A = 500; // Set duty-cycle
|
OCR1A = 500; // Set duty-cycle
|
||||||
TCCR1B = _BV(WGM13) | _BV(CS10); // prescaler = 1
|
TCCR1B = _BV(WGM13) | _BV(CS10); // prescaler = 1
|
||||||
// Configure Timer 3
|
// Configure Timer 3
|
||||||
@@ -56,6 +57,35 @@ void configureTimer()
|
|||||||
// TCCR1A |= _BV(WGM11); //WGM13:WGM10 set 1010
|
// TCCR1A |= _BV(WGM11); //WGM13:WGM10 set 1010
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t value_switcher = 0;
|
||||||
|
|
||||||
|
float led_a_multiplier = 3;
|
||||||
|
float led_b_multiplier = led_a_multiplier + led_a_multiplier * 0.02;
|
||||||
|
float led_c_multiplier = led_a_multiplier - led_a_multiplier * 0.02;
|
||||||
|
|
||||||
|
void changeValue (uint8_t value) {
|
||||||
|
switch (value)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
led_a_multiplier = 3;
|
||||||
|
led_b_multiplier = 6;
|
||||||
|
led_c_multiplier = 9;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
led_a_multiplier = 6;
|
||||||
|
led_b_multiplier = 3;
|
||||||
|
led_c_multiplier = 6;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
led_a_multiplier = 9;
|
||||||
|
led_b_multiplier = 9;
|
||||||
|
led_c_multiplier = 3;
|
||||||
|
value_switcher = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Function: setup
|
* Function: setup
|
||||||
@@ -74,6 +104,7 @@ void setup()
|
|||||||
pinMode(4, OUTPUT);
|
pinMode(4, OUTPUT);
|
||||||
pinMode(9, OUTPUT);
|
pinMode(9, OUTPUT);
|
||||||
pinMode(7, INPUT);
|
pinMode(7, INPUT);
|
||||||
|
pinMode(12, INPUT);
|
||||||
|
|
||||||
// configure timer
|
// configure timer
|
||||||
Serial.println("configure timer for 25kHz PWM...");
|
Serial.println("configure timer for 25kHz PWM...");
|
||||||
@@ -98,10 +129,21 @@ static uint16_t led_b_counter = led_b_period;
|
|||||||
static uint16_t led_c_period = (uint16_t)-1;
|
static uint16_t led_c_period = (uint16_t)-1;
|
||||||
static uint16_t led_c_counter = led_c_period;
|
static uint16_t led_c_counter = led_c_period;
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t button_last_state = HIGH;
|
||||||
|
|
||||||
ISR(TIMER0_COMPA_vect){
|
ISR(TIMER0_COMPA_vect){
|
||||||
iterator++;
|
iterator++;
|
||||||
|
|
||||||
uint8_t tacho_current = digitalRead(7);
|
uint8_t tacho_current = digitalRead(7);
|
||||||
|
uint8_t button_current = digitalRead(12);
|
||||||
|
|
||||||
|
if(button_last_state == HIGH && button_current == LOW){
|
||||||
|
changeValue(value_switcher++);
|
||||||
|
}
|
||||||
|
|
||||||
|
button_last_state = button_current;
|
||||||
|
|
||||||
|
|
||||||
/* Get number of loop iterations per fan revolution */
|
/* Get number of loop iterations per fan revolution */
|
||||||
if (!tacho_last && tacho_current) {
|
if (!tacho_last && tacho_current) {
|
||||||
@@ -110,9 +152,9 @@ ISR(TIMER0_COMPA_vect){
|
|||||||
} else {
|
} else {
|
||||||
tacho_counter = 0;
|
tacho_counter = 0;
|
||||||
period = iterator;
|
period = iterator;
|
||||||
led_a_period = period / 3;
|
led_a_period = period / led_a_multiplier;
|
||||||
led_b_period = period * 20 / 61;
|
led_b_period = period / led_b_multiplier;
|
||||||
led_c_period = period * 20 / 59;
|
led_c_period = period / led_c_multiplier;
|
||||||
iterator = 0;
|
iterator = 0;
|
||||||
rotations++;
|
rotations++;
|
||||||
}
|
}
|
||||||
@@ -155,4 +197,5 @@ ISR(TIMER0_COMPA_vect){
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user