I am running a program to calculate cadence from a hall effect sensor, and would like to send instructions to a servo motor based on that data.
A similar problem was found here: https://forum.arduino.cc/index.php?topic=362521.0 but I could not extract a viable solution from it (I tried renaming the files they specified, but they were not found on my computer).
Right now I have only wired my Galileo to a simple circuit consisting of the hall effect sensor and an LED (the LED has not yet been programmed into the circuit). I also have not yet attached the servo to my circuit. I just wanted to see if the code would upload to my board so that I could test the sensor. This is my first time using a Galileo with my Arduino IDE.
This is my error message:
“Invalid library found in C:\Users\Vivienne\AppData\Local\Arduino15\packages\Intel\hardware\i586\1.6.2+1.0\libraries\Wire: C:\Users\Vivienne\AppData\Local\Arduino15\packages\Intel\hardware\i586\1.6.2+1.0\libraries\Wire”
/* Adapted by Vivienne Jaehn-Kreibaum
* from "Bike better with an LED cadence meter" by jessejwk on Instructables
* Source: http://www.instructables.com/id/Bike-better-with-an-LED-cadence-meter/?ALLSTEPS
* Date: March 6, 2016
* IGEN 230 Term Project - PerfectGear
*/
#include <Servo.h>
#define ARRAY_SIZE 3 //The number of values to keep in the running average
#define TIMEOUT 1500 //Max time without input
/**PORT SETUP**/
const int magnetSensor = A7; //Change this for your arduino
const int ledPin = 11;
const int threshold = 60;
const int shiftspeed = 180; // arbitrary value, will need to confirm with testing
bool magnetOn = false;
bool prevMagOn = false;
bool primed = false;
bool arrayEmpty = false;
float times[ARRAY_SIZE]; //array to hold values for the running average
float startTime = millis();
int rpm = 0;
bool pedaling = false;
Servo myservo;
//add a new value to the array, moving the rest back one space and removing the oldest
void updateTimes(float newValue)
{
for(int i = 0; i < ARRAY_SIZE-1; i++) {
times[i] = times[i+1];
}
times[ARRAY_SIZE-1] = newValue;
}
//returns an average of the values in the array
int avgArray(float values[]) {
int total = 0;
int counted = ARRAY_SIZE;
for(int i = 0; i < ARRAY_SIZE; i++) {
total = total + values[i];
if(values[i] == 0)
counted--;
}
return(total/counted);
}
//for debugging
void printValues() {
for(int i = 0; i < ARRAY_SIZE - 1; i++) {
Serial.print(times[i]);
Serial.print(", ");
}
Serial.println(times[ARRAY_SIZE -1]);
}
//clear the array
void clearTimes() {
for(int i = 0; i < ARRAY_SIZE; i++)
times[i] = 0;
}
//check if the array is full
void checkFullArray() {
arrayEmpty = true;
for(int i = 0; i < ARRAY_SIZE; i++) {
if(times[i] != 0)
arrayEmpty = false;
}
}
// send instruction to servo to change gears
void changeGears(int curr, int next){
myservo.write(shiftspeed); // put the necessary speed in here
}
void setup() {
Serial.begin(9600);
myservo.attach(9); // attached to pin 9
}
void loop() {
//Read from the hall effect sensor (using analog values, unfortunately)
int magnetState = analogRead(magnetSensor);
Serial.println(magnetState);
if(magnetState > threshold) {
magnetOn = false;
}
else {
magnetOn = true;
}
if(!magnetOn && prevMagOn) {
primed = true;
}
//timeout
if(millis()-startTime > TIMEOUT) {
clearTimes();
pedaling = false;
}
if(magnetOn && !prevMagOn && primed) { //if magnet passes sensor once
float currentTime = millis();
float changeTime = (currentTime - startTime); //record the time since the last pedal
startTime = millis();
if(pedaling) //if there has been pedaling since the last timeout
updateTimes(changeTime); //add the time to the running average array
primed = false;
pedaling = true;
}
//mostly for debugging, this blinks the built-in LED whenever the magnet passes the sensor
if(magnetOn)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
prevMagOn = magnetOn;
checkFullArray();
float gap = avgArray(times);
if(arrayEmpty) {
rpm = 0;
}
else
rpm = 60000/gap; //turn millisecond gap value into rpm
}
Thank you for your help!