Je tente de programmer une voiture télécommandé pour mon projet de fin d'année mais j'ai un problème avec ma variable <>.
Quand j'essaie de faire fonctionner une fonction que j'ai créé mon <> qui reprend le message que j'envoie récupère de vieille information et mon code ne fonctionne pas très bien. Avez-vous une solution pour permettre a la variable <> de vidé ce qu'elle possède et qu'elle se reremplisse avec le nouveau message que je lui envoie ?
Voici le code que j'ai produit avec mon professeur :
Code Envoyer :
#include <VirtualWire.h>
const int envoie_pin = 2;
int len;
String message;
boolean actif = true;
int mode = 0;
const int button = 3;
const int pinX = A2; // Axe X du joystick
const int pinY = A4; // Axe Y du joystick
const int buttonJ = 8;
double compt = 0;
void setup() {
Serial.begin(9600);
vw_setup(2000);
vw_set_tx_pin(envoie_pin);
pinMode(button,INPUT_PULLUP);
pinMode(buttonJ,INPUT_PULLUP);
//--------------------------------------------------------
pinMode(pinX, INPUT);
pinMode(pinY, INPUT);
pinMode(buttonJ, INPUT_PULLUP);
}
void loop() {
if(digitalRead(button) == 0 && actif){
mode = (mode+1)%2;
if(mode == 0){
message = "rouleau:0";
}
else if (mode ==1){
message = "rouleau:1";
}
actif = false;
sendMsg(message);
}
if(digitalRead(button) == 1 && !actif){
actif = true;
}
//--------------------
//joystick
//--------------------
if(compt > 480000){
String message = "joystick:";
int boost;
double x = map(analogRead(pinX),0,1023, -512,512);
double y = map(analogRead(pinY),0,1023,512,-512);
if(digitalRead(buttonJ) == LOW){
boost = 1;
}
else if(digitalRead(buttonJ) == HIGH){
boost = 0;
}
message += String(x) + "|" + String(y) + "," + String(boost);
sendMsg(message);
compt = 0;
}
else {
compt ++;
}
}
void sendMsg(String message) {
int len = strlen(message.c_str());
vw_send(message.c_str(), len ); // On envoie le message
delay(100);
vw_wait_tx(); // On attend la fin de l'envoi
Serial.println(message);
Serial.println(len);
}
Code recevoir :
#include <VirtualWire.h>
const int receive_pin = 12;
//-----------------------------------------------
//Rouleau
//-----------------------------------------------
const int enableBridge1 = 2;
//---------------------------------------------------
// JOYSTICK
//---------------------------------------------------
//moteur 1
const int ENA = 5;
const int in1 = 6;
const int in2 = 7;
//moteur 2
const int ENB = 8;
const int in3 = 9;
const int in4 = 10;
int vitesseG;
int vitesseD;
void setup()
{
delay(15);
Serial.begin(9600); // Debugging only
// Initialise the IO and ISR
vw_set_rx_pin(receive_pin);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
//---------------------------------------------------
// Rouleau
//---------------------------------------------------
pinMode(enableBridge1,OUTPUT);
digitalWrite(enableBridge1,1);
//---------------------------------------------------
// JOYSTICK
//---------------------------------------------------
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(ENB,OUTPUT);
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
//---------------------------------------------------
// Rouleau
//---------------------------------------------------
String message = "";
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
char str[buflen];
for (i = 0; i < buflen; i++)
{
str[i] = char(buf[i]);
// Serial.println(i);
// Serial.println(str);
}
Serial.println(str);
Serial.println(buflen);
//char str[VW_MAX_MESSAGE_LEN] = message.c_str();
char* token;
char separator[] = ":";
token = strtok(str, separator);
String commande = token;
Serial.println(commande);
if(commande == "rouleau") {
token = strtok(NULL, separator);
String value = token;
//Serial.println(value);
rouleau(value.toInt());
}
}
/*
//------------------------------
//joystick
//------------------------------
else if ( commande == "joystick") {
token = strtok(NULL, separator);
String value = token;
separator[0] = ',';
token = strtok(value.c_str(), separator);
char * joystickCoordonne = token;
token = strtok(NULL, separator);
String Autrecommande = token;
separator[0] = '|';
token = strtok(joystickCoordonne, separator);
String valueX = token;
int x = valueX.toInt();
token = strtok(NULL, separator);
String valueY = token;
int y = valueY.toInt();
int boost = Autrecommande.toInt();
if(x*x + y*y < 50*50) Stop();
else {
if(y+x > 0) AvancerG();
else ReculerG();
if(y-x > 0) AvancerD();
else ReculerD();
int vitesseG = min(int(map(abs(y+x),0,1024,0,255)), 255);
int vitesseD = min(int(map(abs(y-x),0,1024,0,255)), 255);
if(boost == 1){
Boost(vitesseG, vitesseD);
//Serial.println("Boost");
}
SetSpeed(vitesseG, vitesseD);
//SetSpeed(255, 255);
}
}
}
}*/
//---------------------------------------------------
// Rouleau
//---------------------------------------------------
}
void rouleau(int value){
Serial.println(value);
if(value == 1) {
digitalWrite(enableBridge1,HIGH);
}
else if(value == 0){
digitalWrite(enableBridge1,LOW);
}
return;
}
/*
//--------------------
//joystick
//--------------------
void Stop(){
analogWrite(ENA,LOW);
analogWrite(ENB,LOW);
//Serial.println("ON ARRETE");
}
void AvancerG(){
digitalWrite(in1,0);
digitalWrite(in2,1);
Serial.println("on avance a gauche");
}
void AvancerD(){
digitalWrite(in3,0);
digitalWrite(in4,1);
Serial.println("on avance a droite");
}
void ReculerG(){
digitalWrite(in1,1);
digitalWrite(in2,0);
Serial.println("on recule a gauche");
}
void ReculerD(){
digitalWrite(in3,1);
digitalWrite(in4,0);
Serial.println("on recule a droit");
}
void Boost(int vitesseG, int vitesseD) {
vitesseD = vitesseD + vitesseD ;
vitesseG = vitesseG + vitesseG;
Serial.println("Boost");
if(vitesseG > 255){
vitesseG = 255;
}
if(vitesseD > 255){
vitesseD = 255;
}
}
void SetSpeed(int vitesseG, int vitesseD){
analogWrite(ENA, vitesseG);
analogWrite(ENB, vitesseD);
}
*/