Running essentially the same code that is working on a mkr1010 wifi and RP2040 connect.
I can see the read sensor data in serial monitor, and threshold value set from cloud int values via the dashboard, and boolean values seem to be updating, but the sensorValue1 and sensorValue2 and threshDiff values are not updating to the cloud.
Not sure if there is a difference that I may have missed in the cloud connect code that is specific to the R4, or not.
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/72fdc536-58ed-467c-82a3-170dbd1a36ee
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
int sensorValue1;
int sensorValue2;
int threshDiff;
int threshold;
bool panel_Down;
bool panel_Stop;
bool panel_Up;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
const int Extend = 6;
const int Retract = 7;
int sensorPin1 = A2;
int sensorPin2 = A3;
int counter = 0;
//int valAverage1 = 0;
//int valAverage2 = 0;
int numAverage = 10;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
//valAverage1 =0;
//valAverage2 =0;
for (counter = 0; counter < numAverage; counter++) {
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
threshDiff = (sensorValue2 - (sensorValue1 + threshold));
//valAverage1 = valAverage1 + sensorValue1;
//valAverage2 = valAverage2 + sensorValue2;
}
//valAverage1 = valAverage1/numAverage;
//valAverage2 = valAverage2/numAverage;
//Serial.print(valAverage1, DEC);
//Serial.print(", ");
//Serial.println(valAverage2, DEC);
if (sensorValue1 > sensorValue2 + threshold)
{
digitalWrite(Extend, LOW);
digitalWrite(Retract, HIGH);
delay(1000);
digitalWrite(Extend, LOW);
digitalWrite(Retract, LOW);
delay(500);
}
else if (sensorValue2 > sensorValue1 + threshold)
{
digitalWrite(Retract, LOW);
digitalWrite(Extend, HIGH);
delay(1000);
digitalWrite(Retract, LOW);
digitalWrite(Extend, LOW);
delay(500);
}
else {
if(panel_Up == HIGH) {
digitalWrite(Retract, LOW);
digitalWrite(Extend, HIGH);
}
else {
digitalWrite(Extend, LOW);
digitalWrite(Retract, LOW);
}
}
Serial.print("sens_1 ");
Serial.print(analogRead(sensorPin1));
Serial.println();
Serial.print("sens_2 ");
Serial.print(analogRead(sensorPin2));
Serial.println();
Serial.print("Threshold Difference ");
Serial.print(threshDiff);
Serial.println();
delay(1000);
}
/*
Since Threshold is READ_WRITE variable, onThresholdChange() is
executed every time a new value is received from IoT Cloud.
*/
void onThresholdChange() {
// Add your code here to act upon Threshold change
}