I have built an electronic high-striker for a year 7 project but am having trouble with the code. How it works is the person hits a wooden block that is held over a sealed hose. A pressure sensor (MPX4250AP) on the other end of the hose will sense the pressure and give the info to the arduino mega. The arduino will then light up leds according to this.
This code is for testing and it works but only does a led scrolling effect
const int ledCount = 50; // the number of LEDs in the bar graph
int ledPins[] = { 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51}; // an array of pin numbers to which LEDs are attached
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
digitalWrite(ledPins[thisLed], HIGH); // turn on the LED
delay(20); // wait
digitalWrite(ledPins[thisLed], LOW); // turn off the LED
}
}
I have this code but for some reason, the led pins don't light up
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int ledCount = 50; // the number of LEDs in the bar graph
int ledPins[] = { 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51}; // an array of pin numbers to which LEDs are attached
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
Serial.begin(9600);
}
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
outputValue = (sensorValue / 20); // divided by 20 because there are 50 leds (1023 top input)
for (int thisLed = 0; thisLed > outputValue; thisLed++) {
digitalWrite(ledPins[thisLed], HIGH); // turn on the LED
delay(20); // wait
digitalWrite(ledPins[thisLed], LOW); // turn off the LED
// print the results to the serial monitor
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(2);
}
}
and this code which uses gravity but the same thing happens
const int ledCount = 50; // the number of LEDs in the bar graph
int ledPins[] = { 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51}; // an array of pin numbers to which LEDs are attached
float gravity_constant = 9.8;
int gravity[50] = {};
void setup() {
for (int i = 1; i <= 51; i++) { // this will loop 51 times
float distance_x = i * .035; // calculate the total distance from the top to the LED in question
float distance_y = (i - 1) * .035; // same distance, minus one step
float time_x = sqrt(distance_x / gravity_constant); // the time it takes for an object to fall distance_x meters
float time_y = sqrt(distance_y / gravity_constant); // the time it takes for an object to fall distance_y meters
float time = time_x - time_y; // the time it takes for an object to fall to our LED from the one before it, in seconds
int time_ms = time * 1000; // convert to milliseconds
gravity[i] = time_ms; // add this value to the gravity array
}
}
void loop() {
// put your main code here, to run repeatedly:
int level = 48; // this is the level the person hit to
// * entered manually for testing purposes,
// in the final product this will be determined by
// the force of the hammer strike
for (int i = level; i > 0; i--) {
digitalWrite(ledPins[level - i], HIGH);
delay (gravity[i]);
digitalWrite(ledPins[level - i], LOW);
}
}
Can someone help me?
Moderator edit: Why does it seem so hard to read a few simple guidelines before posting? tags added, italics removed
