I want to have the force sensor have three different values (high, medium, and low)
For prototyping purposes i would like to have a serial read out that also says high, medium, and low.
I would like to have corresponding LEDs (Red, Yellow, and Green)
What I am thinking is have pins 7,8,9 be LED pins so that they are set up in parallel so they can be activated individually by the value coming from the force sensor. This code isn't working it says that ledPin has already been defined, and i know that this isn't correct. Any thoughts on how to make this work?
(p.s. I'm relatively new to coding so your patience is appreciated)
/*
Social Body:
Wearable Technology 2 (GDES 3B44)
Michael Vaughan - Winter 2011
Code taken and modified from:
Sensor Project
Wearable Technology 1 (GDES 3B16)
Kate Hartman - Fall 2010
*/
int ledPin = 7; // LED is connected to digital pin 7
int ledPin = 8; // LED is connected to digital pin 8
int ledPin = 9; // LED is connected to digital pin 9
int sensorPin = 0; // Stretch sensor is connected to analog pin 0
int sensorValue; // variable to store the value coming from the sensor
void setup()
{
pinMode(ledPin = 7, OUTPUT); // sets the ledPin to be an output
pinMode(ledPin = 8, OUTPUT); // sets the ledPin to be an output
pinMode(ledPin = 9, OUTPUT); // sets the ledPin to be an output
Serial.begin(9600); //initialize the serial port
}
void loop() // run over and over again
{
sensorValue = analogRead(sensorPin); // read the value from the sensor
if(sensorValue>850){ //You can change the number here to adjust the threshold.
Serial.print("HIGH");
digitalWrite(ledPin 9 , HIGH); //Turn LED on
if(sensorValue>650){ //You can change the number here to adjust the threshold.
Serial.print("Medium");
digitalWrite(ledPin 8, HIGH); //Turn LED on
if(sensorValue>450){ //You can change the number here to adjust the threshold.
Serial.print("Low");
digitalWrite(ledPin 7, HIGH); //Turn LED 7 on
}
else{
Serial.println("none");
digitalWrite(ledPin, LOW); //Turn LED off
}
Serial.print(" Sensor Value: ");
Serial.println(sensorValue);
delay(100); // delay for 1/10 of a second
}
Lots of little errors. I've corrected them enough so that it now compiles without errors, however I didn't wire up things and try and run it.
Your assignment is to carefully read my code and see where you had mistakes.
/*
Social Body:
Wearable Technology 2 (GDES 3B44)
Michael Vaughan - Winter 2011
Code taken and modified from:
Sensor Project
Wearable Technology 1 (GDES 3B16)
Kate Hartman - Fall 2010
*/
int ledPinA = 7; // LED is connected to digital pin 7
int ledPinB = 8; // LED is connected to digital pin 8
int ledPinC = 9; // LED is connected to digital pin 9
int sensorPin = 0; // Stretch sensor is connected to analog pin 0
int sensorValue; // variable to store the value coming from the sensor
void setup()
{
pinMode(ledPinA, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinB, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinC, OUTPUT); // sets the ledPin to be an output
Serial.begin(9600); //initialize the serial port
}
void loop() // run over and over again
{
sensorValue = analogRead(sensorPin); // read the value from the sensor
if(sensorValue>850){ //You can change the number here to adjust the threshold.
Serial.print("HIGH");
digitalWrite(ledPinC, HIGH); //Turn LED on
}
if(sensorValue>650){ //You can change the number here to adjust the threshold.
Serial.print("Medium");
digitalWrite(ledPinB, HIGH); //Turn LED on
}
if(sensorValue>450){ //You can change the number here to adjust the threshold.
Serial.print("Low");
digitalWrite(ledPinA, HIGH); //Turn LED 7 on
}
else{
Serial.println("none");
digitalWrite(ledPinA, LOW); //Turn LED off
digitalWrite(ledPinB, LOW); //Turn LED off
digitalWrite(ledPinC, LOW); //Turn LED off
}
That works exactly the way I was hoping, Thank you!!
I did what you said and followed what was wrong, the a,b,c labels I imagine are crucial for recognizing that they are different output pins . The bracket errors, well sometimes i just don't know when they're needed and when they're not. I guess experience will help with that.
How would I add a time delay so that the force sensor reads instantly and relays it to the leds instantly but the lights stay on for 10 seconds?
I tried changing the delay at the bottom but it just delayed the whole action (only read the sensor every 10 seconds)
"How would I add a time delay so that the force sensor reads instantly and relays it to the leds instantly but the lights stay on for 10 seconds?"
I added a delay statement inside the sensor brackets and now it reads instantly and the lights stay lit for ten seconds, but I want the sensor to control the lights even if the lights are still on and then stay lit for ten seconds.
Study the IDE's example sketch of blink without delay. It shows how one can use the millis() function to set up time calculations so you can have stuff happen when you want them to happen.
Like this? Its not working properly, when I have it set like this the sensor only inputs the data every 10 seconds, and without it, it doesn't hold the light.
Im sure im not using the millis if statement correctly but Im not sure how i would set it up...
/*
Social Body:
Wearable Technology 2 (GDES 3B44)
Michael Vaughan - Winter 2011
Code taken and modified from:
Sensor Project
Wearable Technology 1 (GDES 3B16)
Kate Hartman - Fall 2010
*/
int ledPinA = 7; // LED is connected to digital pin 7
int ledPinB = 8; // LED is connected to digital pin 8
int ledPinC = 9; // LED is connected to digital pin 9
int sensorPin = 0; // Stretch sensor is connected to analog pin 0
int sensorValue; // variable to store the value coming from the sensor
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
void setup()
{
pinMode(ledPinA, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinB, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinC, OUTPUT); // sets the ledPin to be an output
Serial.begin(9600); //initialize the serial port
}
void loop() // run over and over again
{
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.print(" Sensor Value: ");
Serial.println(sensorValue);
delay(100); // delay for 1/10 of a second
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
// save the last time you blinked the LED
previousMillis = currentMillis;
{if(sensorValue>850){ //You can change the number here to adjust the threshold.
Serial.print("HIGH");
digitalWrite(ledPinC, HIGH); //Turn LED on
}
if(sensorValue>650){ //You can change the number here to adjust the threshold.
Serial.print("Medium");
digitalWrite(ledPinB, HIGH); //Turn LED on
}
if(sensorValue>150){ //You can change the number here to adjust the threshold.
Serial.print("Low");
digitalWrite(ledPinA, HIGH); //Turn LED 7 on
}
else{
Serial.println("none");
digitalWrite(ledPinA, LOW); //Turn LED off
digitalWrite(ledPinB, LOW); //Turn LED off
digitalWrite(ledPinC, LOW); //Turn LED off
delay(10000); // delay for 10 seconds
}
}
}
the sensor data reads, delays for 1/10 of a second.
If the sensor reads data it turns on the led and delays for 10 seconds. i feel like it should continue reading data (delay (100) even if the light is on(delay 10000), but the data flow stops for ten seconds.
Why is that happening? and how can i fix it?
/*
Social Body:
Wearable Technology 2 (GDES 3B44)
Michael Vaughan - Winter 2011
Code taken and modified from:
Sensor Project
Wearable Technology 1 (GDES 3B16)
Kate Hartman - Fall 2010
*/
int ledPinA = 7; // LED is connected to digital pin 7
int ledPinB = 8; // LED is connected to digital pin 8
int ledPinC = 9; // LED is connected to digital pin 9
int sensorPin = 0; // Stretch sensor is connected to analog pin 0
int sensorValue; // variable to store the value coming from the sensor
void setup()
{
pinMode(ledPinA, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinB, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinC, OUTPUT); // sets the ledPin to be an output
Serial.begin(9600); //initialize the serial port
}
void loop() // run over and over again
{
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.print(" Sensor Value: ");
Serial.println(sensorValue);
delay(100); // delay for 1/10 of a second
{if(sensorValue>850){ //You can change the number here to adjust the threshold.
Serial.print("HIGH");
digitalWrite(ledPinC, HIGH); //Turn LED on
}
if(sensorValue>650){ //You can change the number here to adjust the threshold.
Serial.print("Medium");
digitalWrite(ledPinB, HIGH); //Turn LED on
}
if(sensorValue>150){ //You can change the number here to adjust the threshold.
Serial.print("Low");
digitalWrite(ledPinA, HIGH); //Turn LED 7 on
delay(10000); // delay for 10 seconds
}
else{
Serial.println("none");
digitalWrite(ledPinA, LOW); //Turn LED off
digitalWrite(ledPinB, LOW); //Turn LED off
digitalWrite(ledPinC, LOW); //Turn LED off
}
}
}
Hey ok so this works as far as when i press low green lights up when i press harder yellow lights up immediately but the lights don't stay on for ten seconds. if i add a delay anywhere it stops the whole thing until the delay is over. what can i do to add the 10 second delay when the lights go on but not stop the sensor data?
/*
Social Body:
Wearable Technology 2 (GDES 3B44)
Michael Vaughan - Winter 2011
Code taken and modified from:
Sensor Project
Wearable Technology 1 (GDES 3B16)
Kate Hartman - Fall 2010
*/
int ledPinA = 7; // LED is connected to digital pin 7
int ledPinB = 8; // LED is connected to digital pin 8
int ledPinC = 9; // LED is connected to digital pin 9
int sensorPin = 0; // Stretch sensor is connected to analog pin 0
int sensorValue; // variable to store the value coming from the sensor
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 100; // interval at which to blink (milliseconds)
void setup()
{
pinMode(ledPinA, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinB, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinC, OUTPUT); // sets the ledPin to be an output
Serial.begin(9600); //initialize the serial port
}
void loop() // run over and over again
{
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.print(" Sensor Value: ");
Serial.println(sensorValue);
/* delay(100); // delay for 1/10 of a second*/
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
{if(sensorValue>850){ //You can change the number here to adjust the threshold.
Serial.print("HIGH");
digitalWrite(ledPinC, HIGH); //Turn LED on
}
if(sensorValue>650){ //You can change the number here to adjust the threshold.
Serial.print("Medium");
digitalWrite(ledPinB, HIGH); //Turn LED on
}
if(sensorValue>150){ //You can change the number here to adjust the threshold.
Serial.print("Low");
digitalWrite(ledPinA, HIGH); //Turn LED 7 on
/* delay(10000); // delay for 10 seconds*/
}
else{
Serial.println("none");
digitalWrite(ledPinA, LOW); //Turn LED off
digitalWrite(ledPinB, LOW); //Turn LED off
digitalWrite(ledPinC, LOW); //Turn LED off
}
}
}
}