I have been trying to write some code for a project where I move a servo based off of the resistance for a soft pot. I am writing it in C++ on an arduino board, and want my servo to move a certain amount for each range of resistance values. Here is my code:
#include <Servo.h>
const int SoftPot = A0;
int RedLow = 3;
int YellowHigh = 2;
Servo Servo1;
int a = 0;
void setup() {
Serial.begin(9600);
pinMode(SoftPot, INPUT);
pinMode(RedLow, OUTPUT);
pinMode(YellowHigh, OUTPUT);
Servo1.attach(9);
//Servo1.write(a);
}
void loop() {
int SoftPotADC = analogRead(SoftPot);
int SoftPotPos = map(SoftPotADC, 0, 1023, 0, 1023);
/* if(SoftPotPos <= 500) digitalWrite(RedLow, HIGH);
else digitalWrite(YellowHigh, HIGH);
*/
if(SoftPotPos <= 70){
Servo1.write(a);
}
else if(70 < SoftPotPos <= 300){
Servo1.write(a + 30);
}
else if (300 < SoftPotPos <= 500){
Servo1.write(a + 60);
}
else if(500 < SoftPotPos <= 800){
Servo1.write(a + 90);
}
else if(800 < SoftPotPos <= 1000){
Servo1.write(a + 120);
}
Serial.println(SoftPotPos);
}
I would really appreciate your feedback! I have only nascent theories about what the problem could be.
Source: Windows Questions C++