Foutmelding bij berekening

Hallo Iedereen,

Ik probeer een klok - thermometer met een 7 segment display. Ik probeer het geheel in blokjes op te bouwen.

momenteel wil in enkel de enkeltallen van de seconden op een enkel 7 segment display weergeven ( het display moet uiteindelijk ge-multiplexed worden daarom dat ik de eenheden, tientallen enz. wil scheiden.

bij deze berekening krijg ik een foutmelding en ik begrijp niet goed waarom :

: 1.6.6 (Mac OS X), Board:"Arduino Leonardo"
/Users/koenpeeters/Documents/Arduino/sketch_jan26a_DHT11_DS3231_BuildUp/sketch_jan26a_DHT11_DS3231_BuildUp.ino: In function 'void loop()':
sketch_jan26a_DHT11_DS3231_BuildUp:159: error: invalid operands of types '' and 'int' to binary 'operator/'
int tens = second / 10;
** ^**
sketch_jan26a_DHT11_DS3231_BuildUp:160: error: invalid operands of types '' and 'int' to binary 'operator-'
int singles = second - ( tens*10);
** ^**
exit status 1
invalid operands of types '' and 'int' to binary 'operator/'

#include <DS3232RTC.h>

#include <Time.h>

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#include <Wire.h>


#define DHTPIN 4     // what digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

// Pin connected to ST_CP of 74HC595
int latchPin = 8;//8
// Pin connected to SH_CP of 74HC595
int clockPin = 9;  //9
// Pin connected to DS of 74HC595
int dataPin = 10; //10

byte dataArray[12];
byte dataToSend[3];

int tens;  
int singles;

void setup() {
  
  dht.begin();
  Wire.begin();
  Serial.begin(9600);
  //Serial.println("DHTxx test!");

  setSyncProvider(RTC.get);   // the function to get the time from the RTC
    if(timeStatus() != timeSet) 
        Serial.println("Unable to sync with the RTC");
    else
        Serial.println("RTC has set the system time");

  RTC.squareWave(SQWAVE_1_HZ);
  
  pinMode(latchPin, OUTPUT);

  //Bitmasks for the symbols to display on the 7 segment display
  dataArray[0] = 0x3F;  //00111111 0
  dataArray[1] = 0x06;  //00000110 1
  dataArray[2] = 0x5B;  //01011011 2
  dataArray[3] = 0x4F;  //01001111 3
  dataArray[4] = 0x66;  //01100110 4
  dataArray[5] = 0x6D;  //01101101 5
  dataArray[6] = 0x7D;  //01111101 6
  dataArray[7] = 0x07;  //00000111 7
  dataArray[8] = 0x7F;  //01111111 8
  dataArray[9] = 0x6F;  //01101111 9
  dataArray[10] = 0x33; //00110011 °
  dataArray[11] = 0x39; //00111001 C
 
}


void loop() {
  // Wait a few seconds between measurements.
  //delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  //float f = dht.readTemperature(true);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
  
  digitalClockDisplay(); 
  //delay(1000);

 
  int tens = second / 10;
  int singles = second - ( tens*10);
  
  dataToSend[0] += dataArray[7];
  

  shiftOut(dataPin, clockPin, dataToSend[0]);
  //shiftOut(dataPin, clockPin, dataToSend[1]);  
  //shiftOut(dataPin, clockPin, dataToSend[2]);  
  digitalWrite(latchPin, 1);

  delay(500);
}

void digitalClockDisplay(void)
{
    // digital clock display of the time
    Serial.print(hour());
    printDigits(minute());
    printDigits(second());
    Serial.print(' ');
    Serial.print(day());
    Serial.print(' ');
    Serial.print(month());
    Serial.print(' ');
    Serial.print(year()); 
    Serial.println(); 
}

void printDigits(int digits)
{
    // utility function for digital clock display: prints preceding colon and leading 0
    Serial.print(':');
    if(digits < 10)
        Serial.print('0');
    Serial.print(digits);
}

// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);


  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);


  //for each bit in the byte myDataOut
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);


    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {    
      pinState= 0;
    }


    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }


  //stop shifting
  digitalWrite(myClockPin, 0);
}

Hoi.

Je hebt deze variabelen al globaal gedefineerd, bovenaan maak je een int aan met de naam tens, en gelijk daarna eentje met de naam second.
Door dat op die plaats te doen, gelden ze overal in je sketch.
Vervolgens maak je er ze in 'loop' nog een keer aan.
Dat gaat fout en de compiler denkt dat je een functie wil aanroepen, maar dat doe je dan verkeerd (omdat je dat helemaal niet wilde doen, toch ?).
Ik vermoed dat je probleem is opgelost als je de variabele gewoon bewerkt (hij was al beschikbaar gemaakt, en is dat overal in ja sketch).
Dus haal je het stukje tekst "int" ui de betreffende regels weg.

Probeer dat maar eens, succes.

Hoi MAS3,

ik heb dit geprobeerd zoals je voorstelde, maar de IDE blijft dezelfde foutmelding genereren bij het compileren.

Als ik hetzelfde doe met de uitgelezen temperatuur van de DHT sensor, dan werkt het wel.

Ik denk dat er een datatype niet compatibel is ergens... Alleen kan ik niet vinden waar.

Ook werkt dooe de traagheid van de DHT het multiplexen niet zo goed...

hier heb ik nog een opgekuiste versie van de code:

#include <DS3232RTC.h>

#include <Time.h>

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#include <Wire.h>


#define DHTPIN 4     // what digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

// Pin connected to ST_CP of 74HC595
int latchPin = 8;//8
// Pin connected to SH_CP of 74HC595
int clockPin = 9;  //9
// Pin connected to DS of 74HC595
int dataPin = 10; //10


byte DigitOne[12]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x33, 0x39};
byte DigitTwo[12]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x33, 0x39};
byte DigitThree[12]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x33, 0x39};
byte DigitFour[12]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x33, 0x39};

byte DigitSelector[8]={0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};

int tensTemp;
int singlesTemp;

int tensSeconds;
int singlesSeconds;


void setup() {
  
  dht.begin();
  Wire.begin();
  Serial.begin(115200);
  //Serial.println("DHTxx test!");

  setSyncProvider(RTC.get);   // the function to get the time from the RTC
    if(timeStatus() != timeSet) 
        Serial.println("Unable to sync with the RTC");
    else
        Serial.println("RTC has set the system time");

  RTC.squareWave(SQWAVE_1_HZ);
  
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  
}


void loop() {


  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  //float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  //float f = dht.readTemperature(true);
/*
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
  */
  //digitalClockDisplay(); 
  
  tensTemp = t/10;
  singlesTemp = t - (tensTemp*10);

  tensSeconds = second/10;
  singlesSeconds = second - (tensSeconds*10);
  
  digitalWrite(latchPin, 0);
  shiftOut(dataPin, clockPin, MSBFIRST, DigitSelector[0]);
 

  
  shiftOut(dataPin, clockPin, MSBFIRST, DigitOne[singlesTemp]);
  
  digitalWrite(latchPin, 1);
  delay(1);
  digitalWrite(latchPin, 0);
  shiftOut(dataPin, clockPin, MSBFIRST, DigitSelector[1]);
 

  shiftOut(dataPin, clockPin, MSBFIRST, DigitTwo[tensTemp]);
  
  digitalWrite(latchPin, 1);
  delay(1);
  
}

Je kunt geen berekeningen doen met verschillende datatypes.
Is second wel een int ?

Ik denk het wel, aangezien ik de time.h bibliotheek gebruik....
Daarin kon ik volgende vinden:

/*============================================================================*/
/*  time and date functions   */
int     hour();            // the hour now 
int     hour(time_t t);    // the hour for the given time
int     hourFormat12();    // the hour now in 12 hour format
int     hourFormat12(time_t t); // the hour for the given time in 12 hour format
uint8_t isAM();            // returns true if time now is AM
uint8_t isAM(time_t t);    // returns true the given time is AM
uint8_t isPM();            // returns true if time now is PM
uint8_t isPM(time_t t);    // returns true the given time is PM
int     minute();          // the minute now 
int     minute(time_t t);  // the minute for the given time
int     second();          // the second now 
int     second(time_t t);  // the second for the given time
int     day();             // the day now 
int     day(time_t t);     // the day for the given time
int     weekday();         // the weekday now (Sunday is day 1) 
int     weekday(time_t t); // the weekday for the given time 
int     month();           // the month now  (Jan is month 1)
int     month(time_t t);   // the month for the given time
int     year();            // the full four digit year: (2009, 2010 etc) 
int     year(time_t t);    // the year for the given time

daarin staat second toch als een int?

Ik heb het gevonden :slight_smile:

int seconds = second();
  tensSeconds = seconds/10;
  singlesSeconds = seconds - (tensSeconds*10);

zo werkt het wel...

MAS3:
.. de compiler denkt dat je een functie wil aanroepen, maar dat doe je dan verkeerd (omdat je dat helemaal niet wilde doen, toch ?).

Dus toch wel..
Maar het is opgelost, das het belangrijkst.

zoek eens op wat % betekent, dat maakt je berekening wat eenvoudiger.