BLE issues, Code integration, can EvoThings help?
Posted: 07:05, 16 May 2016
Having trouble getting the Arduino code to compile with included scripts.
I'm using the Adafruit nRF8001 breakout board V1 and the Adafruit Motor Shield V2.
When I try to use the included ino files it craps out on me saying it can not compile for board.
I have all of the board specific libraries included.
Here is a link to the board I'm using. https://www.adafruit.com/product/1697
The app controls 4 Trains with the 4th having the option of Trolley mode with delay switch.
Trying to get a mobile app working that will connect to arduino using BLE, write variables to arduino that are obtained from 4 sliders <0-255> and 4 Switches <0-1> There are 2 additional switches that change train 4 into a trolley and set a delay.
Here is my existing code that works with hard wired POTs and switches.
Here is my code that lets the BLE discovery app to connect and see services. Think I'm making some progress but still do not know how to get a EvoThings app to populate variables. as you can see, I only was able to get the broadcast stuff down in the following. I have no idea how to build the app with sliders and switches and how to integrate those values in place of the ones that are hardwired in with physical pots and switches.
Please help, if I don't get this working, I'll prob be looking for another employer.
Any help is greatly appreciated, I'll owe ya.
Thanks
I'm using the Adafruit nRF8001 breakout board V1 and the Adafruit Motor Shield V2.
When I try to use the included ino files it craps out on me saying it can not compile for board.
I have all of the board specific libraries included.
Here is a link to the board I'm using. https://www.adafruit.com/product/1697
The app controls 4 Trains with the 4th having the option of Trolley mode with delay switch.
Trying to get a mobile app working that will connect to arduino using BLE, write variables to arduino that are obtained from 4 sliders <0-255> and 4 Switches <0-1> There are 2 additional switches that change train 4 into a trolley and set a delay.
Here is my existing code that works with hard wired POTs and switches.
Code: Select all
#include <Wire.h>
#include <Adafruit_MotorShield.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
//Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60);
// Select which 'port' M1, M2, M3 or M4. This controls the Trains attached to the ports defined.
Adafruit_DCMotor *Train1 = AFMS.getMotor(1);
Adafruit_DCMotor *Train2 = AFMS.getMotor(2);
Adafruit_DCMotor *Train3 = AFMS.getMotor(3);
Adafruit_DCMotor *Train4 = AFMS.getMotor(4);
// Train Motor Array
Adafruit_DCMotor *trains[4] = {Train1, Train2, Train3, Train4};
//Define POT Speed controls
int trainSpeeds[4] = {A0, A1, A2, A3};
// Train state array
int trainStates[4];
//Define Switches that control Direction of Trains
int DirectionSW1 = 5;
int DirectionSW2 = 6;
int DirectionSW3 = 7;
int DirectionSW4 = 8;
//Direction Switch Array
int switches[4] = { DirectionSW1, DirectionSW2, DirectionSW3, DirectionSW4};
// Define switch for Trolley Mode
// TRUE=Trolley mode, FALSE=Train mode
int TrolleyModeSwitch = 4;
//Define Switch for Trolley Delay
// TRUE=30 second delay, FALSE=60 second delay
int TrolleyDelaySwitch = 7;
// define sensible name for trolley mode
#define TROLLEYMODE LOW
void setup() {
// Setup Serial library at 9600 bps
Serial.begin(9600);
//Setup internal PULLUP for trains
pinMode(DirectionSW1,INPUT_PULLUP);
pinMode(DirectionSW2,INPUT_PULLUP);
pinMode(DirectionSW3,INPUT_PULLUP);
pinMode(DirectionSW4,INPUT_PULLUP);
// PULLUP mode for Trolley Switches
pinMode(TrolleyModeSwitch ,INPUT_PULLUP);
pinMode(TrolleyDelaySwitch,INPUT_PULLUP);
// create with the default frequency 1.6KHz
AFMS.begin();
}
void loop()
{
// Loop through trains (trainArray starts at 0)
for (int trainIndex = 0; trainIndex < 4; trainIndex++)
{
// read the speed from the potentiometer (and divide by 4)
int speed = analogRead(trainSpeeds[trainIndex]) / 4;
// get the direction from the switch
int direction = digitalRead(switches[trainIndex]);
if (trainIndex < 3 || (trainIndex == 3 && digitalRead(TrolleyModeSwitch) != TROLLEYMODE))
{
controlTrain(trainIndex, direction, speed);
}
else
{
doTrolley(trainIndex, speed);
}
}
}
/*
set trainspeed and direction for train specified by index
*/
void controlTrain(int index, int direction, int speed)
{
// set direction
if (direction == LOW)
trains[index]->run(FORWARD);
else
trains[index]->run(BACKWARD);
// set speed
trains[index]->setSpeed(speed);
}
/*
delay for number of milliseconds
returns false if delay in progress, else true
*/
bool doDelay(unsigned long duration)
{
// remember the start time of the delay; 0 means that delay is not started
static unsigned long starttime = 0;
if (starttime == 0)
{
// set the start time
starttime = millis();
}
else
{
// if delay lapsed
if (millis() - starttime > duration)
{
// reset start time
starttime = 0;
// indicate that delay has lapsed
return true;
}
}
// indicate delay is in progress
return false;
}
/*
statemachine for trolley mode
*/
void doTrolley(int index, int speed)
{
// remember what trolley is doing
static int trolleystate = 0;
// remember trolley direction
static int trolleydirection = LOW;
// duration of delay to set runtime
unsigned long delayduration;
if(digitalRead(TrolleyDelaySwitch) == HIGH)
{
delayduration = 30000;
}
else
{
delayduration = 60000;
}
switch (trolleystate)
{
case 0:
controlTrain(index, trolleydirection, speed);
if (doDelay(delayduration))
{
// done with this state
trolleystate++;
}
break;
case 1:
// change direction
if (trolleydirection == LOW)
{
trolleydirection = HIGH;
}
else
{
trolleydirection = LOW;
}
// done with this state, back to first state
trolleystate = 0;
break;
}
}
Here is my code that lets the BLE discovery app to connect and see services. Think I'm making some progress but still do not know how to get a EvoThings app to populate variables. as you can see, I only was able to get the broadcast stuff down in the following. I have no idea how to build the app with sliders and switches and how to integrate those values in place of the ones that are hardwired in with physical pots and switches.
Code: Select all
#include <Adafruit_MotorShield.h>
#include <Wire.h>
// This version uses the internal data queing so you can treat it like Serial (kinda)!
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' M1, M2, M3 or M4. This controls the Trains attached to the ports defined.
Adafruit_DCMotor *Train1 = AFMS.getMotor(1);
Adafruit_DCMotor *Train2 = AFMS.getMotor(2);
Adafruit_DCMotor *Train3 = AFMS.getMotor(3);
Adafruit_DCMotor *Train4 = AFMS.getMotor(4);
// Train Motor Array
Adafruit_DCMotor *trains[4] = {Train1, Train2, Train3, Train4};
//Define POT Speed controls
int trainSpeeds[4] = {A0, A1, A2, A3};
// Train state array
int trainStates[4];
//Define Switches that control Direction of Trains
int DirectionSW1 = 5;
int DirectionSW2 = 6;
int DirectionSW3 = 7;
int DirectionSW4 = 8;
//Direction Switch Array
int switches[4] = { DirectionSW1, DirectionSW2, DirectionSW3, DirectionSW4};
// Define switch for Trolley Mode
// TRUE=Trolley mode, FALSE=Train mode
int TrolleyModeSwitch = 4;
//Define Switch for Trolley Delay
// TRUE=30 second delay, FALSE=60 second delay
int TrolleyDelaySwitch = 7;
// define sensible name for trolley mode
#define TROLLEYMODE LOW
// Connect CLK/MISO/MOSI to hardware SPI
// e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2 // This should be an interrupt pin, on Uno thats #2 or #3
#define ADAFRUITBLE_RST 9
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
/**************************************************************************/
/*!
Configure the Arduino and start advertising with the radio
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(9600);
while(!Serial); // Leonardo/Micro should wait for serial init
Serial.println(F("Adafruit Bluefruit Low Energy nRF8001 Print echo demo"));
// BTLEserial.setDeviceName("NEWNAME"); /* 7 characters max! */
BTLEserial.begin();
//Setup internal PULLUP for trains
pinMode(DirectionSW1,INPUT_PULLUP);
pinMode(DirectionSW2,INPUT_PULLUP);
pinMode(DirectionSW3,INPUT_PULLUP);
pinMode(DirectionSW4,INPUT_PULLUP);
// PULLUP mode for Trolley Switches
pinMode(TrolleyModeSwitch ,INPUT_PULLUP);
pinMode(TrolleyDelaySwitch,INPUT_PULLUP);
// create with the default frequency 1.6KHz
AFMS.begin();
}
/**************************************************************************/
/*!
Constantly checks for new events on the nRF8001
*/
/**************************************************************************/
aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;
void loop()
{
// Tell the nRF8001 to do whatever it should be working on.
BTLEserial.pollACI();
// Ask what is our current status
aci_evt_opcode_t status = BTLEserial.getState();
// If the status changed....
if (status != laststatus) {
// print it out!
if (status == ACI_EVT_DEVICE_STARTED) {
Serial.println(F("* Advertising started"));
}
if (status == ACI_EVT_CONNECTED) {
Serial.println(F("* Connected!"));
}
if (status == ACI_EVT_DISCONNECTED) {
Serial.println(F("* Disconnected or advertising timed out"));
}
// OK set the last status change to this one
laststatus = status;
}
// Loop through trains (trainArray starts at 0)
for (int trainIndex = 0; trainIndex < 4; trainIndex++)
{
// read the speed from the potentiometer (and divide by 4)
int speed = analogRead(trainSpeeds[trainIndex]) / 4;
// get the direction from the switch
int direction = digitalRead(switches[trainIndex]);
if (trainIndex < 3 || (trainIndex == 3 && digitalRead(TrolleyModeSwitch) != TROLLEYMODE))
{
controlTrain(trainIndex, direction, speed);
}
else
{
doTrolley(trainIndex, speed);
}
}
}
/*
set trainspeed and direction for train specified by index
*/
void controlTrain(int index, int direction, int speed)
{
// set direction
if (direction == LOW)
trains[index]->run(FORWARD);
else
trains[index]->run(BACKWARD);
// set speed
trains[index]->setSpeed(speed);
}
/*
delay for number of milliseconds
returns false if delay in progress, else true
*/
bool doDelay(unsigned long duration)
{
// remember the start time of the delay; 0 means that delay is not started
static unsigned long starttime = 0;
if (starttime == 0)
{
// set the start time
starttime = millis();
}
else
{
// if delay lapsed
if (millis() - starttime > duration)
{
// reset start time
starttime = 0;
// indicate that delay has lapsed
return true;
}
}
// indicate delay is in progress
return false;
}
/*
statemachine for trolley mode
*/
void doTrolley(int index, int speed)
{
// remember what trolley is doing
static int trolleystate = 0;
// remember trolley direction
static int trolleydirection = LOW;
// duration of delay to set runtime
unsigned long delayduration;
if(digitalRead(TrolleyDelaySwitch) == HIGH)
{
delayduration = 30000;
}
else
{
delayduration = 60000;
}
switch (trolleystate)
{
case 0:
controlTrain(index, trolleydirection, speed);
if (doDelay(delayduration))
{
// done with this state
trolleystate++;
}
break;
case 1:
// change direction
if (trolleydirection == LOW)
{
trolleydirection = HIGH;
}
else
{
trolleydirection = LOW;
}
// done with this state, back to first state
trolleystate = 0;
break;
}
}
Please help, if I don't get this working, I'll prob be looking for another employer.
Any help is greatly appreciated, I'll owe ya.
Thanks