Go Down

Topic: debug my codes plz cc3000+l298n (Read 185 times) previous topic - next topic

noura91

hi

this code of wifi cc3000 and l298n

who can find where are the mistakes in my codes the connection in serial monitor shows its fine and connect but when i gave it a command forward or backward its not moving

Code: [Select]
// Libraries
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <aREST.h>
#include <avr/wdt.h>


// CC3000 pins
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  8
#define ADAFRUIT_CC3000_CS    10


const int IN1=4;
const int IN2=5;
const int IN3=6;
const int IN4=7;

// CC3000 instance
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT);

// Create aREST instance
aREST rest = aREST();

// The port to listen for incoming TCP connections
#define LISTEN_PORT           80

// Server instance
Adafruit_CC3000_Server restServer(LISTEN_PORT);

#define WLAN_SSID       "i put my wifi name"
#define WLAN_PASS       "my pass"
#define WLAN_SECURITY   WLAN_SEC_WPA2

void setup(void) {
 
   // Start Serial
  Serial.begin(115200);
 
  // Give name to robot
  rest.set_id("1");
  rest.set_name("robot");
 
  // Expose variables to REST API
 // rest.variable("distance",&distance);
  // put your setup code here, to run once:
 rest.function("forward",FORWARD);
  rest.function("backward",BACKWARD);
  rest.function("left",LEFT);
  rest.function("right",RIGHT);
  rest.function("stop",Stop);
  // Set up CC3000 and get connected to the wireless network.
  if (!cc3000.begin())
  {
    while(1);
  }
 
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    while(1);
  }
  while (!cc3000.checkDHCP())
  {
    delay(100);
  }
   
  // Start server
  restServer.begin();
  Serial.println(F("Listening for connections..."));
 
  displayConnectionDetails();
 
  // Init sensor
  //dht.begin();
 
  wdt_enable(WDTO_4S);
}


void loop() {
  // put your main code here, to run repeatedly:
  // Handle REST calls
  Adafruit_CC3000_ClientRef client = restServer.available();
  rest.handle(client);
  wdt_reset();
 
  // Check connection
  if(!cc3000.checkConnected()){while(1){}}
  wdt_reset();

}

int FORWARD(String command){
 
 
  analogWrite(IN1,0);
  analogWrite(IN2,200);
  analogWrite(IN3,200);
  analogWrite(IN4,0);
  return 1;
}
int BACKWARD(String command){
 
  analogWrite(IN1,200);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,200);
    return 1;

}

int RIGHT(String command){
 
  analogWrite(IN1,200);
  analogWrite(IN2,0);
  analogWrite(IN3,150);
  analogWrite(IN4,0);
    return 1;

}

int LEFT(String command ){
 
  analogWrite(IN1,0);
  analogWrite(IN2,150);
  analogWrite(IN3,0);
  analogWrite(IN4,200);
    return 1;

}

int Stop(String command){
 
  analogWrite(IN1,0);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,0);
    return 1;

}

bool displayConnectionDetails(void)
{
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
 
  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }
  else
  {
    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
    Serial.println();
    return true;
  }
}

noura91

if no one know can you suggest a websites can help me plz

Anders53

#2
Mar 13, 2015, 09:18 pm Last Edit: Mar 13, 2015, 09:23 pm by Anders53
Hi -

As an old (& rusty) programmer, IMHO the best way to debug a program, that looks and compiles fine no errors, is to strictly follow the instructions in your code manually :
Very patiently step by step, one instruction at a time.

Make up a table of all (and I mean ALL !) variables and pins used.
At each step in your code, fill your table with applicable values and pin states, using pen & paper.

I guarantee, that after a few instructions, you will find either an error or mistake in the logic method, i.e. the idea, behind your code.
OR a problem (limitation) in your hardware.

Also remember :
Fill your code with comments on what the code is intended to do.
This is especially important and useful, if you are a beginner (or a rusty old gent like me ...)   ;)

noura91

everything is right in my codes there is not any errors even the connection is ok but only when i gave command of forward right...etc its not accepted and nothing moves

AWOL

I'm not a fan of aRest, but maybe try some debug prints in the library?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

noura91

ok do u know another way to control the robot via wifi  :(

noura91

ok this another codes i wrote it the connection is ok but when i write the command in the serial monitor
of F - S - R - L - B its only accept the first command ex: i put F in serial monitor as i first direction but when i command B as a second it dosnt response

plz i need just correction i dont want to pay for some one to correct my codes and i knew its right

Code: [Select]

 #include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include <stdlib.h>

#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  8
#define ADAFRUIT_CC3000_CS    10

String result;


const unsigned long
  dhcpTimeout     = 60L * 1000L, // Max time to wait for address from DHCP
  connectTimeout  = 15L * 1000L, // Max time to wait for server connection
  responseTimeout = 15L * 1000L; // Max time to wait for data from server
uint32_t t;

#define WLAN_SSID       "AFAQShamel"        // cannot be longer than 32 characters!
#define WLAN_PASS       "0508344390"
#define WLAN_SECURITY   WLAN_SEC_WPA2 // This can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2);
#define LISTEN_PORT 8888
Adafruit_CC3000_Server robotServer(LISTEN_PORT);


const int IN1=4;
const int IN2=5;
const int IN3=6;
const int IN4=7;


void setup()
{
   
pinMode( IN1 ,OUTPUT);// Right Motor 1st wire
pinMode( IN2 ,OUTPUT);// Right Motor 2nd wire
pinMode( IN3 ,OUTPUT);// left Motor 1st wire
pinMode( IN4 ,OUTPUT);// left Motor 2nd wire
Serial.begin(115200);
  result = "";
 Serial.println(F("\nInitializing..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }
   
  Serial.println(F("Connected!"));
 
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  } 

  /* Display the IP address DNS, Gateway, etc. */
 
  while (! displayConnectionDetails()) {
    delay(1000);
  }
 
  // Start listening for connections
  robotServer.begin();
 
  Serial.println(F("Listening for connections..."));
 

}

void loop() {
 
    Adafruit_CC3000_ClientRef client = robotServer.available();
  if (client) {
     boolean currentLineIsBlank = true;
     // Check if there is data available to read.
     while (client.available()) {
     
       char c = client.read();
       result = result + c;
       Serial.write(c);
       
       // Delete HTTP headers
      if(result.endsWith("Content-Type: text/html"))
      {
        result="";
      }
       
       if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();         
       }
       if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
       
     }
     // give the web browser time to receive the data
    delay(5);
    // close the connection:
    client.close();
    Serial.println("client disconnected");
   
    // Format result and extract the variables
   
     result = "";
  }
     
  char input;

  // check if data has been sent from the computer:
 
    // switch based on the character
    if (Serial.available()) {
    // read the most recent character
   input = Serial.read();
    // switch based on the character
    switch(input){
      case 'F':
        // forward
  analogWrite(IN1,0);
  analogWrite(IN2,255);
  analogWrite(IN3,255);
  analogWrite(IN4,0);
   break;
 
      case 'B':  // backwards
           // v=Serial.read();
   analogWrite(IN1,200);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,200);
   break;
      case 'S':  // stop
        analogWrite(IN1,0);
  analogWrite(IN2,0);
  analogWrite(IN3,0);
  analogWrite(IN4,0);
        break;
      case 'L':  // left
       
  analogWrite(IN1,0);
  analogWrite(IN2,150);
  analogWrite(IN3,0);
  analogWrite(IN4,200);
        break;
      case 'R':  // right
 
  analogWrite(IN1,150);
  analogWrite(IN2,LOW);
  analogWrite(IN3,200);
  analogWrite(IN4,LOW);
        break;
      default:    // if character not recognized then stop
        analogWrite(IN1, 0);
        analogWrite(IN3, 0);
        break;
    }
  }
}

   
    bool displayConnectionDetails(void){

  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
 
  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }
  else
  {
    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
    Serial.println();
    return true;
  }

    }

Go Up