Help with array!

int digitalCount [] = {
{
  digitalWrite(pin2, 0);
  digitalWrite(pin3, 0);
  digitalWrite(pin4, 0);
  digitalWrite(pin5, 0);    // Sets the number to 0.
  digitalWrite(pin6, 0);
  digitalWrite(pin7, 0);
  digitalWrite(pin8, 1);
},
{
  digitalWrite(pin2, 1);
  digitalWrite(pin3, 0);
  digitalWrite(pin4, 0);
  digitalWrite(pin5, 1);
  digitalWrite(pin6, 1);
  digitalWrite(pin7, 1);
  digitalWrite(pin8, 1);
}
}

I know there is a way of controlling the 7 segments with the byte function but I'm not a good enough coder to figure it out so I'm trying to create an array as you can see but I'm not even sure if its possible.

Any help greatly appreciated!

byte digitalCount[10][7] = {                               { 0,0,0,0,0,0,1 },  // = 0
                                                           { 1,0,0,1,1,1,1 },  // = 1
                                                           { 0,0,1,0,0,1,0 },  // = 2
                                                           { 0,0,0,0,1,1,0 },  // = 3
                                                           { 1,0,0,1,1,0,0 },  // = 4
                                                           { 0,1,0,0,1,0,0 },  // = 5
                                                           { 0,1,0,0,0,0,0 },  // = 6
                                                           { 0,0,0,1,1,1,1 },  // = 7
                                                           { 0,0,0,0,0,0,0 },  // = 8
                                                           { 0,0,0,1,1,0,0 }   // = 9
                                                           };

Got this byte array kind of setup but not sure how to use it.

Really could use some help here :<

byte pins[] = {2,3,4,5,6,7,8};

digit = 3;  // let's set pins for the digit 3

for (int i = 0; i < 7; i++) {
   digitalWrite(pins[i], digitalCount[digit][i]);
}

Personally I would pack all those bytes so you only need an array 10 byte long instead of 10x7. Maybe like this

byte digitalCount[10] = {  
//          abcdefg        
           B0000001,  // = 0
	   B1001111,  // = 1
	   B0010010,  // = 2
	   B0000110,  // = 3
	   B1001100,  // = 4
	   B0100100,  // = 5
	   B0100000,  // = 6
	   B0001111,  // = 7
	   B0000000,  // = 8
	   B0001100   // = 9
	   };

byte pins[] = {2,3,4,5,6,7,8};

digit = 3;  // let's set pins for the digit 3
byte x = digitalCount[digit];
 
for (int i = 0; i < 7; i++) {
   digitalWrite(pins[i], ((x & 1) == 1) ? HIGH : LOW);
   x >>= 1;
}

I don't have a working IDE at present to test but it should give you the idea.


Rob

Hey thanks for the reply.

While I was waiting I came up with this:

//
//
//
//				    --a--
//				 f |     | b
//				   |--g--|                for reference
//				 e |     | c
//				    --d--

// Define the LED digit patterns, from 0 - 9
//                                    { a,b,c,d,e,f,g }
//                         Arduino pin: 2,3,4,5,6,7,8

int uparray []={
1,0,0,1,1,1,1,
0,0,1,0,0,1,0,
0,0,0,0,1,1,0,
1,0,0,1,1,0,0,
0,1,0,0,1,0,0,
0,1,0,0,0,0,0,
0,0,0,1,1,1,1,
0,0,0,0,0,0,0,
0,0,0,0,1,0,0,
0,0,0,0,0,0,1
};

/*
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int pin8 = 8;
int pin9 = 9;
*/

const int threshold = 200;    // used to determine if receiver is receiving
int sensorPin = A0;           // selects the analogue input pin for the IR transmitter/receiver
int sensorValue = 0;          // varible that stores the value coming from the sensor

void setup() {
  for (int t=2;t<9;t=t+1){  
  pinMode (t,OUTPUT);}
  digitalWrite (pin9, 1);  // controls dot - currently set off.
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(sensorPin);    // reads the value from IR transmitter/receiver
  int k=0;
  for (int t=0;t<10;t++){
  for (int j=2;j<9;j++){
    digitalWrite (j, uparray[k]);
  if (sensorValue > threshold) {
    k=k+1;
    //k=k+1;
  }
  delay(2000);}
  Serial.println(sensorValue);
  
  }
}

This is my whole code.

I'm trying to increase the number on my display by 1 whenever my IR beam is broken. But with this code the display just goes a bit wonky.

Going to look into what you've posted to see what I can do. I'm quite a terrible coder so sorry if what I've posted is gobbledygook.

Anyone? :[

Well ... just looking at it quickly you aren't testing your analogvalue

This

void loop() {
  sensorValue = analogRead(sensorPin);    // reads the value from IR transmitter/receiver
  int k=0;
  for (int t=0;t<10;t++){
  for (int j=2;j<9;j++){
    digitalWrite (j, uparray[k]);
  if (sensorValue > threshold) {
    k=k+1;
    //k=k+1;
  }
  delay(2000);}
  Serial.println(sensorValue);
  
  }

Should be more like this (I'm wrinting this free hand so you will have to check it)

int currentValue = 0;

void loop() {
  sensorValue = analogRead(sensorPin);    // reads the value from IR transmitter/receiver
  boolean changeLED = false;
  if (sensorValue == 1) { // 1 might not be the right value you will have to see what sensorValue is when the beam is broken
     changeLED = true;
     currentValue = currentValue + 1;   // Increase the number we want to display * would be a good place to put a max value like
     if (currentValue >9){
       currentValue = 9;
     }
  }
  if (changeLED == true){
    for (int j=2;j<9;j++){ 
       digitalWrite (j, ((uparray[j][currentValue] & 1) == 1) ? HIGH : LOW);  // Set all the pins either on or off based on the number we want to display
       // ie if our currentValue = 0 then 
       // uparray[2][0] = 1 which needs to be high
    }
  }
}

I see I just updated the code using a digital reading as it seems to give a way more accurate response.

//
//
//
//				    --a--
//				 f |     | b
//				   |--g--|                for reference
//				 e |     | c
//				    --d--

// Define the LED digit patterns, from 0 - 9
//                                    { a,b,c,d,e,f,g }
//                         Arduino pin: 2,3,4,5,6,7,8

int uparray []={
1,0,0,1,1,1,1,
0,0,1,0,0,1,0,
0,0,0,0,1,1,0,
1,0,0,1,1,0,0,
0,1,0,0,1,0,0,
0,1,0,0,0,0,0,
0,0,0,1,1,1,1,
0,0,0,0,0,0,0,
0,0,0,0,1,0,0,
0,0,0,0,0,0,1
};


int pin9 = 9;
int sensorPin = 12;           // selects the digital input pin for the IR transmitter/receiver
int sensorValue = 0;          // varible that stores the value coming from the sensor

void setup() {
  for (int t=2;t<9;t=t+1){  
  pinMode (t,OUTPUT);}
  digitalWrite (pin9, 1);  // controls dot - currently set off.
  Serial.begin(9600);
}

void loop() {
  sensorValue = digitalRead(sensorPin);    // reads the value from IR transmitter/receiver
  int k=0;
  for (int t=0;t<10;t++){
  for (int j=2;j<9;j++){
    digitalWrite (j, uparray[k]);
  if (sensorValue == LOW) {
    k=k+1;
    //k=k+1;
  }
  }
  delay(1000);}
  Serial.println(sensorValue);
  
  }
}

Going to see if I can implement your code somehow.

Thanks for helping, I have to present this tomorrow so all help is truly appreciated.

Oook using your code like this:

//
//
//
//				    --a--
//				 f |     | b
//				   |--g--|                for reference
//				 e |     | c
//				    --d--

// Define the LED digit patterns, from 0 - 9
//                                    { a,b,c,d,e,f,g }
//                         Arduino pin: 2,3,4,5,6,7,8

int uparray []={
1,0,0,1,1,1,1,      // Sets display to 1
0,0,1,0,0,1,0,      // Sets display to 2
0,0,0,0,1,1,0,      // Sets display to 3
1,0,0,1,1,0,0,      // Sets display to 4
0,1,0,0,1,0,0,      // Sets display to 5
0,1,0,0,0,0,0,      // Sets display to 6
0,0,0,1,1,1,1,      // Sets display to 7
0,0,0,0,0,0,0,      // Sets display to 8
0,0,0,0,1,0,0,      // Sets display to 9
0,0,0,0,0,0,1       // Sets display to 0
};

/*
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int pin8 = 8;
*/
int pin9 = 9;
int sensorPin = 12;           // selects the analogue input pin for the IR transmitter/receiver
int sensorValue = 0;          // varible that stores the value coming from the sensor
int currentValue = 0;
int k=0;                      // sets position of array


void setup() {
  for (int t=2;t<9;t=t+1){  
  pinMode (t,OUTPUT);}
  digitalWrite (pin9, 1);  // controls dot - currently set off.
  Serial.begin(9600);
}

void loop() {
  sensorValue = digitalRead(sensorPin);    // reads the value from IR transmitter/receiver
  boolean changeLED = false;
  if (sensorValue == 0) {
    changeLED = true;
    currentValue = currentValue + 1;
    if (currentValue >9){
      currentValue = 0;
    }
  }
    if (changeLED == true){
  //for (int t=0;t<10;t++){
  for (int j=2;j<9;j++){
    digitalWrite (j, ((uparray[j][currentValue] & 1 == 1 ? HIGH : LOW));
  }
  Serial.println(sensorValue);
  
  }
}

Its giving me these errors:

sketch_mar22a.cpp: In function 'void loop()':
sketch_mar22a:62: error: invalid types 'int[int]' for array subscript

I'm quite the noob so I'm not too sure what its saying.

All I want to happen is for the array to increase whenever the beam is tripped. Sounds so simple :[

Currently whenever the beam is tripped a series of 0s are read from the digitalInput and while it is active a series of 1s.

There must be an easy solution.

Are you wanting a single pulsed increment per cover/uncover event pair.

Yes exactly that and I've re-written the code:

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F 7
#define G 8
#define DP 9

#define SENSOR 12    // Sensor connected to pin 12

int count = 0;      // current display count

const byte numbers[10] = { 
  0b1000000, 
  0b1111001, 
  0b0100100, 
  0b0110000, 
  0b0011001, 
  0b0010010,
  0b0000010, 
  0b1111000, 
  0b0000000, 
  0b0010000 
};

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(SENSOR, INPUT);
  lightSegments(0b1000000);
}

void loop() {
  int val = digitalRead(SENSOR);
  if (val == LOW) {
    count++;
    if (count == 10) count = 0;
    delay(500);
    lightSegments(numbers[count]);
  }
}

void lightSegments(byte number) {
  for (int i = 0; i < 7; i++) {
    int bit = bitRead(number, i);
    // segments connected to Arduino pins 2-8
    digitalWrite(i+2, bit);
  }
}

This code doesn't interfere with the 7-segment and is very reliable compared to my last attempts.

Thanks for reply :]

And from an earlier post I assume this is homework and not personal interest project?

Didn't you just make a post about F being undefined? And then delete it?

Well, I was investigating during the post's short life.

So here's a heads-up. There is a macro called F introduced in version 1.0 of the IDE. It's for putting strings into Flash memory. As in:

Serial.println (F("hello world"));

So maybe F is a bad choice for your define.

Nick, wrong thread?

You need to split things up as there are two main things

a) is the counter working
b) is the display working

a)
replace

lightSegments(numbers[count]);

with

Serial.println(count);

And check the results on the serial monitor. That will prove if the counter is working correctly.

b)
replace loop() with

void loop() {
    count++;
    if (count == 10) count = 0;
    delay(500);
    lightSegments(numbers[count]);
  }
}

Then there is the delay(500); is the sensor ever active for longer than 500mS? If so you will execute this code again as soon as the delay is finished.

Learn to break things down into their component parts and test each part, when they all work by themselves there's a good chance they will work together.


Rob

Nah Nick is correct lloyd I didn't know whether to post my complete new code as part of this topic or in a new one and when you replied I decided to put it here.

Ah I didn't know about that thing,thanks for the heads-up Nick.

It's part of a project and I was supposed to show it off tomorrow but ran into big problems when I had to do some proper coding.

@Graynomad

The sensor is pretty much going to be active 24/7. And I've spent the last 10 or so hours making sure the display is working and finally about an hour ago its working flawlessly. The sensor input went wonky a few times but that as well seems to be gone.

Thanks for replies folks.

The sensor is pretty much going to be active 24/7.

How long is each activation though? If > 500mS you have a problem because you only test for a LOW level not an edge.


Rob

lloyddean:
Nick, wrong thread?

Not really.

He had:

#define F 7

In WString.h is:

#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal)))

So "F" is redefined. Thus a bad choice for his define. And because of the way the IDE juggles things around, his define will be overwritten.

@Gray i don't really know what you mean, all I can say is the beam is constant and the receiver gives a constant stream of 1s to the digital pin input while un-interrupted and whenever an object/person interrupts the beam the receiver then gives a constant stream of 0s.

I need however to alter the code so that when the beam is interrupted that I get a single pulsed increment per cover/uncover event pair as lloyd stated. So that the display counts only on the initial interruption of the beam and not while its being interrupted.

lloyddean:
Nick, wrong thread?

Besides, the other thread was deleted. :slight_smile:

Just image how you would feel if someone posted "what is the meaning of life?". And just as you were about to type "42" (after considerable research) the thread was deleted!