code_arduino.ino 2.53 KB
int incomingByte = 0;   // for incoming serial data
int i;
char value[3];
char lengh[4];
float temperatureC;

int led1 = 2;
int led2 = 3;
int ledstate1 = 0;
int ledstate2 = 0;

unsigned long previousMillis = 0;
const long interval = 1000;



void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
        pinMode(led1, OUTPUT);
        pinMode(led2, OUTPUT);
}


void loop()
{
  if (Serial.available() > 0)
  {
    incomingByte = Serial.read();
    if (incomingByte == 0)
    {
      int sensorValue = analogRead(A0);
      int n = (log10(sensorValue) + 1);  // nombre de chiffres composant le nombre
      itoa(n, lengh, 10);
      itoa(sensorValue, value, 10);
      strcat(lengh,value);
      Serial.println(lengh);
    }
    else if (incomingByte == 1)
    {
      int sensorValue = analogRead(A1);
      int n = (log10(sensorValue) + 1);  // nombre de chiffres composant le nombre
      itoa(n, lengh, 10);
      itoa(sensorValue, value, 10);
      strcat(lengh,value);
      Serial.println(lengh);
    }
    
    else if (incomingByte == 2)
    {
      int sensorValue = analogRead(A2);
      int n = (log10(sensorValue) + 1);  // nombre de chiffres composant le nombre
      itoa(n, lengh, 10);
      itoa(sensorValue, value, 10);
      strcat(lengh,value);
      Serial.println(lengh);
    }
    else if (incomingByte == 3)
    {
      int sensorValue = ledstate1;
      int n = (log10(sensorValue) + 1);
      itoa(n, lengh, 10);
      itoa(sensorValue, value, 10);
      strcat(lengh,value);
      Serial.println(lengh);
    }
    else if (incomingByte == 4)
    {
      int sensorValue = ledstate2;
      int n = (log10(sensorValue) + 1);
      itoa(n, lengh, 10);
      itoa(sensorValue, value, 10);
      strcat(lengh,value);
      Serial.println(lengh);
    }
  }
  
  
  ////////////////////////////////////
  //FIN DE LA PARTIE DE COM SERIE
  ////////////////////////////////////
  
 
 
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= interval) 
  {
    int valeur = analogRead(A2);
    float tension = valeur * 5.0;
    tension /= 1024.0;
    float temperature = ((tension * 1000) - 500) / 10;
    previousMillis = currentMillis;
    Serial.println(temperature);
    if (temperature < 27)
    {
      digitalWrite(led2, HIGH);
      ledstate2=1;
    }
    else 
    {
      digitalWrite(led2, LOW);
      ledstate2=0;
    }
    if (analogRead(A1) < 300)
    {
      digitalWrite(led1, HIGH);
      ledstate1=1;
    }
    else 
    {
      digitalWrite(led1, LOW);
      ledstate1=0;
    }
  }
  
  
}