Pages: 1 [2]   Go Down
Author Topic: Help with two 7-segment displays & pot code  (Read 3537 times)
0 Members and 1 Guest are viewing this topic.
Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34567
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
For the code that I have it only works this way...if(j= map(analogRead (0), 0, 1023, 0, 10));
Then you don't understand how a program works. Please re read the page on the if statement and then re write your code. At your delicate state of knowledge NEVER use a single = inside an if statement. If you require that statement then perform it outside the if statement and use the if statement properly.
Logged

Ireland
Offline Offline
Full Member
***
Karma: 0
Posts: 176
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Extremely delicate!!!!   smiley
Logged

Ireland
Offline Offline
Full Member
***
Karma: 0
Posts: 176
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley Hello there. This is the result of my understand so far on using the "if" statement.....
Code:
if ((i==2) && (h==2)){
     mapMode == 11;
                 syncPhaseInc = mapPentatonic11(analogRead(SYNC_CONTROL));
   }
it's working for me.

And yes I managed to get three 7-segment displays working together. Two with one pot to select a musical scale (0-99 "i") and a separate one to select the key of that scale (A-G "h"). In case anyone has a need for something similar here it is (three 7-segment common cathode displays using three 595's).
Code:


int dataPin = 11;
int clockPin = 12;
int latchPin = 8;

byte dataRED;
byte dataArrayRED[10];
byte dataArray2[7];



void setup() {
 
    pinMode(latchPin, OUTPUT);

         
  dataArrayRED[0] = 0x3F; //00111111 - 0
  dataArrayRED[1] = 0x06; //00000110 - 1
  dataArrayRED[2] = 0x5B; //01011011 - 2
  dataArrayRED[3] = 0x4F; //01001111 - 3
  dataArrayRED[4] = 0x66; //01100110 - 4
  dataArrayRED[5] = 0x6D; //01101101 - 5
  dataArrayRED[6] = 0x7D; //01111101 - 6
  dataArrayRED[7] = 0x07; //00000111 - 7
  dataArrayRED[8] = 0x7F; //01111111 - 8
  dataArrayRED[9] = 0x6F; //01100111 - 9
 
  // Key Selection
  dataArray2[0] = B01110111; // A  Check display wiring, this has been modified!
  dataArray2[1] = B01111100; // b
  dataArray2[2] = B00111001; // C
  dataArray2[3] = B01011110; // d
  dataArray2[4] = B01111001; // E
  dataArray2[5] = B01110001; // F
  dataArray2[6] = B01101111; // g
 
 
}
 
  void loop() {
 
    int val = 10; //this is for how many digits
    int i =  map(analogRead (0), 0, 1023, 0, 20); // Scale Selection Display
    int h =  map(analogRead (1), 0, 1023, 0, 7);  // Key Selection Display
   
   {
       digitalWrite(latchPin, 0);

 
    int digit2 = i % val;
    int digit1 = ( i - digit2 ) / val;
    int digit3 = h;
 
    shiftOut(dataPin, clockPin, dataArray2[digit3]);
    shiftOut(dataPin, clockPin, dataArrayRED[ digit2 ]);
    shiftOut(dataPin, clockPin, dataArrayRED[ digit1 ]);

   
      digitalWrite(latchPin, 1);

   }
  }
 
  void shiftOut( int myDataPin,  int myClockPin,  byte myDataOut) {

 
    int i=0;
    int pinState;
   
    pinMode(myClockPin, OUTPUT);
    pinMode(myDataPin, OUTPUT);
 
 
    digitalWrite(myDataPin, 0);
    digitalWrite(myClockPin, 0);
 
 
       for (i=7; i>=0; i--)
       
    {
     
          digitalWrite(myClockPin, 0);

         
              if ( myDataOut & (1<<i) ) 
       
     {
pinState= 1;
    }
    else
    {
pinState= 0;
    }   
 
   //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
   //register shifts bits on upstroke of clock pin
    digitalWrite(myClockPin, 1);
   //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
    }
   
      digitalWrite(myClockPin, 0);
 
  }
   
 
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26496
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
dataArrayRED[0] = 0x3F; //00111111 - 0
  dataArrayRED[1] = 0x06; //00000110 - 1
  dataArrayRED[2] = 0x5B; //01011011 - 2
  dataArrayRED[3] = 0x4F; //01001111 - 3
  dataArrayRED[4] = 0x66; //01100110 - 4
  dataArrayRED[5] = 0x6D; //01101101 - 5
  dataArrayRED[6] = 0x7D; //01111101 - 6
  dataArrayRED[7] = 0x07; //00000111 - 7
  dataArrayRED[8] = 0x7F; //01111111 - 8
  dataArrayRED[9] = 0x6F; //01100111 - 9
Constant array initialisers are probably easier.
You can also write them in binary if you find conversion to and from hex difficult.
Saves typing.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34567
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It is much easier to initialises an array like this:-
byte dataArrayRED[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6F };
Logged

Ireland
Offline Offline
Full Member
***
Karma: 0
Posts: 176
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley Thanks for your help AWOL & GRUMPY_MIKE.....Here's the now further simplified code for anyone who needs it and a link for "Binary to Decimal to Hexadecimal Converter" which I found useful! http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html

Code:


int dataPin = 11;
int clockPin = 12;
int latchPin = 8;


byte dataArrayRED[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6F }; // 0 1 2 3 4 5 6 7 8 9
byte dataArray2[] = { 0x77, 0x7c, 0x39, 0x5E, 0x79, 0x71, 0x6F }; // A b C d E F g


void setup() {
 
    pinMode(latchPin, OUTPUT);
   
  }
 
  void loop() {
 
    int val = 10; //this is for how many digits
    int i =  map(analogRead (0), 0, 1023, 0, 20); // Scale Selection Display
    int h =  map(analogRead (1), 0, 1023, 0, 7);  // Key Selection Display
   
   {
       digitalWrite(latchPin, 0);

 
    int digit2 = i % val;
    int digit1 = ( i - digit2 ) / val;
    int digit3 = h;
 
    shiftOut(dataPin, clockPin, dataArray2[digit3]);
    shiftOut(dataPin, clockPin, dataArrayRED[ digit2 ]);
    shiftOut(dataPin, clockPin, dataArrayRED[ digit1 ]);

   
      digitalWrite(latchPin, 1);

   }
  }
 
  void shiftOut( int myDataPin,  int myClockPin,  byte myDataOut) {

 
    int i=0;
    int pinState;
   
    pinMode(myClockPin, OUTPUT);
    pinMode(myDataPin, OUTPUT);
 
 
    digitalWrite(myDataPin, 0);
    digitalWrite(myClockPin, 0);
 
 
       for (i=7; i>=0; i--)
       
    {
     
          digitalWrite(myClockPin, 0);

         
              if ( myDataOut & (1<<i) ) 
       
     {
pinState= 1;
    }
    else
    {
pinState= 0;
    }   
 
   //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
   //register shifts bits on upstroke of clock pin
    digitalWrite(myClockPin, 1);
   //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
    }
   
      digitalWrite(myClockPin, 0);
 
  }
   
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26496
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any good reason all the "pinMode"s aren't in "setup"?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Ireland
Offline Offline
Full Member
***
Karma: 0
Posts: 176
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I place  pinMode(myClockPin, OUTPUT);  &   pinMode(myDataPin, OUTPUT); in set up, then I must also  "int myClockPin; &
int myDataPin;  at the beginning of my code. I must also leave them where originally positioned under "void shiftOut" in order for the code to run correctly. But maybe this is the way it should be done??? Can they be both under "set up" and "void shiftOut" ??
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26496
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But you never change the pin assignments, so why bother making them parameters of shiftOut?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Ireland
Offline Offline
Full Member
***
Karma: 0
Posts: 176
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley Ok, cool! Thanks AWOl. I see what you mean and corrected it. My confusion was having a mix of "dataPin" & myDataPin" etc, throughout the code.
Here she is now.....
Code:


 int dataPin = 11;
 int clockPin = 12;
 int latchPin = 8;
 


byte dataArrayRED[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6F }; // 0 1 2 3 4 5 6 7 8 9
byte dataArray2[] = { 0x77, 0x7c, 0x39, 0x5E, 0x79, 0x71, 0x6F }; // A b C d E F g


void setup() {
 
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(dataPin, OUTPUT);
 
   
  }
 
  void loop() {
 
    int val = 10; //this is for how many digits
    int i =  map(analogRead (0), 0, 1023, 0, 20); // Scale Selection Display
    int h =  map(analogRead (1), 0, 1023, 0, 7);  // Key Selection Display
   
   {
       digitalWrite(latchPin, 0);

 
    int digit2 = i % val;
    int digit1 = ( i - digit2 ) / val;
    int digit3 = h;
 
    shiftOut(dataPin, clockPin, dataArray2[digit3]);
    shiftOut(dataPin, clockPin, dataArrayRED[ digit2 ]);
    shiftOut(dataPin, clockPin, dataArrayRED[ digit1 ]);

   
      digitalWrite(latchPin, 1);

   }
  }


 
  void shiftOut( int dataPin,  int clockPin,  byte dataOut) {

   int i=0;
   int pinState;
 
    digitalWrite(dataPin, 0);
    digitalWrite(clockPin, 0);
 
      for (i=7; i>=0; i--)
       
    {
     
          digitalWrite(clockPin, 0);

         
              if ( dataOut & (1<<i) ) 
       
     {
pinState= 1;
    }
    else
    {
pinState= 0;
    }   
 
   //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(dataPin, pinState);
   //register shifts bits on upstroke of clock pin
    digitalWrite(clockPin, 1);
   //zero the data pin after shift to prevent bleed through
    digitalWrite(dataPin, 0);
    }
   
      digitalWrite(clockPin, 0);
 
  }
   
Logged

Pages: 1 [2]   Go Up
Jump to: