Robofun 機器人論壇

標題: Sensor 傳回比較的問題? [打印本頁]

作者: havy    時間: 2014-11-21 12:14
標題: Sensor 傳回比較的問題?
本帖最後由 havy 於 2014-11-21 12:17 編輯

Sensor 傳回比較的問題?

目的:由電壓及電流讀取值;然後換算為功率(P=I*V),程序碼如下~

問題:sensor 回傳的值沒問題;換算丟入P0&Pn 值沒問題<==serial monitor 觀查皆正確。

             執行上卻沒有辦法得到if 判斷條件(因為P0&Pn改變皆是同時的)後執行PWM+-結果.請教各位大神,幫忙檢視程式碼那裡寫錯了謝謝

========Code================================

float P0 = 0;

float Pn = 0;

int pwm=9;           // pwm output to mosfet

void setup()

{

TCCR1B = TCCR1B & 0b11111000 | 0x01; // setting prescaar for31.372KHz pwm

Serial.begin(9600);  // Openserial communications and wait for port to open:

pinMode(pwm,OUTPUT);

}

void loop()

{

int P[5],i,d;

float pwm_d=0;

intsensor1 =analogRead(0);

intsensor2 =analogRead(1);

float Solar_volt = sensor1*25/1024;

float Solar_curr = 0.0488281*sensor2-25;

analogWrite(pwm,127);  //set 50%Duty ration for MOSFET switch

Serial.print("Duty ratio is 50%");

P0= Solar_volt*Solar_curr; //read P0

for(i=0;i<5;i++)   

  {

   P=P0;

   if (P!=P0)

    {

     Pn=P;

     break;

    }

  }

while(P0<Pn)

  {

   for(d=127;d<=179;d++)

   analogWrite(pwm,d);

   delay(100);

   if(P0 == Pn)

    {

     analogWrite(pwm,d);

     pwm_d=d/256*100;

     Serial.print("Duty ratio is=/n");

     Serial.println(pwm_d);

     Serial.print(P0);

     Serial.print(Pn);

     break;

    }

  }

while(P0>Pn)

  {

   for(d=179;d<=0;d++)

   analogWrite(pwm,d);

   delay(100);

   if(P0 == Pn)

    {

     analogWrite(pwm,d);

     pwm_d=d/256*100;

     Serial.print("Duty ratio is=/n");

     Serial.println(pwm_d);

     Serial.print(P0);

     Serial.print(Pn);

     break;

    }

  }

delay(1000);

}








歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/) Powered by Discuz! X3.2