I am extremely new to arduino and programing in general.
I’m working on a project that requires two LEDs a vibe motor and I need it to be triggered by a force sensor.
I have been having issues just getting the force sensor and LEDs working. I think I solved it though, I just need to figure out how to get the Vibe motor to run with it now.
The code I have so far is just pieced together from two tutorials but I’m still not sure if it’s correct or not.
/* FSR testing sketch.
Connect one end of FSR to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
Connect LED from pin 11 through a resistor to ground
For more information see www.ladyada.net/learn/sensors/fsr.html */
int fsrAnalogPin = 0; // FSR is connected to analog 0
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int fsrReading; // the analog reading from the FSR resistor divider
int LEDbrightness;
void setup(void) {
Serial.begin(9600); // We'll send debugging information via the Serial monitor
pinMode(LEDpin, OUTPUT);
pinMode(1, OUTPUT); // Vibe motor (Change pin number)
}
void loop(void) {
fsrReading = analogRead(fsrAnalogPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading);
// we'll need to change the range from the analog reading (0-1023) down to the range
// used by analogWrite (0-255) with map!
LEDbrightness = map(fsrReading, 0, 1023, 0, 255); // (I need to change this so it's either on or off. Not sure how to go about that.)
// LED gets brighter the harder you press
analogWrite(LEDpin, LEDbrightness);
delay(100);
digitalWrite(HIGH, 1);
delay(1000);
digitalWrite(LOW, 1);
delay(1000);
}
I have tried about fifty different pieces of code for this.
First thing I notice is that your syntax for turning the vibe motor on (and then off after 1 sec) is swapped. It should be …
digitalWrite(pin, value) … not (value, pin) as you have it.
http://arduino.cc/en/Reference/DigitalWrite
Secondly I have to ask how you’re driving the motor. Connecting it directly to an Arduino ouput pin is not recommended and probably won’t work (even with a tiny pager motor). You should read the CIRC-03 lesson and follow what was done there (changing the pin used to your pin). Also the 2.2k ohm resistor should really be more like a 200 - 500 ohm resistor.
http://oomlout.com/a/products/ardx/circ-03/
Thirdly the Arduino uses pins 0 and 1 to communicate with the USB bus. They really shouldn’t be used for anything else unless you take precautions. In your case I’d recommend using pin 2.
Lastly if you want the force sensor to just turn the LED on/off you need to add a comparison to your code and remove the mapping and analogWrite that’s presently done. Add a new int “Thresh” to the declaration section. Perhaps initialize it to … well I don’t know … you’ll have to experiment and look at the prints to the serial monitor to see what the on/off threshold should be by pressing on the sensor. Then the portion of your loop that presently varies the brightness would be something like …
/* FSR testing sketch.
Connect one end of FSR to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
Connect LED from pin 11 through a resistor to ground
For more information see www.ladyada.net/learn/sensors/fsr.html */
int fsrAnalogPin = 0; // FSR is connected to analog 0
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int fsrReading; // the analog reading from the FSR resistor divider
int Thresh = 250; // threshold for turning LED on or off
int MotorPin = 2; // the pin to control the motor on or off
void setup()
{
Serial.begin(9600); // We'll send debugging information via the Serial monitor
pinMode(LEDpin, OUTPUT);
pinMode(MotorPin , OUTPUT);
}
void loop()
{
fsrReading = analogRead(fsrAnalogPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading);
// compare the reading from 0-1023 to a threshold
// the harder you push the higher the reading
if(fsrReading > Thresh)
{
digitalWrite(LEDpin, HIGH); //LED on
}
else
{
digitalWrite(LEDpin, LOW); //LED off
}
delay(100);
// turn the motor on and wait 1 sec then turn it off
digitalWrite(MotorPin , HIGH);
delay(1000);
digitalWrite(MotorPin , LOW);
delay(1000);
}