tdogb
September 29, 2016, 12:24pm
1
Hey so I am trying to make a servo stop moving after the pressure sensor hits a threashold. I have been using many for and if loops and it seems very inefficiant,
fsrReadingPoint: current pressure of pressure sensor
TouchValMax: threashold
void checkoktogo() {
if (fsrReadingPoint>TouchValMax) {
oktogoPoint = false;
}
else{
oktogoPoint = true;
}
Then I have it move the servos forward 1 degree at a time until the threashold is met:
void closehand(){
if(oktogoPoint=true){
for(int lerpsPoint = 0; lerpsPoint<180;lerpsPoint++) {
myservo_Point.write(lerpsPoint);
if (fsrReadingPoint>TouchValMax) {
break;
}
}
}
Closehand(); is called from within the loop.
I am not sure if this code works, but I feel it will be limited by arduino's number of threads.
BTW: I have 5 pressure sensors and 5 servos, I just included the code for one of them here.
system
September 29, 2016, 12:37pm
2
There's no such thing as an "if loop", and the Arduino doesn't support threads.
If you want help, post code.
if(oktogoPoint=true)
Oops
tdogb
September 29, 2016, 5:22pm
4
AWOL:
There's no such thing as an "if loop", and the Arduino doesn't support threads.
If you want help, post code.
if(oktogoPoint=true)
Oops
I didn't mean to say if loops in my first post
Full code:
#include <Servo.h>
#define THRESHOLD 150 //In order to determine the state of the hand (opened/closed)
#define EMGPIN 0 //Analog pin connected to Muscle Sensor V3 Board
const byte fsrPinPoint = 0; // the FSR and 10K pulldown are connected to a0
const byte fsrPinMid = 1;
const byte fsrPinRing = 2;
const byte fsrPinPinky = 3;
const byte fsrPinThumb = 4;
int fsrReadingPoint; // the analog reading from the FSR resistor divider
int fsrReadingMid;
int fsrReadingRing;
int fsrReadingPinky;
int fsrReadingThumb;
float servopos;
float MuscleReading;
float previousNum;
float Muscvalue;
boolean oktogoPoint;
boolean oktogoMid;
boolean oktogoRing;
boolean oktogoPinky;
boolean oktogoThumb;
const int TouchValMax = 1000;
Servo myservo_Point; //Pointer
Servo myservo_Mid; //Middle
Servo myservo_Ring; //Ring
Servo myservo_Pinky; //Pinky
Servo myservo_Thumb; //Thumb
void setup() {
fsrReadingPoint = analogRead(fsrPinPoint);
fsrReadingMid = analogRead(fsrPinMid);
fsrReadingRing = analogRead(fsrPinRing);
fsrReadingPinky = analogRead(fsrPinPinky);
fsrReadingThumb = analogRead(fsrPinThumb);
previousNum=0;
MuscleReading = 150;
myservo_Point.attach(9);
myservo_Mid.attach(10);
myservo_Ring.attach(11);
myservo_Pinky.attach(6);
myservo_Thumb.attach(5);
}
void checkoktogo() {
//
if (fsrReadingPoint>TouchValMax) {
oktogoPoint = false;
}
else{
oktogoPoint = true;
}
//
//
if (fsrReadingMid>TouchValMax) {
oktogoMid = false;
}
else{
oktogoMid = true;
}
//
//
if (fsrReadingRing>TouchValMax) {
oktogoRing = false;
}
else{
oktogoRing = true;
}
//
//
if (fsrReadingPinky>TouchValMax) {
oktogoPinky = false;
}
else{
oktogoPinky = true;
}
//
//
if (fsrReadingThumb>TouchValMax) {
oktogoThumb = false;
}
else{
oktogoThumb = true;
}
//
}
void loop() {
int Muscvalue = analogRead(EMGPIN); //Sampling analog signal
checkoktogo();
if(Muscvalue>THRESHOLD) { //If the value of the sample is greater than THRESHOLD means that the hand has been closed
closehand();
}
else {
openhand();
}
}
void closehand(){
if(oktogoPoint=true){
for(int lerpsPoint = 0; lerpsPoint<180;lerpsPoint++) {
myservo_Point.write(lerpsPoint);
if (fsrReadingPoint>TouchValMax) {
break;
}
}
}
if(oktogoMid=true){
for(int lerpsMid = 0; lerpsMid<180;lerpsMid++) {
myservo_Mid.write(lerpsMid);
if (fsrReadingMid>TouchValMax) {
break;
}
}
}
if(oktogoRing=true){
for(int lerpsRing = 0; lerpsRing<180;lerpsRing++) {
myservo_Ring.write(lerpsRing);
if (fsrReadingRing>TouchValMax) {
break;
}
}
}
if(oktogoPinky=true){
for(int lerpsPinky = 0; lerpsPinky<180;lerpsPinky++) {
myservo_Pinky.write(lerpsPinky);
if (fsrReadingPinky>TouchValMax) {
break;
}
}
}
if(oktogoThumb=true){
for(int lerpsThumb = 0; lerpsThumb<180;lerpsThumb++) {
myservo_Thumb.write(lerpsThumb);
if (fsrReadingThumb>TouchValMax) {
break;
}
}
}
}
void openhand(){
myservo_Pointer.write(0);
myservo_Mid.write(0);
myservo_Ring.write(0);
myservo_Pinky.write(0);
myservo_Thumb.write(0);
}
}
}
Still need two ='s in these for a comparison test:
if(oktogoPoint=true){