簡單arduino code幫忙解釋

供程式設計討論與經驗分享。例如: C / C++ / Java / C# / Delphi / Visual Basic...

版主: maa

分享到: Facebook

簡單arduino code幫忙解釋

文章ed1024 » 週四 11月 23, 2017 4:21 am

這是一個手電筒的 arduino code 很想做來實驗,但我不懂程式看不懂 認識的人也沒人會這個希望這有高手能幫我解釋程式碼的內容 ,
無限感激麻煩大家 謝謝
可以寄這給我 eddieli1024@gmail.com thanks

代碼: 選擇全部
#include <util/delay.h>
int value;
int redpin1 = 5, redpin2 = 6;
int greenpin1 = 3, greenpin2 = 11;
int bluepin1 = 9, bluepin2 = 10;
int ledmax;
#define GLITTER 0
#define SWIRL 1
#define DAZZLE 2
volatile int mode = DAZZLE;
// we use a button on pin 2 (interrupt pin) to detect mode changes
void modechange(void)
{
   // debounce it
   if (digitalRead(2) == LOW) {
      _delay_ms(10);
      if (digitalRead(2) != LOW)
      return;
      Serial.println("button");
      mode++;
      if (mode > 2)
      mode = 0;
      Serial.print("new mode! ");
      Serial.println(mode, DEC);
   }
}
void setup()
{
   pinMode(2, INPUT);
   digitalWrite(2, HIGH); // pullup on mode button
   attachInterrupt(0, modechange, CHANGE);
   Serial.begin(9600);
   randomSeed(analogRead(0));
   // nothing for setup
   analogWrite(redpin1, 0);
   analogWrite(redpin2, 0);
   analogWrite(greenpin1, 0);
   analogWrite(greenpin2, 0);
   analogWrite(bluepin1, 0);
   analogWrite(bluepin2, 0);
   ledmax = 250; // change this value to adjust the maximum brightness
}
void loop()
{
   switch(mode) {
   case SWIRL:
      //Serial.println("swirl");
      ckswirl(ledmax, 10);
      break;
   case GLITTER:
      //Serial.println("glimmer");
      glimmertest(ledmax, ledmax/10, 30);
      break;
   case DAZZLE:
      //Serial.println("dazzle");
      bedazzle(ledmax, 10, 7, 11);
      break;
   }
}
void bedazzle(int ledmax, int pulselensec, int freqmin, int freqmax) {
   long pulses;
   analogWrite(redpin1, 0);
   analogWrite(redpin2, 0);
   analogWrite(greenpin1, 0);
   analogWrite(greenpin2, 0);
   analogWrite(bluepin1, 0);
   analogWrite(bluepin2, 0);
   // note we dont use red LEDs in this
   int freq = random(freqmin, freqmax+1);
   int pulsedelay = 1000/freq;
   pulsedelay /= 2;
   pulses = pulselensec;
   pulses *= 1000;
   pulses /= 2*pulsedelay;
   /*
Serial.print("pulsing at ");
Serial.print(freq, DEC);
Serial.print(" Hz (");
Serial.print(pulsedelay, DEC);
Serial.println(" ms on/off)");
Serial.print(pulses);
Serial.println(" pulses");
*/
   while (pulses--) {
      analogWrite(greenpin1, ledmax);
      analogWrite(greenpin2, ledmax);
      analogWrite(bluepin1, ledmax);
      analogWrite(bluepin2, ledmax);
      _delay_ms(pulsedelay);
      analogWrite(greenpin1, 0);
      analogWrite(greenpin2, 0);
      analogWrite(bluepin1, 0);
      analogWrite(bluepin2, 0);
      _delay_ms(pulsedelay);
      if (mode != DAZZLE) return;
   }
}
void ckswirl(int ledmax, uint8_t z) {
   int r, g, b;
   // fade from red to orange to yellow to green
   for (g=0; g<255; g++) { // turn red down
      analogWrite(redpin1, ledmax-g);
      analogWrite(redpin2, ledmax-g);
      analogWrite(greenpin1, g); // sets the value (range from 0 to 255)
      analogWrite(greenpin2, g); // sets the value (range from 0 to 255)
      delay(z);
      if (mode != SWIRL) return;
   }
   // fade from green to blue
   for (b=0; b<255; b++) { // turn red down
      analogWrite(bluepin1, b);
      analogWrite(bluepin2, b);
      analogWrite(greenpin1, ledmax-b); // sets the value (range from 0 to 255)
      analogWrite(greenpin2, ledmax-b); // sets the value (range from 0 to 255)
      delay(z);
      if (mode != SWIRL) return;
   }
   // from blue to red
   for (r=0; r<255; r++) { // turn red down
      analogWrite(redpin1, r);
      analogWrite(redpin2, r);
      analogWrite(bluepin1, ledmax-r); // sets the value (range from 0 to 255)
      analogWrite(bluepin2, ledmax-r); // sets the value (range from 0 to 255)
      delay(z);
      if (mode != SWIRL) return;
   }
}
void glimmertest(int maxvalue, int incr, int z) {
   for(value = 0 ; value <= maxvalue; value+=incr)
   {
      analogWrite(greenpin1, value); // sets the value (range from 0 to 255)
      analogWrite(greenpin2, maxvalue-value); // sets the value (range from 0 to 255)
      analogWrite(bluepin1, value);
      analogWrite(bluepin2, maxvalue-value); // sets the value (range from 0 to 255)
      analogWrite(redpin1, value);
      analogWrite(redpin2, maxvalue-value); // sets the value (range from 0 to 255)
      delay(z); // waits for 30 milli seconds to see the dimming effect
      if (mode != GLITTER) return;
   }
   for(value = maxvalue; value >=0; value-=incr) // fade out (from max to min)
   {
      analogWrite(greenpin1, value);
      analogWrite(greenpin2 , maxvalue-value); // sets the value (range from 0 to 255)
      analogWrite(bluepin1, value);
      analogWrite(bluepin2, maxvalue-value); // sets the value (range from 0 to 255)
      analogWrite(redpin1, value);
      analogWrite(redpin2, maxvalue-value); // sets the value (range from 0 to 255)
      delay(z);
      if (mode != GLITTER) return;
   }
}

再次感謝
ed1024
剛學走路的小朋友
 
文章: 3
註冊時間: 週六 4月 11, 2015 1:51 am

Re: 簡單arduino code幫忙解釋

文章ed1024 » 週五 11月 24, 2017 2:45 pm

還是有誰知道那可以找到人幫忙解讀,要付費的也可以
Thanks
ed1024
剛學走路的小朋友
 
文章: 3
註冊時間: 週六 4月 11, 2015 1:51 am


回到 程式設計討論版

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客

cron