High Striker

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

Half your post is in italics.

Read this before posting a programming question

How to use this forum

Code tags, please.

I have this code but for some reason, the led pins don't light up

Why are you printing the value read from the sensor in the light-the-LED loop? What values are you seeing?

When I open the serial monitor, I see the pressure sensor values but as i said before, the leds don't light up

This doesn't look good:

  for (int thisLed = 0; thisLed > outputValue; thisLed++)

Shouldn't that condition be using less than ( < )?

thepie423:
When I open the serial monitor, I see the pressure sensor values but as i said before, the leds don't light up

What values? 3? 476? 1013?

replying to wildbill, what I am trying to say in the code is to make the leds keep going up until the level reaches the output value (sensor value/50)

Is there a 'repeat until' or 'continue until' command?

Could you suggest anything to replace this with?

and I see the same as what you would if you used the 'analog_read_serial' code:

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
 
 This example code is in the public domain.
 */

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

Is there a 'repeat until' or 'continue until' command?

Yes. It's the for loop. The middle clause is a "while true" statement. The loop continues as long as the middle clause is true. It ends when the middle clause is no longer true.

Right so how would I make it so that the leds will continue going up until it reaches the output/sensor value?

Could I use for (int thisLed = 0; thisLed < outputValue; thisLed++) like somebody else suggested?

Could I use

Yes.

Ok so I edited the code and put in the 'for loop' function but the Arduino program is saying I need a bracket before the command 'loop' (in 'for loop'). This is fine with me but when I press verify, it says 'too many arguments to function 'void loop'

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};

const int analogInPin = A0;  // Analog input pin that the sensor is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;

void setup() {
  // put your setup code here, to run once:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly: 
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out and set it to fit the 1-50 leds:
  outputValue = ((map(sensorValue, 0, 1023, 0, 255)) - 23) / 50;  
  
  int thisLed = 0;
  
  for loop(thisLed < outputValue); { 
    thisLed++ ;
    digitalWrite(ledPins[thisLed], HIGH);  // turn on the LED
    delay(20);                             // wait
    digitalWrite(ledPins[thisLed], LOW);   // turn off the LED 
  }
}

Compare:

  for loop(thisLed < outputValue); {

to:

for (int thisLed = 0; thisLed < outputValue; thisLed++)

Figure out which one is right.

Thanks.

Would this work?

// map it to the range of the analog out and set it to fit the 1-50 leds:
  outputValue = map(sensorValue, 360, 1023, 0, 50);

What i am trying to do is to map the sensor value to fit the leds. (360 is the natural pressure from the weight of the block on top of the hose and 50 is the no of leds)

sorry i checked again and i am seeing nothing but 1023 in the serial monitor when i use the analog read serial code!

Make sure your wires are connected properly. Use a voltage meter to make sure you aren't sitting at over the max input.

Have you tried connecting a voltmeter with a 'max' function and testing the circuit to see what kind of output you get?

The wires: I am using a mini breadboard to do this
pin 1 (pressure sensor MPX4250AP) - Analog input 0
pin 2 - ground (or 5V can't remember)
pin 3 - 5V (or ground can't remember)
pins 4, 5, 6 - not connected

I can't remember which power source I connected the pin 2 and 3 to but when I switched them, I was getting nothing but 0s even when I changed the pressure

Have I connected the mini breadboard correctly?
Not sure, see attachment for a picture

I'm fairly sure that I have connected it correctly because all of the websites and schematics say that it is done this way