Hi All
Have an issue with with a new board replacement.
I was using a Arduino UNO and the below code worked as it should.
I swapped the board today for a Arduino R4 wifi and the same program uploads with no errors but does not run.
I have tried uploading some of the example sketches which upload with no issues and work as intended.
Any help would be appreciated.
#include <SolarPosition.h>
#include <DS1307RTC.h>
const uint8_t digits = 3;
// some test positions:
SolarPosition Home(42.898551, -12.539774); // Home//RTC CODE
int sun_position;
const int encoder_a = 2; // Green - pin 2 - Digital
const int encoder_b = 3; // White - pin 3 - Digital
long encoder = 690;
int trackerAzimuthAngle;
int start=0;
int limitSwitchPin = 8;
int azLimitSwitch = 0;
const int azMotorPinPositive = 11;
const int azMotorPinNegative = 12;
int hours; int minutes; int seconds;
void setup() {
Serial.begin(9600);
pinMode(encoder_a, INPUT_PULLUP);
pinMode(encoder_b, INPUT_PULLUP);
attachInterrupt(0, encoderPinChangeA, CHANGE);
attachInterrupt(1, encoderPinChangeB, CHANGE);
pinMode(13, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(limitSwitchPin, INPUT);
pinMode(azMotorPinPositive, OUTPUT);
pinMode(azMotorPinNegative, OUTPUT);
// set the Time service as the time provider//RTC CODE
SolarPosition::setTimeProvider(RTC.get);
}
void loop() {
azLimitSwitch = digitalRead(limitSwitchPin);
if(start<1){
homing();
} // Send tracker back to home position if power enabled
start ++;
rtcCode();
if(hours > 12){
running();
Serial.print("RUNNING "); Serial.print('\n');}
else{Serial.print("Waiting ");Serial.print('\n');
}
if(hours == 13 && minutes == 39 && seconds == 0){
homing();}
delay(1000);
}
void homing() {
digitalWrite(azMotorPinNegative, HIGH);
digitalWrite(azMotorPinPositive, HIGH);
while(digitalRead(limitSwitchPin) == HIGH) {
Serial.print("HOMING");// Change this to move the motor
Serial.print('\n');
digitalWrite(azMotorPinNegative, LOW);
}
digitalWrite(azMotorPinNegative, HIGH);
while(digitalRead(limitSwitchPin) == LOW){
Serial.print("BACKING OFF");//Change this to move motor
Serial.print('\n');
digitalWrite(azMotorPinPositive, LOW);
encoder = 0;// Check this in main program
}
digitalWrite(azMotorPinPositive, HIGH);
}
void running(){
trackerAzimuthAngle = encoder/6.7;//change to degrees
if(sun_position > trackerAzimuthAngle){
digitalWrite(azMotorPinPositive, LOW);}
else{digitalWrite(11, HIGH);}
Serial.print("start ");
Serial.print(start);
Serial.print('\n');//establish if the program has just started
Serial.print("Tracker Azimuth Angle = ");
Serial.println(trackerAzimuthAngle);
}
void rtcCode(){
printTime(RTC.get());
Serial.print(F("Home:\t"));
printSolarPosition(Home.getSolarPosition(), digits);
Serial.println();
Serial.print('\n');
}
void encoderPinChangeA() {
encoder += digitalRead(encoder_a) == digitalRead(encoder_b) ? -1 : 1;
}
void encoderPinChangeB() {
encoder += digitalRead(encoder_a) != digitalRead(encoder_b) ? -1 : 1;
}
void printSolarPosition(SolarPosition_t pos, int numDigits)
{
Serial.print(F("el: "));
Serial.print(pos.elevation, numDigits);
Serial.print(F(" deg\t"));
Serial.print(F("az: "));
Serial.print(pos.azimuth, numDigits);
Serial.println(F(" deg"));
sun_position=(pos.azimuth);
}
void printTime(time_t t)
{
tmElements_t someTime;
breakTime(t, someTime);
Serial.print(someTime.Hour);
Serial.print(F(":"));
Serial.print(someTime.Minute);
Serial.print(F(":"));
Serial.print(someTime.Second);
Serial.print(F(" UTC on "));
Serial.print(dayStr(someTime.Wday));
Serial.print(F(", "));
Serial.print(monthStr(someTime.Month));
Serial.print(F(" "));
Serial.print(someTime.Day);
Serial.print(F(", "));
Serial.println(tmYearToCalendar(someTime.Year));
hours = someTime.Hour; minutes = someTime.Minute; seconds = someTime.Second;
}