here is the code i have been trying to understand
// Robot test via aREST + WiFi
#define NUMBER_VARIABLES 1
#define NUMBER_FUNCTIONS 5
// Libraries
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <aREST.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
uint8_t servonum1 = 0;
uint8_t servonum2 = 1;
uint8_t servonum3 = 2;
#define SERVOMIN 152
#define SERVOMAX 575
// CC3000 pins
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
// 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 "HOME-8970"
#define WLAN_PASS "lkasdfhvgjnajokvnasdvgp;oijash#"
#define WLAN_SECURITY WLAN_SEC_WPA2
// Variable to be exposed to the API
int distance;
void setup(void)
{
// Start Serial
Serial.begin(9600);
Serial.begin(115200);
// Give name to robot
rest.set_id("1");
rest.set_name("robot");
// Expose variables to REST API
rest.variable("distance",&distance);
// Expose functions
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
pwm.begin();
restServer.begin();
Serial.println(F("Listening for connections..."));
pwm.setPWMFreq(60);
displayConnectionDetails();
// Init sensor
//dht.begin();
}
void loop() {
// Handle REST calls
Adafruit_CC3000_ClientRef client = restServer.available();
rest.handle(client);
// Check connection
if(!cc3000.checkConnected()){while(1){}}
}
// Forward
int forward(String command) {
send_motor_command(servonum1, 0, map(0, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum1, 0, map(45, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum1, 0, map(90, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum1, 0, map(135, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum1, 0, map(180, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(0, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(45, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(90, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(135, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(180, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(0, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(45, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(90, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(135, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(180, 0, 180, SERVOMIN, SERVOMAX));
return 1;
}
// Backward
int backward(String command) {
send_motor_command(servonum1, 0, map(180, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum1, 0, map(135, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum1, 0, map(90, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum1, 0, map(45, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum1, 0, map(0, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(180, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(135, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(90, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(45, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(0, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(180, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(135, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(90, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(45, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(0, 0, 180, SERVOMIN, SERVOMAX));
return 1;
}
// Left
int left(String command) {
send_motor_command(servonum1, 0, map(0, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(0, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(0, 0, 180, SERVOMIN, SERVOMAX));
return 1;
}
// Right
int right(String command) {
send_motor_command(servonum1, 0, map(180, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(180, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(180, 0, 180, SERVOMIN, SERVOMAX));
return 1;
}
// Stop
int stop(String command) {
send_motor_command(servonum1, 0, map(90, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum2, 0, map(90, 0, 180, SERVOMIN, SERVOMAX));
send_motor_command(servonum3, 0, map(90, 0, 180, SERVOMIN, SERVOMAX));
return 1;
}
// Function to command a given motor of the robot
void send_motor_command(int speed_pin, int direction_pin, int pwm, boolean dir)
{
analogWrite(speed_pin,pwm); // Set PWM control, 0 for stop, and 255 for maximum speed
digitalWrite(direction_pin,dir);
}
}
// Print connection details of the CC3000 chip
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;
}
}
The code above does not compile at the moment so i don't know how it interacts with the arduino and the tablet yet. An earlier version of the code above that i tampered with didn't print of details in the serial monitor,(yes it was on the correct baud 115200). The earlier code that i tampered with didn't connect to my tablet and did not move the servos at all when i typed the command that the tutorial instructed. here is the tutorial.
Here is the working code just for the servos,(only one servo is activated in the code since i was trying to learn how to change the speed of the servo movement).
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
uint8_t servonum3 = 2;
#define SERVOMIN 152
#define SERVOMAX 575
void setup() {
Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(60);
}
void loop() {
pwm.setPWM(servonum3, 0, map(0, 0, 180, SERVOMIN, SERVOMAX));
delay(10000);
pwm.setPWM(servonum3, 0, map(90, 0, 180, SERVOMIN, SERVOMAX));
delay(10000);
pwm.setPWM(servonum3, 0, map(180, 0, 180, SERVOMIN, SERVOMAX));
delay(10000);
pwm.setPWM(servonum3, 0, map(90, 0, 180, SERVOMIN, SERVOMAX));
delay(10000);
}
What i want, is to be able to control the servos when a given input from the tablet is received by the arduino via wifi. I don't know of any apps that i can use with this but most of the input from the tablet will come from the ipega controller i have paired to the tablet. http://shop.brando.com/prod_img/zoom/SGAME000700_6.jpg
I just don't know how to translate any of what i want into code and need help.