當(dāng)前位置:首頁(yè) > 芯聞號(hào) > 電機(jī)控制電路
[導(dǎo)讀]ATtiny 電子蠟燭最難的部分就閃爍神態(tài)逼真,所以皮特做了一個(gè)蠟燭光檢測(cè)電阻( LDR )和固定電阻作為一個(gè)分壓器。這是作為ATTINY85 ADC之中的一個(gè)輸入端,并離散時(shí)間間隔的進(jìn)行采樣。采樣速率為100毫秒。然后將采集的8bit的電頻值存儲(chǔ)到EEPROM中,以便記錄蠟燭的閃爍圖譜,驅(qū)動(dòng)將其連接的LED、PWM形成通路。在用三節(jié)干電池供電。最后您只需編程程序,然后通過(guò)開(kāi)關(guān)進(jìn)行控制。

想想當(dāng)你好不容易跟女朋友共度燭光晚餐,卻因?yàn)橄灎T點(diǎn)沒(méi)了或打翻著火了,那是一件多么坑爹的事啊!今天為你分享一款自己diy的超自然的燭光蠟燭。

 

WP_000356.jpg

ATtiny 電子蠟燭,皮特•米爾斯開(kāi)發(fā)這個(gè)偉大的蠟燭,正如我們圖片所見(jiàn)到的一樣,但怎樣讓這蠟燭的光芒像傳統(tǒng)的蠟燭一樣閃爍呢。

 

WP_000370.jpg

皮特使用一個(gè)高亮的LED和一些模擬的輔助軟件,這樣就使得ATtiny 電子蠟燭的燭光和傳統(tǒng)蠟燭擁有一樣的閃爍的燭光,并且優(yōu)于傳統(tǒng)蠟燭,因?yàn)樗话橛忻骰鸬奈kU(xiǎn)。

 

WP_000376.jpg

ATtiny 電子蠟燭最難的部分就閃爍神態(tài)逼真,所以皮特做了一個(gè)蠟燭光檢測(cè)電阻( LDR )和固定電阻作為一個(gè)分壓器。這是作為ATTINY85 ADC之中的一個(gè)輸入端,并離散時(shí)間間隔的進(jìn)行采樣。采樣速率為100毫秒。然后將采集的8bit的電頻值存儲(chǔ)到EEPROM中,以便記錄蠟燭的閃爍圖譜,驅(qū)動(dòng)將其連接的LED、PWM形成通路。在用三節(jié)干電池供電。最后您只需編程程序,然后通過(guò)開(kāi)關(guān)進(jìn)行控制。

 

WP_000345.jpg

下面是ATtiny 電子蠟燭的電路圖

 

ATTiny Candle Sch.jpg

下面是程序的代碼以及寫(xiě)入EEPROM的數(shù)據(jù)

view plainprint?

/*

Program Description: This program reads a light detecting resistor thru an internal ADC and stores the value,

after scaling it, to eeprom. This ADC value is sent to a PWM channel with attached led. This is essentially a data logger

for light and replay by LED. If, if you aim the LDR at a flickering candle during its recording phase, you have a flickering

led candle.

A circuit description and other details can be found at http://petemills.blogspot.com

Filename: ATTiny_Candle_v1.0.c

Author: Pete Mills

Int. RC Osc. 8 MHz; Start-up time PWRDWN/RESET: 6 CK/14 CK + 64 ms

*/

//********** Includes **********

#include

#include

#include

//********** Definitions **********

// LED for flame simulation

#define LED PB0

#define LED_PORT PORTB

#define LED_DDR DDRB

// Light Detecting Resistor for recording a live flame

#define LDR PINB3

#define LDR_PORT PINB

#define LDR_DDR DDRB

// Tactile Switch Input

#define SW1 PINB4

#define SW1_PORT PINB

#define SW1_DDR DDRB

#define ARRAY_SIZE 500 // size of the flicker array

#define SAMPLE_RATE 100 // ms delay for collecting and reproducing the flicker

//********** Function Prototypes **********

void setup(void);

void toggle_led(void);

void program_flicker(void);

void led_alert(void);

void eeprom_save_array(void);

void eeprom_read_array(void);

void scale_array(void);

uint8_t get_adc(void);

uint8_t scale( uint8_t input, uint8_t inp_low, uint8_t inp_hi, uint8_t outp_low, uint8_t outp_hi);

uint8_t is_input_low(char port, char channel, uint8_t debounce_time, int input_block);

//********** Global Variables **********

uint8_t flicker_array[ ARRAY_SIZE ] = { 0 };

uint8_t EEMEM ee_flicker_array[ ARRAY_SIZE ] = { 0 };

int main(void)

{

uint16_t replay = 0;

setup();

eeprom_read_array();

while(1)

{

if( is_input_low( SW1_PORT, SW1, 25, 250 ) )

{

// program the flicker

// after entering and upon completion, a predetermined flash pattern will occur as described in led_alert()

// aim the ldr at a flickering candle or any other light source ( like a laser ) you want to record during this time

// and upon completion the values are stored to eeprom. They are played back immediately as well

// as being recalled from eeprom upon first start up

led_alert();

program_flicker();

scale_array();

eeprom_save_array();

led_alert();

}

// replay the recorded flicker pattern

OCR0A = flicker_array[ replay ];

++replay;

if( replay >= ( ARRAY_SIZE - 13 ) ) // if the end of the stored array has been reached

{

replay = 0; // start again from the beginning

//led_alert();

}

_delay_ms( SAMPLE_RATE );

_delay_ms( 3 ); // ADC Conversion time

}

}

//********** Functions **********

void setup(void)

{

//********* Port Config *********

LED_DDR |= ( 1 << LED); // set PB0 to "1" for output

LED_PORT &= ~( 1 << LED ); // turn the led off

LDR_DDR &= ~( 1 << LDR ); // set LDR pin to 0 for input

LDR_PORT |= ( 1 << LDR ); // write 1 to enable internal pullup

SW1_DDR &= ~( 1 << SW1 ); // set sw1 pin to 0 for input

SW1_PORT |= ( 1 << SW1 ); // write a 1 to sw1 to enable the internal pullup

//********** PWM Config *********

TCCR0A |= ( ( 1 << COM0A1 ) | ( 1 << WGM01 ) | ( 1 << WGM00 ) ); // non inverting fast pwm

TCCR0B |= ( 1 << CS00 ); // start the timer

//********** ADC Config **********

ADMUX |= ( ( 1 << ADLAR ) | ( 1 << MUX1 ) | ( 1 << MUX0 ) ); // left adjust and select ADC3

ADCSRA |= ( ( 1 << ADEN ) | ( 1 << ADPS2 ) | ( 1 << ADPS1 ) ); // ADC enable and clock divide 8MHz by 64 for 125khz sample rate

DIDR0 |= ( 1 << ADC3D ); // disable digital input on analog input channel to conserve power

}

void toggle_led()

{

LED_PORT ^= ( 1 << LED );

}

uint8_t is_input_low( char port, char channel, uint8_t debounce_time, int input_block )

{

/*

This function is for debouncing a switch input

Debounce time is a blocking interval to wait until the input is tested again.

If the input tests low again, a delay equal to input_block is executed and the function returns ( 1 )

*/

if ( bit_is_clear( port, channel ) )

{

_delay_ms( debounce_time );

if ( bit_is_clear( port, channel ) )

{

_delay_ms( input_block );

return 1;

}

}

return 0;

}

uint8_t get_adc()

{

ADCSRA |= ( 1 << ADSC ); // start the ADC Conversion

while( ADCSRA & ( 1 << ADSC )); // wait for the conversion to be complete

return ~ADCH; // return the inverted 8-bit left adjusted adc val

}

void program_flicker()

{

// build the flicker array

for( int i = 0; i < ARRAY_SIZE; i++ )

{

flicker_array[ i ] = get_adc();

_delay_ms( SAMPLE_RATE );

}

}

void led_alert()

{

// this is a function to create a visual alert that an event has occured within the program

// it toggles the led 10 times.

for( int i = 0; i < 10; i++ )

{

OCR0A = 0;

_delay_ms( 40 );

OCR0A = 255;

_delay_ms( 40 );

}

}

void eeprom_save_array()

{

for( int i = 0; i < ARRAY_SIZE; i++ )

{

eeprom_write_byte( &ee_flicker_array[ i ], flicker_array[ i ] );

}

}

void eeprom_read_array()

{

for( int i = 0; i < ARRAY_SIZE; i++ )

{

flicker_array[ i ] = eeprom_read_byte( &ee_flicker_array[ i ] );

}

}

uint8_t scale( uint8_t input, uint8_t inp_low, uint8_t inp_hi, uint8_t outp_low, uint8_t outp_hi)

{

return ( ( ( input - inp_low ) * ( outp_hi - outp_low ) ) / ( ( inp_hi - inp_low ) + outp_low ) );

}

void scale_array()

{

uint8_t arr_min = 255;

uint8_t arr_max = 0;

uint8_t out_low = 20;

uint8_t out_high = 255;

// find the min and max values

for( int i = 0; i < ARRAY_SIZE; i++ )

{

if( flicker_array[ i ] < arr_min )

arr_min = flicker_array[ i ];

if( flicker_array[ i ] > arr_max )

arr_max = flicker_array[ i ];

}

// now that we know the range, scale it

for( int i = 0; i < ARRAY_SIZE; i++ )

{

flicker_array[ i ] = scale( flicker_array[ i ], arr_min, arr_max, out_low, out_high );

}

} igh );

}

} igh );

}

}

}

}

}

}

}

} }

} }

} }

}

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀(guān)點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車(chē)的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車(chē)技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車(chē)工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車(chē)。 SODA V工具的開(kāi)發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車(chē) 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶(hù)希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開(kāi)幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱(chēng),數(shù)字世界的話(huà)語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱(chēng)"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉