A votre demande:
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/dd6e31cb-a4eb-4994-ad6b-a1f275aa6aed
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
int charge;
int fanspeed;
int rotaspeed;
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"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int fanset = 0; // consigne ventilateur initialisée à 0 rpm
int rotaset = 0; // consigne moteur initialisée à 0 rpm
const int fanplusPin = 18; // Bouton poussoir fan+ branché en pullup sur le pin 8
const int fanminusPin = 19; // Bouton poussoir fan- branché en pullup sur le pin 7
const int rotaplusPin = 15; // Bouton poussoir rotis+ branché en pullup sur le pin 4
const int rotaminusPin = 5; // Bouton poussoir rotis- branché en pullup sur le pin 2
int fanplusState = 0;
int fanminusState = 0;
int rotaplusState = 0;
int rotaminusState = 0;
const int fanpin = 25;
const int onoffstatepin = 4;
const int fangreenstatepin = 13;
const int fanbluestatepin = 14;
const int motorgreenstatepin = 18;
const int motorbluestatepin = 19;
const int motorpin = 26;
const int BATTERYPIN = 33; //pin de la batterie AVEC PONT DIVISEUR OBLIGATOIRE sur Batterie 12V avec Umax= 14V et R1=2200 et R2=1000 (Tcible arduino 5V) ou R1=24200 et R2=6900 (Tcible ESP 3.3V)
float voltage;
const int pwmfreq = 5000; // 5000 Hz
const int pwmfanChannel = 0;
const int pwmrotaChannel = 1;
const int pwmresolution = 8; // Résolution de 8 bits donc 256niveaux
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C // See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
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();
// Configure les canneaux 0 et 1
ledcSetup(pwmfanChannel, pwmfreq, pwmresolution);
ledcSetup(pwmrotaChannel, pwmfreq, pwmresolution);
// Configure Analogread pin36
analogReadResolution(12); //entre 9et12
// Attache canneaux sur pins
ledcAttachPin(fanpin, pwmfanChannel);
ledcAttachPin(motorpin, pwmrotaChannel);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.setTextSize(2); // Normal 2:1 pixel scale
display.setCursor(0,0);
display.println("THE");
display.setCursor(60,25);
display.print("GRILL");
display.setCursor(100,50);
display.print("LINE");
display.display();
delay(3000);
display.clearDisplay();
//définition des modes
pinMode(fanplusPin, INPUT_PULLUP);
pinMode(fanminusPin, INPUT_PULLUP);
pinMode(rotaplusPin, INPUT_PULLUP);
pinMode(rotaminusPin, INPUT_PULLUP);
pinMode(33,INPUT);
analogRead(33);
}
void loop() {
ArduinoCloud.update();
// Your code here
voltage = analogRead(BATTERYPIN); //valeur analogique covertie sur 12bits
charge = voltage/4095*100; //mettre en pourcentage
if (charge > 100) //max is 100%
charge = 100;
else if (charge < 0) //min is 0%
charge = 0;
fanplusState = digitalRead(fanplusPin);
fanminusState = digitalRead(fanminusPin);
rotaplusState = digitalRead(rotaplusPin);
rotaminusState = digitalRead(rotaminusPin);
//test des conditions
if (fanplusState == LOW) //test si fanplus appuyé
{
fanspeed = fanspeed +25;
if (fanspeed>100)
{
fanspeed = 100;
}
delay(500);
}
if (fanminusState == LOW) //test si fanminus appuyé
{
fanspeed = fanspeed -25;
if (fanspeed<0)
{
fanspeed = 0;
}
delay(500);
}
if (rotaplusState == LOW) //test si rotaplus appuyé
{
rotaspeed = rotaspeed +1;
if (rotaspeed>10)
{
rotaspeed = 10;
}
delay(500);
}
if (rotaminusState == LOW) //test si rotaminus appuyé
{
rotaspeed = rotaspeed -1;
if (rotaspeed<0)
{
rotaspeed = 0;
}
delay(500);
}
//convertion %age to pwm
fanset = map(fanspeed,0,100,0,255);
rotaset = map(rotaspeed,0,10,0,255);
//envoi consigne
ledcWrite(fanpin,fanset);
ledcWrite(motorpin,rotaset);
// Envoi réglages sur display
display.setTextSize(2); // Double 2:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0);
display.println("Fan:");
display.setCursor(60,0);
display.print(fanspeed);
display.setCursor(100,0);
display.print("%");
display.setCursor(0,25);
display.print("Rot:");
display.setCursor(60,25);
display.print(rotaspeed);
display.setCursor(90,25);
display.print("t/m");
display.setCursor(0,50);
display.print("Charge");
display.setCursor(80,50);
display.print(charge,0);
display.setCursor(115,50);
display.print("%");
display.display();
display.clearDisplay();
Serial.print("-");
Serial.print(rotaminusState);
Serial.print(rotaplusState);
Serial.print(fanminusState);
Serial.print(fanplusState);
delay(1000);
}
/*
Since Charge is READ_WRITE variable, onChargeChange() is
executed every time a new value is received from IoT Cloud.
*/
void onChargeChange() {
// Add your code here to act upon Charge change
}
/*
Since Fanspeed is READ_WRITE variable, onFanspeedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onFanspeedChange() {
// Add your code here to act upon Fanspeed change
}
/*
Since Rotaspeed is READ_WRITE variable, onRotaspeedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onRotaspeedChange() {
// Add your code here to act upon Rotaspeed change
}