Comment les objets électroniques prennent-ils soin de notre maison en notre absence?
   Comment les objets électroniques prennent-ils soin de notre maison en notre absence?

La  programmation

Voici le programme que nous avons écrit pour piloter notre objet connecté.

 

En vert est écrit le programme et en gris les commentaires pour comprendre le programme.

 

#include <SoftwareSerial.h>   // Insert Library for Serial communication to ports

SoftwareSerial BT(10,11);   // the number of the bluetooth receptor and transmitor pins
  // named constants for the switch and motor pins
const int switchPin =  2;   // the number of the switch pin
const int motorPin =  9;   // the number of the motor pin
const int rxPin =  10;   // the number of the motor pin
const int txPin =  11;   // the number of the motor pin

int switchState = 0;    // variable for reading the switch's status
int motorspeed = 0;   //variable for reading the motor's speed
int sequence = 1;    //variable for reading the current speed of motor

void setup() {   // loop to initialize all the pins needed
  BT.begin(9600);
  Serial.begin(9600);
    // initialize the motor pin as an output:
  pinMode(motorPin, OUTPUT);  // initialize the switch pin as an input:
  pinMode(switchPin, INPUT);
  pinMode(rxPin,INPUT);   // initialize the bluetooth receptor pin as an input
  pinMode(txPin,OUTPUT);   // initialize the bluetooth transmitor
  
  Serial.println("coucou");  // say "coucou"

}// end of the first void

void loop() { //loop to initialize the programme sent in the circuit
  while (BT.available()>0){ //start of the while loop via bluetooth
    
    motorspeed = BT.read();  // set speed via smartphone
    Serial.println("Speed= ");  // say "speed="
    Serial.println(motorspeed);  // say the current motor speed

   switch (motorspeed) {  // change the motor's speed when the smartphone's button is pushed
    case 0:  //change the motor's speed to 0
      Serial.print("Motor is OFF");  // say "Motor is OFF"
      break;
    case 255:  //change the motor's speed to 255
      Serial.print("Motor runs FAST");  // say "Motor runs FAST"
      break;
     default: //change the motor's speed 0
      Serial.print("Why am I here?");  // say "Why am I here?"
    break;
   }  // end of the switch case loop
    analogWrite(motorPin, motorspeed); 
    
  }  // end of the while loop
  
 // Serial.print(" S=");
 //  Serial.print(motorspeed);

  
  
  // read the state of the switch value:
  // switchState = digitalRead(switchPin);

  // check if the switch is pressed.
  if(switchState == HIGH) { // read the state of the button
    // turn motor on:

    

   switch (sequence) { // start of the second switch case loop
    case 1:
      motorspeed = 0;  //do something when sequence equals 1
      sequence = 2;
      break;
    case 2:
      motorspeed = 100;  //do something when sequence equals 2
      sequence = 3;
      break;
    case 3:
      motorspeed = 255;  //do something when sequence equals 3
      sequence = 1;
      break;
    default: 
      motorspeed = 0;  // if nothing else matches, do the default
      // default is optional
      sequence = 1;
    break;
   }  // end of the second switch case loop
  analogWrite(motorPin, motorspeed); 
  delay(1000); //wait a second before starting
 }  // end of the if else loop
}  // end of the second void loop

 

Versión para imprimir Versión para imprimir | Mapa del sitio
Antoine, Théo, Victor