Buon giorno a tutti...Ho un problema con la automobilina wifi che ho costruito...
Dopo aver ridefinito i piedini dei motori 7 e 10 (usati dalla wifi sheild per lavorare) nei piedini 2 e 3 (assolutamente liberi) il motore destro va molto più veloce di quello sinistro nonostante io li setti a pari velocità!
Qualcuno saprebbe aiutarmi? Grazie mille per l'attenzione
/*
-----------------------------------------------------------------
@author Lonzi Mattia
@version 0.1 2014-03
-----------------------------------------------------------------
WiFi Web Server
A simple web server that shows the value of the analog input pins.
using a WiFi shield.
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
Circuit:
* WiFi shield attached
* Analog inputs attached to pins A0 through A5 (optional)
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
*/
#include <SPI.h>
#include <WiFi.h>
#define out_STBY 2 //PIN RIDEFINITO 7
#define out_B_PWM 3 //B motore destro PIN RIDEFINITO 10
#define out_A_PWM 5 //A motore sinistro
#define out_A_IN2 6
#define out_A_IN1 4
#define out_B_IN1 8
#define out_B_IN2 9
#define left_motor 0
#define right_motor 1
//--------------------------------------------------------------
// parametri di configurazione modificabili
//--------------------------------------------------------------
// i parametri seguenti possono essere modificati a piacimento
// per migliorare il comportamento del robot
//
#define attesaPerManovra 60 // tempo di attesa tra il riconoscimento della curva e il comando ai motori
#define turn_speed 30 // velocita' di curvatura (0-100)
char ssid[] = "*****************"; // your network SSID (name)
char pass[] = "*****************"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(3333);
boolean alreadyConnected = false; // whether or not the client was connected previously
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(out_STBY,OUTPUT);
pinMode(out_A_PWM,OUTPUT);
pinMode(out_A_IN1,OUTPUT);
pinMode(out_A_IN2,OUTPUT);
pinMode(out_B_PWM,OUTPUT);
pinMode(out_B_IN1,OUTPUT);
pinMode(out_B_IN2,OUTPUT);
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// start the server:
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
}
void loop() {
// wait for a new client:
WiFiClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clead out the input buffer:
client.flush();
Serial.println("We have a new client");
//client.println("Hello, client!");
alreadyConnected = true;
}
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
Serial.write(thisChar);
if(thisChar == 'a'){
goForward();
Serial.println("AVANTI");
}
if(thisChar == 'd'){
turnRight();
Serial.println("DESTRA");
}
if(thisChar == 'i'){
goBack();
Serial.println("INDIETRO");
}
if(thisChar == 's'){
turnLeft();
Serial.println("SINISTRA");
}
if(thisChar == 'o'){
stopEngine();
Serial.println("STOP");
}
}
}
}
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 the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
// FUNZIONI DEI COMANDI ---------------------------------------
void goForward(){
motor_standby(false);
delay(attesaPerManovra);
set_motor(left_motor,35);
set_motor(right_motor,35);
}
void goBack(){
motor_standby(false);
delay(attesaPerManovra);
set_motor(left_motor,-35);
set_motor(right_motor,-35);
}
void turnLeft(){
motor_standby(false);
delay(attesaPerManovra);
set_motor(left_motor,-turn_speed);
set_motor(right_motor,+turn_speed);
}
void turnRight(){
motor_standby(false);
delay(attesaPerManovra);
set_motor(left_motor,+turn_speed);
set_motor(right_motor,-turn_speed);
}
void stopEngine(){
motor_standby(true);
}
//--------------------------------------------------------------
void set_motor(boolean motor, char speed) { // imposta la velocità tra -100 (indietro) e +100 (avanti)
byte PWMvalue=0;
PWMvalue = map(abs(speed),0,100,50,255);
if (speed > 0)
motor_speed(motor,0,PWMvalue);
else if (speed < 0)
motor_speed(motor,1,PWMvalue);
else {
motor_coast(motor);
}
}
void motor_speed(boolean motor, boolean direction, byte speed) { // imposta la velocità tra 0 e 255
if (motor == left_motor) {
if (direction == 0) {
digitalWrite(out_A_IN1,HIGH);
digitalWrite(out_A_IN2,LOW);
}
else {
digitalWrite(out_A_IN1,LOW);
digitalWrite(out_A_IN2,HIGH);
}
analogWrite(out_A_PWM,speed);
}
else {
if (direction == 0) {
digitalWrite(out_B_IN1,HIGH);
digitalWrite(out_B_IN2,LOW);
}
else {
digitalWrite(out_B_IN1,LOW);
digitalWrite(out_B_IN2,HIGH);
}
analogWrite(out_B_PWM,speed);
}
}
void motor_standby(boolean state) { // abilita/disabilita i motori
if (state == true)
digitalWrite(out_STBY,LOW);
else
digitalWrite(out_STBY,HIGH);
}
void motor_coast(boolean motor) { // motore in folle
if (motor == left_motor) {
digitalWrite(out_A_IN1,LOW);
digitalWrite(out_A_IN2,LOW);
digitalWrite(out_A_PWM,HIGH);
}
else {
digitalWrite(out_B_IN1,LOW);
digitalWrite(out_B_IN2,LOW);
digitalWrite(out_B_PWM,HIGH);
}
}
void motor_brake(boolean motor) { // freno motore
if (motor == left_motor) {
digitalWrite(out_A_IN1,HIGH);
digitalWrite(out_A_IN2,HIGH);
}
else {
digitalWrite(out_B_IN1,HIGH);
digitalWrite(out_B_IN2,HIGH);
}
}