Hi guys,
I have this really complicated problem. I want to read the value of a potentio meter and store them in an Array to be able to save multiple values from the potentio meter.
Then I want my vibrating motor to vibrate once when the potentio meter is within range of 50 of this stored value.
The problem is that i want to vibrate my motor once when the potentio meter is in range, i use the int motorRun for this. It works once, but i do not know where i can make the Motorrun 0 again to let him vibrate again when the potentio meter is out of range.
I use this for loop to make the motor vibrate once :
for (i = 0; i < 10; i = i + 1) {
int difVal = abs(Coordinates[i] - potValue);
potValue = analogRead(A0);
if (difVal <=tolerance && motorRun == 0 ) {
digitalWrite(vibPin, HIGH);
delay(100);
digitalWrite(vibPin, LOW);
Serial.println("In range");
motorRun= 1;
}
else {
motorRun= 0;
Serial.println("Out of range");
}
But the value is almost always out of range due to the for loop keeps running. So my question is : where can i make the motorRun back 0 again so that my motor vibrates once when I am in range, and let him be able to vibrate again in another range ?
My whole code:
int Coordinates[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int tolerance = 50;
const int buttonPin = 4;
const int vibPin = 7;
int buttonState = 0;
int potValue = 9999;
int i;
int led = 3;
int motorRun= 0;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(vibPin, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
potValue = analogRead(A0);
Serial.println(potValue);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// print out the value you read:
Serial.println("ingedrukt");
for (i = 0; i < 10; i = i + 1) {
if(Coordinates[i]==-1) {
Coordinates[i]=potValue;
Serial.println(i);
i=9999;
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(500);
}
}
}
for (i = 0; i < 10; i = i + 1) {
int difVal = abs(Coordinates[i] - potValue);
potValue = analogRead(A0);
if (difVal <=tolerance && motorRun == 0 ) {
digitalWrite(vibPin, HIGH);
delay(100);
digitalWrite(vibPin, LOW);
Serial.println("In range");
motorRun= 1;
}
else {
motorRun= 0;
Serial.println("Out of range");
}
}
delay(200);
}