정보나눔

오픈소스하드웨어 프로젝트에 대한 다양한 정보를 나누는 공간입니다.

가능한 메모리 부족으로 실행을 못시키고 있습니다 부탁드려요
김남훈 | 2017-06-12

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <UnoWiFiDevEd.h>
#define CONNECTOR     "rest"
#define SERVER_ADDR   "api.thingspeak.com"
#define APIKEY_THINGSPEAK  "9T2M09ZUXUIPFNXG" //Insert your API Key
#define Temp 2
#define VOLTAGE 5.00    //system voltage
#define OFFSET 0        //zero drift voltage
#define Valve 3         //operating instruction
#define SensorPin 0          //pH meter Analog output to Arduino Analog Input 0
unsigned long int avgValue;  //Store the average value of the sensor feedback
float b;
int buf[10],temp;

OneWire oneWire(Temp);
DallasTemperature sensors(&oneWire);

double orpValue;

#define ArrayLenth  40    //times of collection
#define orpPin 1          //orp meter output,connect to Arduino controller ADC pin

int orpArray[ArrayLenth];
int orpArrayIndex=0;

double avergearray(int* arr, int number)
{
  int i;
  int max,min;
  double avg;
  long amount=0;
  if(number<=0){
    printf("Error number for the array to avraging!/n");
    return 0;
  }
  if(number<5){   //less than 5, calculated directly statistics
    for(i=0;i<number;i++){
      amount+=arr[i];
    }
    avg = amount/number;
    return avg;
  }else{
    if(arr[0]<arr[1]){
      min = arr[0];max=arr[1];
    }
    else{
      min=arr[1];max=arr[0];
    }
    for(i=2;i<number;i++){
      if(arr[i]<min){
        amount+=min;        //arr<min
        min=arr[i];
      }else {
        if(arr[i]>max){
          amount+=max;    //arr>max
          max=arr[i];
        }else{
          amount+=arr[i]; //min<=arr<=max
        }
      }//if
    }//for
    avg = (double)amount/(number-2);
  }//if
  return avg;
}

void setup()
{
  Serial.begin(9600); 
  Ciao.begin();
}
void loop()
{
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  {
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue;                      //convert the millivolt into pH value
  Serial.print("pH:"); 
  Serial.print(phValue,2);
  Serial.print(" ");
  delay(800);
  static unsigned long orpTimer=millis();   //analog sampling interval
  static unsigned long printTime=millis();
  if(millis() >= orpTimer)
  {
    orpTimer=millis()+20;
    orpArray[orpArrayIndex++]=analogRead(orpPin);    //read an analog value every 20ms
    if (orpArrayIndex==ArrayLenth) {
      orpArrayIndex=0;
    }  
    orpValue=((30*(double)VOLTAGE*1000)-(75*avergearray(orpArray, ArrayLenth)*VOLTAGE*1000/1024))/75-OFFSET;   //convert the analog value to orp according the circuit
  }
  if(millis() >= printTime)   //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
  {
  printTime=millis()+800;
  Serial.print("ORP: ");
  Serial.print((int)orpValue);
        Serial.println("mV");
 }
  sensors.requestTemperatures();
  Serial.print(" ");
  Serial.print(sensors.getTempCByIndex(0));

   String uri = "/update?api_key=";
    uri += APIKEY_THINGSPEAK;
    uri += "&field1=";
    uri += String(sensors.getTempCByIndex(0));
    uri += "&field2=";
   uri += String(phValue);
        uri += "&field3=";
         uri += String(orpValue);

 Ciao.println("Send data on ThingSpeak Channel");
     
    CiaoData data = Ciao.write(CONNECTOR, SERVER_ADDR, uri);
 
    if (!data.isEmpty()){
      Ciao.println( "State: " + String (data.get(1)) );
      Ciao.println( "Response: " + String (data.get(2)) );
    }
    else{
      Ciao.println("Write Error");
    }   
 
  delay(30000); // Thinkspeak policy


  if( 200 <= orpValue <=400)
  {
    digitalWrite(Valve, LOW);
  }
  else
  {
    if( 6.5 <= phValue <= 7.7)
    {
      digitalWrite(Valve, LOW);
    }
    else
    {
      digitalWrite(Valve, HIGH);
    }
  }
}

이런 코드를 짜서 작품을 만드는데 사용 가능한 메모리 부족, 안정성에 문제가 생길 수 있습니다. 라고 계속 뜹니다. ㅜㅜ 정말로 이부분 해결을 해야되는데 제발 도와주시면 감사하겠습니다.

이전글   |    Klant 님의 아두이노 한글시계 질문있습니다... 2017-06-11
다음글   |    아두이노 모터코드 좀 도와주세요 ㅠㅠ 2017-06-12