Question: My created code below (Currently working with Polling). Need help making this code work? /* Code Referenced from: http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay https://www.arduino.cc/en/Tutorial/WhileStatementConditional?from=Tutorial.WhileLoop */ // These constants won't
My created code below (Currently working with Polling). Need help making this code work?
/* Code Referenced from:
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
https://www.arduino.cc/en/Tutorial/WhileStatementConditional?from=Tutorial.WhileLoop
*/
// These constants won't change:
const int sensor1Pin = 2; // pin that the sensor1 is attached to
const int sensor2Pin = 3; // pin that the sensor2 is attached to
const int ledPin = LED_BUILTIN; // pin that the LED is attached to
// These variables will change:
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int sensorValue = 0; // the sensor value
int ledState = LOW;
unsigned long previousMillis = 0;
long interval = 1000;
void setup() {
// set the LED pins as outputs and the switch pin as input:
pinMode(ledPin, OUTPUT);
pinMode(sensor1Pin, INPUT);
pinMode(sensor2Pin, INPUT);
}
void loop() {
// while the sensor is low:
while (digitalRead(sensor1Pin) == HIGH) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}
// while sensor is high
while (digitalRead(sensor2Pin) == HIGH) {
int ledState = HIGH;
unsigned long previousMillis = 0;
interval = 500;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}
}
This is to develop a UNO code that would measure the speed of a flying bullet and print out the speed in ft/sec to the serial monitor. The on-board LED would be flashed at 0.5 Hz until a flying bullet is detected. At that time the LED should flash at 1 Hz. Use the following assumptions: the bullet speed is between 100 to 500 ft/sec, the clock frequency on the UNO board is 16 MHz, two optical sensors are placed one foot apart and they are connected to two different UNO digital pins, and these optical sensors provide 5-volt outputs when they detect lights and O-volt outputs when the lights are blocked by the bullet. This is to develop a UNO code that would measure the speed of a flying bullet and print out the speed in ft/sec to the serial monitor. The on-board LED would be flashed at 0.5 Hz until a flying bullet is detected. At that time the LED should flash at 1 Hz. Use the following assumptions: the bullet speed is between 100 to 500 ft/sec, the clock frequency on the UNO board is 16 MHz, two optical sensors are placed one foot apart and they are connected to two different UNO digital pins, and these optical sensors provide 5-volt outputs when they detect lights and O-volt outputs when the lights are blocked by the bullet
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
