serial.ino 1.21 KB
 // Pot de fleurs connecté
int STBY = 2;
int PWMA = 6; 
int AIN1 = 3;
int AIN2 = 4;
int consigne = -1;

String received;

void setup() 
{
  pinMode(STBY, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);  
  Serial.begin(115200);
}

void loop() {

  int temp_analog = analogRead(A0);
  int water_lvl = analogRead(A1);
  int hum_purcent = analogRead(A2)/10.23;
  
  received = Serial.readString();

  if(received != "")
  {
    
    char c = received.charAt(0);

    if(c == 'M')
    {
      consigne = (received.substring(1)).toInt();
      Serial.print(received);
      delay(50);
    }

  }

  if(hum_purcent < consigne && consigne != -1 )
  {
    move(255); 
  }
  else
  {
    stop();
  }
  
  String water = "N"+String(water_lvl,DEC);
  String temp = "T"+String(temp_analog,DEC);
  String hum = "H"+String(hum_purcent,DEC);
  
  Serial.print(water);

  delay(300);

  Serial.print(hum);

  delay(300);

  Serial.print(temp);  
  
  delay(300);
}

void move(int speed){

  digitalWrite(STBY, HIGH); //disable standby

  boolean inPin1 = LOW;
  boolean inPin2 = HIGH;

  digitalWrite(AIN1, inPin1);
  digitalWrite(AIN2, inPin2);
  analogWrite(PWMA, speed);

}

void stop(){
  digitalWrite(STBY, LOW); 
}