I can't control the dim led with the potentiometer

what is wrong?

/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/5e741b64-9034-4e98-9313-34f1ba7eedc0

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  bool dimled;

  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"

int potPin = A1;
int gPin = 5;
int potVal;
float LEDVal;

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);
  pinMode(potPin, INPUT);
  pinMode(gPin, OUTPUT);

  // 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
  potVal = analogRead(potPin);
  LEDVal = (255. / 1023.) * potVal;
  Serial.println(LEDVal);

}



/*
  Since Dimled is READ_WRITE variable, onDimledChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onDimledChange()  {
  // Add your code here to act upon Dimled change
  if (dimled == 1) {

    potVal = analogRead(potPin);

    LEDVal = (255. / 1023.) * potVal;
    Serial.println(LEDVal);
    analogWrite(gPin, LEDVal);

    delay(1000);
  }  else {

    analogWrite(gPin, LOW);


    delay(1000);
  }
}

Your topic has been moved to the IoT Cloud category of the forum

I do not use the cloud but you have not defined gPin. That has to be one of the following pins depending on the Arduino. The following is from: https://support.arduino.cc/hc/en-us/articles/9350537961500-Use-PWM-output-with-Arduino

Board PWM Pins *
UNO (R3 and earlier), Nano, Mini 3, 5, 6, 9, 10, 11
UNO R4 (Minima, WiFi) 3, 5, 6, 9, 10, 11
Mega 2 - 13, 44 - 46
GIGA R1 2 - 13
Leonardo, Micro, Yún 3, 5, 6, 9, 10, 11, 13
UNO WiFi Rev2, Nano Every 3, 5, 6, 9, 10
MKR boards** 0 - 8, 10, A3, A4
MKR1000 WiFi** 0 - 8, 10, 11, A3, A4
Zero** 3 - 13, A0, A1
Nano 33 IoT** 2, 3, 5, 6, 9 - 12, A2, A3, A5
Nano 33 BLE/BLE Sense 1 - 13, A0 - A7
Due*** 2-13
101 3, 5, 6, 9

I see

int gPin = 5;

in the sketch

I'm sorry but I don't understand what you mean by gpin not being defined

I missed it.

it works when i use r4wifi but not when i use mkr1010 wifi

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.