Hey,
So I fixed it and it’s still running full speed ahead on boot-up just as the board’s instructions thought it would do if it didn’t have anything recognizably connected.
I’ve tried turning board on and letting it connect to the cloud before turning the board on, same problem.
Here’s my code
Spark Core Code:
Servo left; // create servo object to control a servo
Servo right;
#define PIN D7
// Load hash map up with values
int leftpin = A0;
int rightpin = A1;
int speedx = 90;
int pos = 90; // for motion
int eyeSignal = A4;
int headSignal = A5;
void setup()
{
left.attach(leftpin); // attaches the servo on the A0 pin to the servo object
right.attach(rightpin); // attaches the servo on the A0 pin to the servo object
//speed adjusting forward
Spark.function("forward", Forward); //setpos // setPosition // original
//Backward default
Spark.function("backward", Backward);
Spark.function("turnright", TurnRight);
Spark.function("turnleft", TurnLeft);
//initialize off
pinMode(PIN, OUTPUT);
}
void loop()
{
digitalWrite(PIN, HIGH);
myDelay(1000); // give time to receive messages!
digitalWrite(PIN, LOW);
}
//Motion Functions
int Forward(String speed){
//change value to int from string because string can only be passed to function
speedx = speed.toInt();
left.write(speedx);
right.write(speedx);
return 1;
}
int Backward(String speed){
speedx = speed.toInt();
left.write(convertBackSpeed(speedx)); // must be > 90
right.write(convertBackSpeed(speedx));
return 1;
}
int TurnLeft(String speed){
speedx = speed.toInt();
left.write(speedx);
right.write(convertBackSpeed(speedx));
return 1;
}
int TurnRight(String speed){
speedx = speed.toInt();
left.write(convertBackSpeed(speedx));
right.write(speedx);
return 1;
}
//Convert forward to backwards!
int convertBackSpeed(int value){
//value = 90 70 80 60 45 35
//convert back to 0 to 5
int clean[90] = {90};
clean[80] = {110};
clean[70] = {120};
clean[65] = {130};
clean[50] = {135};
clean[40] = {140};
return clean[value];
}
//Allow time to receive messages
void myDelay(unsigned long duration) {
unsigned long start = millis();
while (millis() - start <= duration){
Spark.process();
}
}
HTML / Javascript:
<!--
best version here
–>
Command App:
<!-- form here
We need to map 90 to 30 : 0 mph to 5 mph or 12 pts = 1 mph 6 pts = .5 mph 3 pts = .25 mph 1.5 pts = .125 mph
We then need to map 0 mph back to 90 - 30 for forward or 90 - 180 for backward function
-->
<input type="range" name="degBox" id="degBoxId" min="0" max="5" step="1" value="0" list="myData">
<!-- This adds the tick marks to the range but does not in Safari
Changed 0 from 90in value
-->
<datalist id="myData">
<option value="5">
<option value="4">
<option value="3">
<option value="2">
<option value="1">
<option value="0">
</datalist>
<p><i>0-- 1-- 2---3 ---4 --5</i></p>
<br>
<button id="minusbutton">⇐ -1 °</button>
<button id="plusbutton">+1 ° ⇒</button>
<br><br>
<P>MPH: <span id="curPos"></span><br>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<!-- Javascript server side code -->
<script type="text/javascript">
//set credentials
// set functions to call
var deviceID = "53ff6e066667574845592267";
var accessToken = "6d3faea8e2c0d23c4fb78b1ca846c619855010ae";
var speed = 90; // default speed changed from 40
var currentCommand = '';
// USELESS
var fwd = "forward";
// DONT NEED THESE //
var tr = "turnright";
var tl = "turnleft";
var getFunc = "getpos";
var setFunc = "setpos";
// DONT NEED THESE //
//Mapping
// 0 mph = 90 degrees pos
// 5 mph = 30 degrees pos
// 1 mph = 12 degrees pos
// .083 mph = 1 degree pos
//Updating speed output
$('#degBoxId').val(speed); // sets any ID with degBoxId to value speed
// If i want to do multiple things use class. i.e. if i have multiple degree boxes and I want to set them.
// use class anywhere I want it to be set.
//Updates now for first update
// ROBOT DRIVE COMMANDS ONLY
var is_keydown = false;
$(‘body’).on(‘keydown’, function (event) { // whole page onclick
if(is_keydown) return // dont do anything while a key is pressed down.
is_keydown = true;
console.log(event.keyCode);
actual_speed = speed; // 0
switch(event.keyCode){
case 40 :
currentCommand = 'backward';
actual_speed = convertToDegrees(speed);
break;
case 38:
currentCommand = 'forward';
actual_speed = convertToDegrees(speed);
break;
case 37:
currentCommand = 'turnleft';
actual_speed = convertToDegrees(speed);
break;
case 39:
currentCommand = 'turnright';
actual_speed = convertToDegrees(speed);
break
case 75:
currentCommand = 'thr';
actual_speed = '33';
break;
case 76:
currentCommand = 'turnheadleft'
actual_speed ='34';
break;
case 77:
currentCommand = 'lookdown'
actual_speed ='33';
break;
case 74:
currentCommand = 'lookup'
actual_speed ='240';
break;
default:
currentCommand = null;
}
if(currentCommand){
var requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + currentCommand + "/";
// execution post of lrequest string
$.post(requestURL, { params: actual_speed, access_token: accessToken });
}
});
$('body').on('keyup', function () {
is_keydown = false;
if(!currentCommand){return}
if(currentCommand === 'turnheadleft' || currentCommand === 'thr' || currentCommand === 'lookup' || currentCommand === 'lookdown'){
var requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + currentCommand + "/";
$.post( requestURL, { params: '0', access_token: accessToken });
}else{
var requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + currentCommand + "/";
$.post( requestURL, { params: '90', access_token: accessToken });
}
});
// END OF ROBOT DRIVE COMMANDS ONLY
//whenever the slider changes, it will update the current position by re-assigning speed value
$('#degBoxId').on('change', function (event) {
speed = event.target.value;
$('#curPos').text(speed);//assigns ID #curPos the newly made speed
});
// decrementing down with fineAdjust function
$('#minusbutton').on('click', function (event) {
fineAdjust(-1);
});
// Incrementing
$('#plusbutton').on('click', function (event) {
fineAdjust(1);
});
// Map mph to degrees of which the control is manipulated through
function convertToDegrees(value){
var map = ['90', '80', '70', '65', '50', '40'];
return map[value];
}
// Used in input form. Gets curPos string element value, changes it to an integer
function fineAdjust(value) {
var setValue = speed + value; //adds last value plus this one
document.getElementById("degBoxId").value = setValue; // set degBoxId element value to setValue DISPLAY
// conversion needs to happen here befoe being given to speed then to function on backend
speed = setValue;
//sp(setValue); //sets current position
}
function oppositeNum(value){
}
</script>