Software voor regenwaternivo.

algemene C code
Berichten: 3
Geregistreerd: 22 Apr 2015, 22:16

Software voor regenwaternivo.

Berichtdoor Evarist » 11 Mei 2015, 13:41

Ik vond op internet een bouwbeschrijving met code om de inhoud van een regenwatervat uit te lezen.
De code heeft echter fouten. Wat zou het kunnen zijn?

Ontwerp: http://www.open-electronics.org/water-t ... h-arduino/

Code:

Code: Alles selecteren
/* -*- mode: c -*- */
/**
 * pozzo.pde
 * version: 1.2
 */
 
#include <LiquidCrystal.h>
 
#define PING_PIN 13
#define BUZZER_PIN 8
#define SWITCH_INT 0 /* 0 => pin 2 */
#define PI 3.1415926535898
#define SUPERFICE_BASE (R_POZZO * R_POZZO * PI)
#define SIZE_BAR (16 * 5)
#define ALARM_ICON 0 /* code */
#define SOUND_ICON 6 /* code */
#define SOUND_ICON_ON 7 /* code */
 
#define R_POZZO 0.5 /* raggio pozzo (m) */
#define H_POZZO 146.0 /* cm */
#define SOGLIA_ALLARME_1 100 /* cm */
#define SOGLIA_ALLARME_2 120 /* cm */
#define DELAY_0 60000 /* ms; 1000 * 60 * 1 = 1 min */
#define DELAY_1 600 /* ms */
#define DELAY_2 200 /* ms */
 
/* initialize the library with the numbers of the interface pins */
LiquidCrystal lcd(12, 11, 5, 4, 3, 6);
 
int mute = 0;
 
byte *getChar(int n, byte newChar[]) {
  int i;
  byte code[5] = {
    B10000,
    B11000,
    B11100,
    B11110,
    B11111};
 
  for (i = 0; i < 8; i++)
    newChar[i] = code[n - 1];
 
  return newChar;
}
 
void setup() {
  int i;
  float h;
  byte newChar[8];
 
  /* set up the LCD's number of rows and columns: */
  lcd.begin(16, 2);
 
  for (i = 1; i < 6; i++)
    lcd.createChar(i, getChar(i, newChar));
 
  newChar = {
    B00000,
    B00100,
    B01010,
    B01010,
    B11111,
    B00100,
    B00000,
  };
 
  lcd.createChar(ALARM_ICON, newChar);
 
  newChar = {
    B00011,
    B00101,
    B11001,
    B11001,
    B11001,
    B00101,
    B00011,
  };
 
  lcd.createChar(SOUND_ICON, newChar);
 
  newChar = {
    B00100,
    B10010,
    B01001,
    B01001,
    B01001,
    B10010,
    B00100,
  };
 
  lcd.createChar(SOUND_ICON_ON, newChar); 
 
  pinMode(BUZZER_PIN, OUTPUT);
 
  /**
   * LOW to trigger the interrupt whenever the pin is low,
   * CHANGE to trigger the interrupt whenever the pin changes value
   * RISING to trigger when the pin goes from low to high,
   * FALLING for when the pin goes from high to low.
   */
  attachInterrupt(SWITCH_INT, button, RISING);
 
  /* initialize serial communication */
  Serial.begin(9600);
}
 
void loop() {
  long hWatherCm;
  int litres;
 
  hWatherCm = read_height();
  if (check_alarm(hWatherCm) != 0) /* read again wather height */
    hWatherCm = read_height();
 
  lcd.clear();
 
  print_histogram(hWatherCm);
 
  lcd.setCursor(0, 1);
 
  lcd.print(hWatherCm);
  lcd.print(" cm - ");
 
  // litres = SUPERFICE_BASE * (hWather / 100.0) * 1000
  litres = floor(SUPERFICE_BASE * hWatherCm * 10);
  lcd.print(litres);
  lcd.print(" l ");
 
  lcd.setCursor(14, 1);
  lcd.write(SOUND_ICON);
  lcd.setCursor(15, 1);
  if (!mute)
    lcd.write(SOUND_ICON_ON);
  else
    lcd.write('X');
 
/*
  Serial.print("cm = ");
  Serial.println(hWatherCm);
*/
 
  switch (check_alarm(hWatherCm)) {
  case 1:
    lcd.setCursor(0, 0);
    lcd.write(ALARM_ICON);
 
    buzz(200);
    delay(DELAY_1);
    break;
 
  case 2:
    lcd.setCursor(0, 0);
    lcd.write(ALARM_ICON);
 
    buzz(200);
    delay(200);
    buzz(200);
    delay(DELAY_2);
    break;
 
  case 0: // no alarm
    delay(DELAY_0);
  }
}
 
void print_histogram(int hWatherCm) {
  int i;
  int bloks;
  float histogram;
 
  // hWatherCm : HPOZZO = histogram : SIZE_BAR
  histogram = (SIZE_BAR * hWatherCm) / H_POZZO;
  histogram = histogram + 0.5;
 
  bloks = (int)histogram / 5;
 
  for (i = 0; i < bloks; i++)
    lcd.write(5);
 
  if ((int)(histogram) % 5 > 0)
    lcd.write((int)(histogram) % 5);
}
 
long read_height() {
  /**
   * establish variables for duration of the ping,
   * and the distance result in centimeters:
   */
  long duration, hWatherCm;
 
  /**
   * The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
   * Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
   */
  pinMode(PING_PIN, OUTPUT);
  digitalWrite(PING_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(PING_PIN, HIGH);
  delayMicroseconds(5);
  digitalWrite(PING_PIN, LOW);
 
  /**
   * The same pin is used to read the signal from the PING))): a HIGH
   * pulse whose duration is the time (in microseconds) from the sending
   * of the ping to the reception of its echo off of an object.
   */
  pinMode(PING_PIN, INPUT);
  duration = pulseIn(PING_PIN, HIGH);
 
  /* convert the time into a distance */
  hWatherCm = H_POZZO - microseconds_to_centimeters(duration);
 
  if (hWatherCm < 0)
    return 0;
 
  if (hWatherCm > H_POZZO)
    return H_POZZO;
 
  return hWatherCm;
}
 
void buzz(int msec) {
  if (!mute)
    digitalWrite(BUZZER_PIN, HIGH);
  delay(msec);
  digitalWrite(BUZZER_PIN, LOW);
}
 
int check_alarm(int hWatherCm) {
  if (hWatherCm > SOGLIA_ALLARME_1) {
     if (hWatherCm < SOGLIA_ALLARME_2)
       return 1;
     else
       return 2;
  }
  return 0;
}
 
long microseconds_to_centimeters(long microseconds) {
  /**
   * The speed of sound is 340.29 m/s or 29.4 microseconds per centimeter.
   * The ping travels out and back, so to find the distance of the
   * object we take half of the distance travelled.
   */
  return microseconds / 29.387 / 2;
}
 
void button() {
  //  Serial.println("Pulsante premuto");
  mute = !mute;
 
  lcd.setCursor(15, 1);
  if (!mute)
    lcd.write(SOUND_ICON_ON);
  else
    lcd.write('X');
}


Advertisement

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: Software voor regenwaternivo.

Berichtdoor nicoverduin » 11 Mei 2015, 17:45

Als je wilt dat men een reactie geeft kan het handiger zijn om aan te geven "Wat het niet doet".....
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 4067
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Software voor regenwaternivo.

Berichtdoor shooter » 11 Mei 2015, 19:33

het is vooral een {}() verhaal wat niet helemaal klopt. zorg dat je de reference naast het programma hebt staan en dan zoeken naar de haken
elke regel nakijken want er staan vele fouten in het origineel.
het programma loopt overigens wel hoor als je de fouten eruit haalt. (maar ja dat zou ik verraad vinden naar nico als ik de oplossing geef.
paul deelen
shooter@home.nl

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: Software voor regenwaternivo.

Berichtdoor nicoverduin » 11 Mei 2015, 20:06

shooter schreef:maar ja dat zou ik verraad vinden naar nico als ik de oplossing geef.
Als je het maar weet :mrgreen: :mrgreen: :mrgreen: Bij uitstek een programma om veel van te leren :D
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 3
Geregistreerd: 22 Apr 2015, 22:16

Re: Software voor regenwaternivo.

Berichtdoor Evarist » 11 Mei 2015, 22:36

Dit is wat het niet doet. De foutmeldingen die ik niet goed begrijp.

Arduino: 1.6.4 (Windows Vista), Board:"Arduino Uno"

Waterput.ino: In function 'void setup()':
Waterput:58: error: assigning to an array from an initializer list
Waterput:70: error: assigning to an array from an initializer list
Waterput:82: error: assigning to an array from an initializer list
Waterput.ino: In function 'void loop()':
Waterput:146: error: call of overloaded 'write(int)' is ambiguous
Waterput.ino:146:25: note: candidates are:
In file included from Waterput.ino:7:0:
C:\Program Files\Arduino\libraries\LiquidCrystal\src/LiquidCrystal.h:83:18: note: virtual size_t LiquidCrystal::write(uint8_t)
virtual size_t write(uint8_t);
^
In file included from C:\Program Files\Arduino\libraries\LiquidCrystal\src/LiquidCrystal.h:5:0,
from Waterput.ino:7:
C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/Print.h:49:12: note: size_t Print::write(const char*)
size_t write(const char *str) {
^
Waterput:154: error: call of overloaded 'write(int)' is ambiguous
Waterput.ino:154:25: note: candidates are:
In file included from Waterput.ino:7:0:
C:\Program Files\Arduino\libraries\LiquidCrystal\src/LiquidCrystal.h:83:18: note: virtual size_t LiquidCrystal::write(uint8_t)
virtual size_t write(uint8_t);
^
In file included from C:\Program Files\Arduino\libraries\LiquidCrystal\src/LiquidCrystal.h:5:0,
from Waterput.ino:7:
C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/Print.h:49:12: note: size_t Print::write(const char*)
size_t write(const char *str) {
^
assigning to an array from an initializer list

Dit rapport zou meer informatie hebben met
"Tijdens de compilatie uitgebreide uitvoer weergeven"
ingeschakeld in Bestand > Voorkeuren.

Berichten: 3
Geregistreerd: 22 Apr 2015, 22:16

Re: Software voor regenwaternivo.

Berichtdoor Evarist » 13 Mei 2015, 20:55

Ik snap dus niets van die foutcode's.

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: Software voor regenwaternivo.

Berichtdoor nicoverduin » 13 Mei 2015, 21:09

Ik zou de Arduino IDE versie 1.5.8 of 1.5.2 installeren. Daar ligt vermoedelijk het probleem. De nieuwe compilers zijn veel strikter (zoals het hoort) maar de voorbeeld sketches zijn niet allemaal door ontwikkelaars schreven die perfectie in de taal willen toepassen.
Wil je dat gaan leren, dan zet je het waarschuwings niveau op maximaal en streef je ernaar om geen enkele fout cq waarschuwing te krijgen.
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Terug naar C code

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 34 gasten