ropy/twins/arduino_serialreader/arduino_serialreader.ino

120 lines
3.2 KiB
Arduino
Raw Normal View History

2022-11-07 03:09:51 +01:00
#include "Wire.h"
2022-11-06 14:49:34 +01:00
/******************************************************************************
* arduino-serialreader.ino
* @title: Lecteur du port serie d'une carte arduino
* @project: Ropy (Blender-EduTech)
* @lang: fr
* @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
* @copyright: Copyright (C) 2022 Philippe Roy
* @license: GNU GPL
******************************************************************************/
/******************************************************************************
* Pupitre
******************************************************************************/
2022-11-07 03:09:51 +01:00
// Adressage Led Arduino
const int led_com = 13; // Led de communication modele 3d-> arduino (onboard)
// Adressage Entrees Arduino
const int bt = 4; // Bouton
2022-11-06 14:49:34 +01:00
2022-11-07 03:09:51 +01:00
// Entrees numeriques (modele 3D)
bool bt_num=false; // Bouton numérique
// Adressage Sorties Arduino
const int v = 2; // Voyant
2022-11-06 14:49:34 +01:00
/******************************************************************************
* Communication serie
******************************************************************************/
String serial_msg = ""; // Message
bool serial_msg_complet = false; // Flag de message complet
/******************************************************************************
* Initialisation
******************************************************************************/
void setup() {
pinMode(bt, INPUT); // Bouton
pinMode(v, OUTPUT); // Voyant
digitalWrite(v, LOW);
2022-11-06 15:13:05 +01:00
Serial.begin(115200); // Liaison série
}
2022-11-06 14:49:34 +01:00
/******************************************************************************
* Boucle principale
******************************************************************************/
void loop() {
/*****
* Communication : modele 3d -> arduino
*****/
2022-11-07 03:09:51 +01:00
// digitalWrite(v, HIGH);
2022-11-06 14:49:34 +01:00
if (serial_msg_complet) {
Serial.println(serial_msg); // Echo
if (serial_msg =="S\n") bt_num=true; // S pour Set
if (serial_msg =="R\n") bt_num=false; // R pour Reset
2022-11-06 14:49:34 +01:00
2022-11-07 03:09:51 +01:00
// Serial.println("Echo : "+serial_msg);
2022-11-06 14:49:34 +01:00
serial_msg = "";
serial_msg_complet = false;
}
/*****
* Bouton (arduino -> modele 3d)
*
* Bouton physique : LOW = actif et HIGH = pas actif
*****/
2022-11-07 03:09:51 +01:00
//Serial.println("Bouton"+ digitalRead(bt));
if (digitalRead(bt)== LOW) {
Serial.println("Bouton actif");
digitalWrite(v, HIGH);
delay(300);
}
else {
// Serial.println("Bouton repos");
digitalWrite(v, LOW);
}
2022-11-06 14:49:34 +01:00
/*****
2022-11-07 03:09:51 +01:00
* Voyant modele 3d-> arduino
2022-11-06 14:49:34 +01:00
*
* Bouton numérique (modele 3d) : true = actif et false = pas actif
*****/
2022-11-07 03:09:51 +01:00
if (bt_num) {
digitalWrite(v, HIGH);
2022-11-06 14:49:34 +01:00
}
2022-11-07 03:09:51 +01:00
else {
digitalWrite(v, LOW);
2022-11-06 14:49:34 +01:00
}
2022-11-07 03:09:51 +01:00
//delay(300);
2022-11-06 14:49:34 +01:00
}
/******************************************************************************
* Evenements provoques par la communication serie
******************************************************************************/
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
serial_msg += inChar;
if (inChar == '\n') {
serial_msg_complet = true;
2022-11-07 03:09:51 +01:00
digitalWrite(v, HIGH);
2022-11-06 14:49:34 +01:00
}
}
}