Bonjour à tous,
En utilisant la library AFMotor, plus précisément la classe AF_DCMotor, Je reçois une erreur comme quoi MOTOR34_8KHZ utilise CS01 qui n'est pas défini. J'utilise la carte Arduino UNO WiFi Rev2.
Voici ci-besoin mon code :
#include <SPI.h>
#include <WiFiNINA.h>
#include <AFMotor.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key index number (needed only for WEP)
int led = LED_BUILTIN;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
AF_DCMotor motorLeft(3, MOTOR34_1KHZ);
AF_DCMotor motorRight(4, MOTOR34_1KHZ);
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Access Point Web Server");
pinMode(led, OUTPUT); // set the LED pin mode
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
motorLeft.setSpeed(0);
motorRight.setSpeed(0);
motorLeft.run(RELEASE);
motorRight.run(RELEASE);
// by default the local IP address will be 192.168.4.1
// you can override it with the following:
// WiFi.config(IPAddress(10, 0, 0, 1));
// print the network name (SSID);
Serial.print("Creating access point named: ");
Serial.println(ssid);
// Create open network. Change this line if you want to create an WEP network:
status = WiFi.beginAP(ssid, pass);
if (status != WL_AP_LISTENING) {
Serial.println("Creating access point failed");
// don't continue
while (true);
}
// wait 10 seconds for connection:
delay(10000);
// start the web server on port 80
server.begin();
// you're connected now, so print out the status
printWiFiStatus();
}
void loop() {
// compare the previous status to the current status
if (status != WiFi.status()) {
// it has changed update the variable
status = WiFi.status();
if (status == WL_AP_CONNECTED) {
// a device has connected to the AP
Serial.println("Device connected to AP");
} else {
// a device has disconnected from the AP, and we are back in listening mode
Serial.println("Device disconnected from AP");
}
}
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.println("<!DOCTYPE html>");
client.println("<html lang=\"fr\">");
client.println("<head>");
client.println(" <meta charset=\"UTF-8\" />");
client.println(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />");
client.println(" <title>Virtual joystick</title>");
client.println(" <style>");
client.println(" body {");
client.println(" overflow: hidden;");
client.println(" }");
client.println("");
client.println(" .joystick {");
client.println(" width: 200px;");
client.println(" height: 200px;");
client.println(" background-color: #ccc;");
client.println(" border-radius: 50%;");
client.println(" position: relative;");
client.println(" margin: 50px;");
client.println(" }");
client.println("");
client.println(" .knob {");
client.println(" width: 50px;");
client.println(" height: 50px;");
client.println(" background-color: #333;");
client.println(" border-radius: 50%;");
client.println(" position: absolute;");
client.println(" top: 75px;");
client.println(" left: 75px;");
client.println(" cursor: pointer;");
client.println(" }");
client.println(" </style>");
client.println("</head>");
client.println("<body>");
client.println(" <div class=\"joystick\" id=\"joystick\">");
client.println(" <div class=\"knob\" id=\"knob\"></div>");
client.println(" </div>");
client.println(" <script>");
client.println(" const joystick = document.getElementById(\"joystick\");");
client.println(" const knob = document.getElementById(\"knob\");");
client.println("");
client.println(" let isDragging = false;");
client.println("");
client.println(" const joystickRect = joystick.getBoundingClientRect();");
client.println(" const joystickCenterX = joystickRect.left + joystickRect.width / 2;");
client.println(" const joystickCenterY = joystickRect.top + joystickRect.height / 2;");
client.println("");
client.println(" let url = null;");
client.println("");
client.println(" function getCoordinates(event) {");
client.println(" if (event.touches && event.touches.length) {");
client.println(" return {");
client.println(" x: event.touches[0].clientX,");
client.println(" y: event.touches[0].clientY,");
client.println(" };");
client.println(" } else {");
client.println(" return {");
client.println(" x: event.clientX,");
client.println(" y: event.clientY,");
client.println(" };");
client.println(" }");
client.println(" }");
client.println(" function updateKnobPosition(event) {");
client.println(" const coordinates = getCoordinates(event);");
client.println(" const deltaX = coordinates.x - joystickCenterX;");
client.println(" const deltaY = coordinates.y - joystickCenterY;");
client.println(" const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);");
client.println(" let motorSpeed = parseInt(distance * 2.55);");
client.println(" const angleATan2 = Math.atan2(deltaY, deltaX);");
client.println(" const angle = angleATan2 * (180 / Math.PI);");
client.println("");
client.println(" let leftMotor = 0;");
client.println(" let rightMotor = 0;");
client.println(" let leftDirection = null;");
client.println(" let rightDirection = null;");
client.println(" if (distance > joystickRect.width / 2) {");
client.println(" motorSpeed = 255;");
client.println(" const newX = joystickCenterX + Math.cos(angleATan2) * (joystickRect.width / 2);");
client.println(" const newY = joystickCenterY + Math.sin(angleATan2) * (joystickRect.width / 2);");
client.println("");
client.println(" newPosX = newX - joystickRect.left - knob.offsetWidth / 2;");
client.println(" newPosY = newY - joystickRect.top - knob.offsetHeight / 2;");
client.println("");
client.println(" knob.style.left = newPosX + \"px\";");
client.println(" knob.style.top = newPosY + \"px\";");
client.println("");
client.println(" } else {");
client.println(" newPosX = coordinates.x - joystickRect.left - knob.offsetWidth / 2;");
client.println(" newPosY = coordinates.y - joystickRect.top - knob.offsetHeight / 2;");
client.println("");
client.println(" knob.style.left = newPosX + \"px\";");
client.println(" knob.style.top = newPosY + \"px\";");
client.println(" }");
client.println(" if (angle < -90) {");
client.println(" rightMotor = motorSpeed;");
client.println(" leftMotor = parseInt((angle + 135) / 45 * motorSpeed);");
client.println(" }");
client.println(" else if (angle >= -90 && angle < 0) {");
client.println(" rightMotor = parseInt((angle + 45) / (-45) * motorSpeed);");
client.println(" leftMotor = motorSpeed;");
client.println(" }");
client.println(" else if (angle >= 0 && angle < 90) {");
client.println(" leftMotor = -motorSpeed;");
client.println(" rightMotor = parseInt((angle - 45) / (-45) * motorSpeed);");
client.println(" }");
client.println(" else if (angle >= 90) {");
client.println(" leftMotor = parseInt((angle - 135) / 45 * motorSpeed);");
client.println(" rightMotor = -motorSpeed;");
client.println(" }");
client.println("");
client.println("");
client.println(" if (rightMotor < 0) {");
client.println(" rightMotor = 0 - rightMotor;");
client.println(" rightDirection = BACKWARD;");
client.println(" } else {");
client.println(" rightDirection = FORWARD;");
client.println(" }");
client.println("");
client.println(" if (leftMotor < 0) {");
client.println(" leftMotor = 0 - leftMotor;");
client.println(" leftDirection = BACKWARD;");
client.println(" } else {");
client.println(" leftDirection = FORWARD;");
client.println(" }");
client.println(" url = `?leftMotor=${leftMotor}&rightMotor=${rightMotor}&leftDirection=${leftDirection}&rightDirection=${rightDirection}`;");
client.println(" console.log(url);");
client.println(" }");
client.println("");
client.println(" knob.addEventListener(\"mousedown\", (event) => {");
client.println(" isDragging = true;");
client.println(" updateKnobPosition(event);");
client.println(" });");
client.println("");
client.println(" knob.addEventListener(\"touchstart\", (event) => {");
client.println(" isDragging = true;");
client.println(" updateKnobPosition(event);");
client.println(" });");
client.println("");
client.println(" document.addEventListener(\"mousemove\", (event) => {");
client.println(" if (isDragging) {");
client.println(" updateKnobPosition(event);");
client.println(" }");
client.println(" });");
client.println(" document.addEventListener(\"touchmove\", (event) => {");
client.println(" if (isDragging) {");
client.println(" updateKnobPosition(event);");
client.println(" }");
client.println(" });");
client.println(" document.addEventListener(\"mouseup\", () => {");
client.println(" isDragging = false;");
client.println(" knob.style.left = \"75px\";");
client.println(" knob.style.top = \"75px\";");
client.println(" url = \"?stop\";");
client.println(" });");
client.println(" document.addEventListener(\"touchend\", () => {");
client.println(" isDragging = false;");
client.println(" knob.style.left = \"75px\";");
client.println(" knob.style.top = \"75px\";");
client.println(" url = \"?stop\";");
client.println(" });");
client.println(" function sendRequest() {");
client.println(" const xhr = new XMLHttpRequest();");
client.println(" xhr.open(\"POST\", url, true);");
client.println(" xhr.onreadystatechange = () => {");
client.println(" if (xhr.readyState === XMLHttpRequest.DONE && xhr.status != 200) {");
client.println(" console.error(\"Erreur lors de l'envoi de la position du joystick!\");");
client.println(" }");
client.println(" };");
client.println(" xhr.send();");
client.println(" }");
client.println(" let sender = setInterval(sendRequest, 300);");
client.println(" </script>");
client.println("</body>");
client.println("</html>");
// The HTTP response ends with another blank line:
client.println();
String currentLine = "";
String leftMotorString = "leftMotor=";
String rightMotorString = "rightMotor=";
String leftDirectionString = "leftDirection=";
String rightDirectionString = "rightDirection=";
int leftMotorSpeed;
int rightMotorSpeed;
String leftDirection;
String rightDirection;
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
if (currentLine.startsWith("POST /?")) {
int start = currentLine.indexOf("POST /?") + 7; // trouver la position de la fin de "POST /?"
int end = currentLine.indexOf(" HTTP/1.1"); // trouver la position de " HTTP/1.1"
if (start != -1 && end != -1) {
currentLine = currentLine.substring(start, end); // extraire la partie de l'URL
Serial.println(currentLine);
if (currentLine == "stop") {
motorLeft.run(RELEASE);
motorRight.run(RELEASE);
} else {
currentLine = currentLine.substring(currentLine.indexOf(leftMotorString) + leftMotorString.length());
leftMotorSpeed = currentLine.substring(0, currentLine.indexOf("&")).toInt();
motorLeft.setSpeed(leftMotorSpeed);
Serial.println(leftMotorSpeed);
currentLine = currentLine.substring(currentLine.indexOf(rightMotorString) + leftMotorString.length());
rightMotorSpeed = currentLine.substring(0, currentLine.indexOf("&")).toInt();
motorRight.setSpeed(rightMotorSpeed);
Serial.println(rightMotorSpeed);
currentLine = currentLine.substring(currentLine.indexOf(leftDirectionString) + leftMotorString.length());
leftDirection = currentLine.substring(0, currentLine.indexOf("&"));
if (leftDirection == "FORWARD") {
motorLeft.run(FORWARD);
} else {
motorLeft.run(BACKWARD);
}
Serial.println(leftDirection);
currentLine = currentLine.substring(currentLine.indexOf(rightDirectionString) + leftMotorString.length());
rightDirection = currentLine;
if (rightDirection == "FORWARD") {
motorRight.run(FORWARD);
} else {
motorRight.run(BACKWARD);
}
Serial.println(rightDirection);
}
}
}
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
et l'erreur :
In file included from c:\users\main\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5\avr\include\avr\io.h:99:0,
from c:\users\main\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5\avr\include\avr\pgmspace.h:90,
from C:\Users\main\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.8\cores\arduino/api/String.h:31,
from C:\Users\main\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.8\cores\arduino/api/IPAddress.h:24,
from C:\Users\main\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.8\cores\arduino/api/ArduinoAPI.h:30,
from C:\Users\main\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.8\cores\arduino/Arduino.h:23,
from C:\Users\main\AppData\Local\Temp\arduino\sketches\32F96E674C6F643472455FAFA83BC13A\sketch\tests-wifi.ino.ino.cpp:1:
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:49:30: error: 'CS01' was not declared in this scope
#define MOTOR34_8KHZ _BV(CS01) // divide by 8
^
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:52:33: note: in expansion of macro 'MOTOR34_8KHZ'
#define DC_MOTOR_PWM_RATE MOTOR34_8KHZ // PWM rate for DC motors
^~~~~~~~~~~~
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:156:47: note: in expansion of macro 'DC_MOTOR_PWM_RATE'
AF_DCMotor(uint8_t motornum, uint8_t freq = DC_MOTOR_PWM_RATE);
^~~~~~~~~~~~~~~~~
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:49:30: note: suggested alternative: 'B101'
#define MOTOR34_8KHZ _BV(CS01) // divide by 8
^
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:52:33: note: in expansion of macro 'MOTOR34_8KHZ'
#define DC_MOTOR_PWM_RATE MOTOR34_8KHZ // PWM rate for DC motors
^~~~~~~~~~~~
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:156:47: note: in expansion of macro 'DC_MOTOR_PWM_RATE'
AF_DCMotor(uint8_t motornum, uint8_t freq = DC_MOTOR_PWM_RATE);
^~~~~~~~~~~~~~~~~
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:50:30: error: 'CS01' was not declared in this scope
#define MOTOR34_1KHZ _BV(CS01) | _BV(CS00) // divide by 64
^
C:\Users\main\Desktop\Programmation\Arduino\tests-wifi.ino\tests-wifi.ino.ino:32:25: note: in expansion of macro 'MOTOR34_1KHZ'
AF_DCMotor motorLeft(3, MOTOR34_1KHZ);
^~~~~~~~~~~~
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:50:30: note: suggested alternative: 'B101'
#define MOTOR34_1KHZ _BV(CS01) | _BV(CS00) // divide by 64
^
C:\Users\main\Desktop\Programmation\Arduino\tests-wifi.ino\tests-wifi.ino.ino:32:25: note: in expansion of macro 'MOTOR34_1KHZ'
AF_DCMotor motorLeft(3, MOTOR34_1KHZ);
^~~~~~~~~~~~
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:50:42: error: 'CS00' was not declared in this scope
#define MOTOR34_1KHZ _BV(CS01) | _BV(CS00) // divide by 64
^
C:\Users\main\Desktop\Programmation\Arduino\tests-wifi.ino\tests-wifi.ino.ino:32:25: note: in expansion of macro 'MOTOR34_1KHZ'
AF_DCMotor motorLeft(3, MOTOR34_1KHZ);
^~~~~~~~~~~~
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:50:42: note: suggested alternative: 'B100'
#define MOTOR34_1KHZ _BV(CS01) | _BV(CS00) // divide by 64
^
C:\Users\main\Desktop\Programmation\Arduino\tests-wifi.ino\tests-wifi.ino.ino:32:25: note: in expansion of macro 'MOTOR34_1KHZ'
AF_DCMotor motorLeft(3, MOTOR34_1KHZ);
^~~~~~~~~~~~
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:50:30: error: 'CS01' was not declared in this scope
#define MOTOR34_1KHZ _BV(CS01) | _BV(CS00) // divide by 64
^
C:\Users\main\Desktop\Programmation\Arduino\tests-wifi.ino\tests-wifi.ino.ino:33:26: note: in expansion of macro 'MOTOR34_1KHZ'
AF_DCMotor motorRight(4, MOTOR34_1KHZ);
^~~~~~~~~~~~
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:50:30: note: suggested alternative: 'B101'
#define MOTOR34_1KHZ _BV(CS01) | _BV(CS00) // divide by 64
^
C:\Users\main\Desktop\Programmation\Arduino\tests-wifi.ino\tests-wifi.ino.ino:33:26: note: in expansion of macro 'MOTOR34_1KHZ'
AF_DCMotor motorRight(4, MOTOR34_1KHZ);
^~~~~~~~~~~~
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:50:42: error: 'CS00' was not declared in this scope
#define MOTOR34_1KHZ _BV(CS01) | _BV(CS00) // divide by 64
^
C:\Users\main\Desktop\Programmation\Arduino\tests-wifi.ino\tests-wifi.ino.ino:33:26: note: in expansion of macro 'MOTOR34_1KHZ'
AF_DCMotor motorRight(4, MOTOR34_1KHZ);
^~~~~~~~~~~~
c:\Users\main\OneDrive\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:50:42: note: suggested alternative: 'B100'
#define MOTOR34_1KHZ _BV(CS01) | _BV(CS00) // divide by 64
^
C:\Users\main\Desktop\Programmation\Arduino\tests-wifi.ino\tests-wifi.ino.ino:33:26: note: in expansion of macro 'MOTOR34_1KHZ'
AF_DCMotor motorRight(4, MOTOR34_1KHZ);
^~~~~~~~~~~~
exit status 1
Compilation error: exit status 1
Merci pour votre aide.