Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 3033|回復: 5
打印 上一主題 下一主題

請問此程式哪裏不正確?

[複製鏈接]
跳轉到指定樓層
1#
發表於 2011-9-7 00:19:33 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
以下程式是摘自 http://www.andrewfrueh.com/electronics/?pageContent=fade_LED_with_button.htm
請問為何它Verify有錯誤?

/*

This script uses a button to fade an LED -- with an easing equation for smoothness.

This script allows you to use a button to turn an LED on and off (with fading).
We use digitalRead() to get the value from the button.
We use analogWrite() to set the brightness of the LED.
We use an easing equation (thanks to Robert Penner) to make the fading smooth.
We use a timer to allow us to have a specific clock speed (see note below).

The circuit:
* LED attached from digital pin 9 to ground.
* Button with one leg connected to digital pin 8 and ground through a 10k resistor, while the other leg is connected to source voltage.


created 2010
by Andrew Frueh

I built this code starting with "Fading" from Arduino examples - 1 Nov 2008, By David A. Mellis
http://arduino.cc/en/Tutorial/Fading
  
*/

// You can change the settings here
// >>
// fadeTimerFreq is the clock speed in milliseconds, lower numbers are faster
const int fadeTimerFreq = 30;
// fadeTime is the total time it will take to complete the ease (in milliseconds)
const int fadeTime = 3000;
// <<

// additional variable for the timer
int currentTime, fadeTimerLast;

// these constant variables store the pin numbers
const int ledPin = 9;
const int buttonPin = 8;
const int fadeRange = 254;


// the amount to step the fade; must be between 1 and the fadeRange
const float fadeStep = (float(fadeTimerFreq) / (fadeTime)) * fadeRange;

int buttonValue, fadeTarget, fadeValueTweened;
float fadeValue;

void setup()  {
  // initialize the serial port; needed for debugging below
  Serial.begin(9600);
  // initialize the LED pin
  pinMode(ledPin, OUTPUT);
  // initialize the input pin
  pinMode(buttonPin, INPUT);
}

void loop()  {

  // for all timers
  currentTime = millis();

  // checks to see if the number of milliseconds has passed
  if ( abs(currentTime - fadeTimerLast) >= fadeTimerFreq) {
    fadeTimerLast = currentTime;
   
    // read the value from the input
    buttonValue = digitalRead(buttonPin);
    // step the fading
    if(buttonValue == 1){
      // if the button is pressed, increase the fade
      fadeValue = fadeValue + fadeStep;
    }
    else{
      // if the button is not pressed, decrease the fade
      fadeValue = fadeValue - fadeStep;
    }
    // constrain the fadeValue so it can't go off toward infinity
    fadeValue = constrain(fadeValue, 0, fadeRange);

    // get the tweened value -- i.e. the smooth value
    fadeValueTweened = Quad_easeInOut(fadeValue, 0, fadeRange);
    // use the tweened value to set the brightness of the LED
    analogWrite(ledPin, fadeValueTweened);
    // print the values to the serial port for debugging
    Serial.print(buttonValue);
    Serial.print(", ");
    Serial.println(fadeValue);
  }
}


// Quad easing thanks to Robert Penner
// variables used are type "float" so that you can throw smaller numbers at it and it will still work well
float Quad_easeInOut(float t, float fixedScaleStart, float fixedScaleEnd){
  // float b = 0, c = 1, d = 1;
  float b = fixedScaleStart;
  float c = fixedScaleEnd - fixedScaleStart;
  float d = fixedScaleEnd;
  if ((t/=d/2) < 1) return c/2*t*t + b;
  return -c/2 * ((--t)*(t-2) - 1) + b;
}

2#
發表於 2011-9-7 10:45:57 | 只看該作者
不會啊
貼到 Arduino 後 Verify 過關了啊 (我用的是 Arduino 0022)
你遇到的 Verify 錯誤訊息是什麼?
3#
 樓主| 發表於 2011-9-7 23:07:42 | 只看該作者
不會啊
貼到 Arduino 後 Verify 過關了啊 (我用的是 Arduino 0022)
你遇到的 Verify 錯誤訊息是什麼?
coopermaa 發表於 2011-9-7 10:45


感謝你的回覆,
我也可以正常Verify了.
4#
發表於 2011-9-8 11:06:07 | 只看該作者
Mm ?  
所以是莫名不能 Verify 後來就又正常了嗎?

Arduino 的編譯如果發生錯誤,視窗下方會顯示錯誤訊息
下次遇到的時候,看一下它寫什麼
5#
 樓主| 發表於 2011-9-8 23:55:34 | 只看該作者
本帖最後由 pizg 於 2011-9-8 23:57 編輯

Verify 會有問題, 大部份都是前一個程式正持續把資料送到Serial Moniter, 這個時候若執行Verify動作就很容易出現問題, 嚴重的話會導致編譯器告訴你COM?找不到!
6#
發表於 2011-9-9 11:35:06 | 只看該作者
原來是這樣
在 upload 的時候,我都很乖,很耐心的等他傳完,難怪沒有遇到這個問題
反倒是曾遇過另一個問題,就是在 verify 的時候,按了一下存檔,結果 verufy 程序會失敗
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-6-14 11:05 , Processed in 0.372365 second(s), 8 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表