potentiometer and seven segment display

HELLO EVERYBODY IN THIS PART OF THE PROGRAM I TURN THE POTENTIOMETER AND THE SEGMENT ON THE DISPLAY CHANGES FOR DIFFERENT VALUES OF IT......I HAVE TWO VERSIONS OF THE SAME PROGRAM, BUT THE LAST ONE DOESNT'T WORK....SOMEONE CAN KINDLY SEE WHAT I CAN CHANGE IN THE FIRST??

FIRST VERSION THAT WORKS BUT IT IS NOT VERY ELEGANT

define LATCH 7

define CLOCK 8

define DATA 4

define POT2 1

define buttonPin 11

define BUZZER 3

define pitch 1000

include "pitches.h"

int prec_Stato=0; unsigned long elapsed;

int fractional; unsigned long start; unsigned long finished;

int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 };

int buttonPushCounter; const byte numeral[10] = { //ABCDEFG /dp B11000000, // 0//okokok B11111001, // 1//okokok B10100100, // 2//okokok B10110000, // 3//okokok B10011001, // 4//okokok B10010010, // 5//okokok B10000010, // 6//okokok B11111000, // 7//okokok B10000000, // 8//okokok B10011000, // 9//okokok };

int buttonState;

void displayResult() { float h, m, s, ms; unsigned long over; elapsed = finished - start;

h= int (elapsed/3600000); over=elapsed%3600000; m= int (over/60000); over=over%60000; s=int(over/1000); ms=over%1000;

Serial.print("Raw elapsed time: "); Serial.println(elapsed); Serial.print("Elapsed time: "); Serial.print(h,0); Serial.print("h "); Serial.print(m,0); Serial.print("m "); Serial.print(s,0); Serial.print("s "); Serial.print(ms,0); Serial.print("ms "); Serial.println();

}

void displayDigit() {

int y = analogRead(POT2);

if (y>0 && y<100) { digitalWrite(LATCH,LOW); shiftOut(DATA,CLOCK,MSBFIRST,numeral[1]); digitalWrite(LATCH,HIGH); start = millis(); }

else if (y>100 && y<200) { digitalWrite(LATCH,LOW); shiftOut(DATA,CLOCK,MSBFIRST,numeral[2]); digitalWrite(LATCH,HIGH); start = millis(); }

else if (y>200 && y<300) { digitalWrite(LATCH,LOW); shiftOut(DATA,CLOCK,MSBFIRST,numeral[3]); digitalWrite(LATCH,HIGH); start = millis(); }

else if (y>300 && y<400) { digitalWrite(LATCH,LOW); shiftOut(DATA,CLOCK,MSBFIRST,numeral[4]); digitalWrite(LATCH,HIGH); start = millis(); }

else if (y>400 && y<500) { digitalWrite(LATCH,LOW); shiftOut(DATA,CLOCK,MSBFIRST,numeral[5]); digitalWrite(LATCH,HIGH); start = millis(); }

else if (y>500 && y<600) { digitalWrite(LATCH,LOW); shiftOut(DATA,CLOCK,MSBFIRST,numeral[6]); digitalWrite(LATCH,HIGH); start = millis(); }

else if (y>600 && y<700) { digitalWrite(LATCH,LOW); shiftOut(DATA,CLOCK,MSBFIRST,numeral[7]); digitalWrite(LATCH,HIGH); start = millis(); } else if (y>700 && y<800) { digitalWrite(LATCH,LOW); shiftOut(DATA,CLOCK,MSBFIRST,numeral[8]); digitalWrite(LATCH,HIGH); start = millis(); }

else if (y>800 && y<1000) { digitalWrite(LATCH,LOW); shiftOut(DATA,CLOCK,MSBFIRST,numeral[9]); digitalWrite(LATCH,HIGH); start = millis(); }

}

void setup() { Serial.begin(9600); pinMode(LATCH, OUTPUT); pinMode(CLOCK, OUTPUT); pinMode(DATA,OUTPUT); pinMode(POT2, INPUT); pinMode(buttonPin, INPUT); //digitalWrite(buttonPin, HIGH); digitalWrite(buttonPin, LOW); pinMode(BUZZER,OUTPUT); }

void loop() { displayDigit();

int buttonState = digitalRead(buttonPin); if (buttonState!=prec_Stato) { if (!buttonState) { tone(BUZZER,pitch); Serial.print("pressed"); finished = millis(); delay(2000); displayResult(); delay(2000); }

else

{ Serial.print("NOT pressed"); noTone(3); }

prec_Stato=buttonState; }

}

SECOND VERSION THAT DOESN' T WORK

define LATCH 7

define CLOCK 8

define DATA 4

define POT2 1

define buttonPin 11

define BUZZER 3

define pitch 1000

include "pitches.h"

int prec_Stato=0; unsigned long elapsed; int i=0; int j=0; int fractional; unsigned long start; unsigned long finished; char range[10];

int buttonPushCounter; const byte numeral[10] = { //ABCDEFG /dp B11000000, // 0//okokok B11111001, // 1//okokok B10100100, // 2//okokok B10110000, // 3//okokok B10011001, // 4//okokok B10010010, // 5//okokok B10000010, // 6//okokok B11111000, // 7//okokok B10000000, // 8//okokok B10011000, // 9//okokok };

int buttonState;

void displayResult() { float h, m, s, ms; unsigned long over; elapsed = finished - start;

h= int (elapsed/3600000); over=elapsed%3600000; m= int (over/60000); over=over%60000; s=int(over/1000); ms=over%1000;

Serial.print("Raw elapsed time: "); Serial.println(elapsed); Serial.print("Elapsed time: "); Serial.print(h,0); Serial.print("h "); Serial.print(m,0); Serial.print("m "); Serial.print(s,0); Serial.print("s "); Serial.print(ms,0); Serial.print("ms "); Serial.println();

}

void displayDigit() {

int y = analogRead(POT2);

range[10]=1023/10;

for (j=0;j<10; j++) { for(i=0;i<10;i++) { while (y==range*)* * {* * digitalWrite(LATCH,LOW);* shiftOut(DATA,CLOCK,MSBFIRST,numeral[j]); digitalWrite(LATCH,HIGH); start = millis(); } } * }* } void setup() { * Serial.begin(9600);* pinMode(LATCH, OUTPUT); pinMode(CLOCK, OUTPUT); pinMode(DATA,OUTPUT); pinMode(POT2, INPUT); pinMode(buttonPin, INPUT); //digitalWrite(buttonPin, HIGH); * *digitalWrite(buttonPin, LOW); pinMode(BUZZER,OUTPUT); } void loop() { displayDigit(); * int buttonState = digitalRead(buttonPin);* * if (buttonState!=prec_Stato)* * {* * if (!buttonState)* * {* * tone(BUZZER,pitch);* * Serial.print("pressed");* * finished = millis();* * delay(2000);* * displayResult();* * delay(2000);* * }*

  • else*

  • {*

  • Serial.print("NOT pressed");*

  • noTone(3);*

  • }*

  • prec_Stato=buttonState;*

  • }* } //start = millis(); /* while (digitalRead(buttonPin)==LOW) {

  • //wait until I press the button*

  • ;*

  • tone(BUZZER,pitch);*

  • Serial.print("pressed");*

  • }*

/* if(digitalRead(buttonPin)==LOW) * {* * tone(BUZZER,pitch);* * Serial.print("pressed");* _ } /_ *// finished = millis();

//noTone(3); MANY THANKS

When you write that it doesn't work, what is it doing that isn't correct?

This looks problematic:

range[10]=1023/10;

  1. 10 is outside the arrays index range
  2. Doing this only sets one index to said value.

Use a for loop to set each index to their proper values. (0 - 9) -> 10 total

One more thing I saw.

float h, m, s, ms;

Why make them floats if you are just going to cast them to ints later?

Thank you a lot for the answers...

really there are many things that I have to change ...

for the moment I use float I will change surely it later...

I don' t know how to compact the series of if for values from 0 to 1023 :roll_eyes: I have tried with the division of 1023 by 10...but doesn't work

It shows all the numbers in the display with the less elegant program, instead with the compact program shows only one number :sob:

Try this.

#define LATCH 7
#define CLOCK 8
#define DATA 4
#define POT2 1
#define buttonPin 11
#define BUZZER 3
#define pitch 1000
#define sensitivity 10

#include "pitches.h"

int prec_Stato = 0;
unsigned long elapsed;
int fractional;
unsigned long start;
unsigned long finished;
char range[10];

const byte numeral[10] = {
  //ABCDEFG /dp
  B11000000, // 0//okokok
  B11111001, // 1//okokok
  B10100100, // 2//okokok
  B10110000, // 3//okokok
  B10011001, // 4//okokok
  B10010010, // 5//okokok
  B10000010, // 6//okokok
  B11111000, // 7//okokok
  B10000000, // 8//okokok
  B10011000, // 9//okokok
};

void displayResult()
{
  static int h, m, s, ms;
  static unsigned long over;

  elapsed = finished - start;

  h = int (elapsed / 3600000);
  over = elapsed % 3600000;
  m = int (over / 60000);
  over = over % 60000;
  s = int(over / 1000);
  ms = over % 1000;

  Serial.print("Raw elapsed time: ");
  Serial.println(elapsed);
  Serial.print("Elapsed time: ");

  Serial.print(h, 0);
  Serial.print("h ");
  Serial.print(m, 0);
  Serial.print("m ");
  Serial.print(s, 0);
  Serial.print("s ");
  Serial.print(ms, 0);
  Serial.print("ms ");
  Serial.println();

}

void displayDigit()
{
  static int y;

  //------------Smooth out the readings-------------
  int average = 0;
  for (byte Avg = 0; Avg < 5; Avg++)
  {
    average += analogRead(POT2);
  }
  y = average / 5;
  //------------------------------------------------

  for (byte i = 0; i < 10; i++)
  {
    if ((y >= range[i] - sensitivity) && (y <= range[i] + sensitivity))
    {
      digitalWrite(LATCH, LOW);
      shiftOut(DATA, CLOCK, MSBFIRST, numeral[i]);
      digitalWrite(LATCH, HIGH);
      start = millis();
    }
  }
}


void setup()
{
  Serial.begin(9600);
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, OUTPUT);
  pinMode(POT2, INPUT);
  pinMode(buttonPin, INPUT);
  //digitalWrite(buttonPin, HIGH);
  digitalWrite(buttonPin, LOW);
  pinMode(BUZZER, OUTPUT);

  for (int i = 9; i > 0; i--)
  {
    range[9 - i] = 1023 / (i + 1) ;
  }
}

void loop()
{
  displayDigit();

  byte buttonState =  digitalRead(buttonPin);

  if (buttonState != prec_Stato)
  {
    if (!buttonState)
    {
      tone(BUZZER, pitch);
      Serial.print("pressed");
      finished = millis();
      delay(2000);
      displayResult();
      delay(2000);
    }
    else
    {
      Serial.print("NOT pressed");
      noTone(3);
    }

    prec_Stato = buttonState;
  }
}



//start = millis();

/* while (digitalRead(buttonPin)==LOW)
{
  //wait until I press the button
  ;
  tone(BUZZER,pitch);
   Serial.print("pressed");
  }

/* if(digitalRead(buttonPin)==LOW)
  {
   tone(BUZZER,pitch);
   Serial.print("pressed");
  } */
// finished = millis();


//noTone(3);

Thank you a lot Hazardsmind for the time spent and your availability :relaxed: ...I will try it as soon as possible!!!!

A question......how can I make the led to pulse for a choosen number of times??

for example is i choose 5 with the potentiometer, I would like it pulses 5 times....

Thank you

may be

for (i=0; i

digitalwrite(led, high);

}

:o

check your pm