I'm having an error when trying to pass a struct to different functions. I followed this guide (specifically the procedure outlined in #8) Passing struct to function (again) - Syntax & Programs - Arduino Forum and am still having issues.
Here is my 'rtc.h'.
#ifndef rtc_h
#define rtc_h
typedef struct{
int seconds_ones;
int seconds_tens;
int minutes_ones;
int minutes_tens;
int hours_ones;
int hours_tens;
int date_ones;
int date_tens;
int month_ones;
int month_tens;
int year_ones;
int year_tens;
} RTC_STRUCT;
#endif
Here is my main application.
#define rtc_address B1101000
#define rtc_seconds 0x00
#define rtc_minutes 0x01
#define rtc_hours 0x02
#define rtc_day 0x03
#define rtc_date 0x04
#define rtc_month 0x05
#define rtc_year 0x06
#define rtc_control 0x0E
#define rtc_status 0x0F
#define rtc_trickle 0x10 // all defines above are register addresses
#define rtc_trickle_val 0xA6 //no diode, 2k ohm
#define set_seconds_ones 0
#define set_seconds_tens 0
#define set_minutes_ones 0
#define set_minutes_tens 0
#define set_hours_ones 0
#define set_hours_tens 0
#define set_date_ones 2
#define set_date_tens 2
#define set_month_ones 4
#define set_month_tens 0
#define set_year_ones 7
#define set_year_tens 1
//all defines above are for initial setting of RTC time
#include <Wire.h>
#include "rtc.h"
RTC_STRUCT rtc = {set_seconds_ones, set_seconds_tens, set_minutes_ones, set_minutes_tens,
set_hours_ones, set_hours_tens, set_date_ones, set_date_tens, set_month_ones, set_month_tens,
set_year_ones, set_year_tens};
void setup() {
Wire.begin(); //I2C initialization
Serial.begin(9600);
}
void loop(){
i2c_read( &rtc );
Serial.println("----------");
Serial.print("Seconds:");
Serial.print(rtc.seconds_ones);
Serial.println(rtc.seconds_tens);
Serial.println("----------");
delay(1000);
}
void i2c_read( rtc* r){
byte i2c_read_buffer[17];
Wire.beginTransmission(rtc_address); //send rtc address
Wire.write(rtc_seconds); //start position of read
int n = Wire.requestFrom(rtc_address, 17); //request only one byte
if(Wire.available ()<= 17){
for (int i = 0; i < n; i++){
i2c_read_buffer[i] = Wire.read(); //read that byte
}
}
Wire.endTransmission(); //end read
for (int i = 0; i < n; i++){ //fill the struct
if(i = 0)
{
r -> seconds_ones = i2c_read_buffer[i] & B00001111;
r -> seconds_tens = i2c_read_buffer[i] >> 4;
}
else if(i = 1)
{
r -> minutes_ones = i2c_read_buffer[i] & B00001111;
r -> minutes_tens = i2c_read_buffer[i] >> 4;
}
else if(i = 2)
{
r -> hours_ones = i2c_read_buffer[i] & B00001111;
r -> hours_tens = i2c_read_buffer[i] >> 4;
}
else if(i = 4)
{
r -> date_ones = i2c_read_buffer[i] & B00001111;
r -> date_tens = i2c_read_buffer[i] >> 4;
}
else if(i = 5)
{
r -> month_ones = i2c_read_buffer[i] & B00001111;
r -> month_tens = i2c_read_buffer[i] >> 4;
}
else if(i = 6)
{
r -> date_ones = i2c_read_buffer[i] & B00001111;
r -> date_tens = i2c_read_buffer[i] >> 4;
}
else
{
}
}
}
When I try to compile this I get the following error:
rtc_1:76: error: 'r' was not declared in this scope
void i2c_read( rtc* r){
^
exit status 1
variable or field 'i2c_read' declared void
I'm a little confused about this because I thought that r would have inherited the datatype RTC_STRUCT from the rtc struct that is getting passed to it. As far as I can tell this is exactly what they're doing in the example I gave that supposedly works.
Any help is appreciated. Thank you.