Go Down

Topic: 595 display driver code (Read 851 times) previous topic - next topic

Mar 03, 2013, 06:54 pm Last Edit: Mar 03, 2013, 06:57 pm by IAM_Carbon_Based Reason: 1
I have 3 595 shift registers driving a 3 digit, 7 segment display. this is the code I'm using right now to drive everything.
I'm looking for ways to make my program smaller as I'm using 37% of the total space on my Arduino, and I think I might be able to shrink it a bit here. Can anyone recommend a better / more efficient way of doing this?
Note: I'm NOT using the first output pin on each register, just outputs B-H.

Edit: I accidentally a word. Also does using libraries increase code size more the writing your own code?(I'm using the stepper library)

Code: [Select]
void display(int disp){
/* This function will take a 3 digit INT and display the number on the 7 segment display */

int num[10] = {254,48,109,121,51,91,95,112,127,115};  // Display bits 0-9

unsigned int dc1; // 1st display digit
unsigned int dc2; // 2nd display digit
unsigned int dc3; // 3rd display digit

String disp_num = String(disp);

dc1 = disp_num[0]-48;  // Subtract 48 to get decimal value - Normally returned as a CHAR
dc2 = disp_num[1]-48;
dc3 = disp_num[2]-48;

digitalWrite(latch_pin, LOW);

shiftOut(data_pin, clk_pin, LSBFIRST, num[dc1]);
shiftOut(data_pin, clk_pin, LSBFIRST, num[dc2]);
shiftOut(data_pin, clk_pin, LSBFIRST, num[dc3]);

digitalWrite(latch_pin, HIGH);
}

AWOL

Code: [Select]
int num[10] = {254,48,109,121,51,91,95,112,127,115};no good reason for that to be an into array.
Cut it down to byte type.
Code: [Select]
unsigned int dc1;
Ditto

Post your code.
"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.

#2
Mar 03, 2013, 07:01 pm Last Edit: Mar 03, 2013, 07:08 pm by IAM_Carbon_Based Reason: 1
Here's the full program:
Sorry for the edit: Updated to reflect changes:

Code: [Select]
#include <arduino.h>
#include <Stepper.h>

/********************** Define Global Variables Here *************************/

Stepper s1(48, 2,3,4,5);
int s1_RPM;

int btn1 = 6; // Button 1 Pin
int btn2 = 7; // Button 2 Pin
int adj1 = 3; // Adjustment 1 input Pin

int btn1v = 0; // Value for Button 1
int btn2v = 0; // Value for Button 2


int en = 8; // Enable pin for SN754410 IC

int clk_pin = 10; // Pin for 595
int latch_pin = 11; // Pin for 595
int data_pin = 12; // Pin for 595



/********************** Custom Functions Here ***********************/



void fwd_rev(int b1, int b2, int RPM, int numstep){
/* This function controls the forward and reverse of the stepper motor.
Allows you to define the forward and reverse buttons to use.
As well as the minimum # of steps per function call the motor will operate at,

"b1" and "b2" are the values of the buttons controlling the motor.
"RPM" is self explanatory.
"numstep " is the minimum number of steps the motor should make as required by the stepper library.  */

s1.setSpeed(RPM);

if (b1 ==1 && b2 == 0){
digitalWrite(en, HIGH);
s1.step(numstep);
}

if (b1 == 0 && b2 == 1){
digitalWrite(en, HIGH);
s1.step(-numstep);

}

if(b1 == 0 && b2 == 0){
digitalWrite(en, LOW);
}
}




int adj_dial(int pin, int min, int max){
/* This function will read from an adjustment pin and return the value read.
the value is mapped between min and max values */

int adj_val = 0; // Value for Adj
int rval; // Return Value

adj_val = analogRead(pin);

rval = map(adj_val, 0, 1023, min, max);


return rval;
}




void display(int disp){
/* This function will take a 3 digit INT and display the number on the 7 segment display */

byte num[10] = {254,48,109,121,51,91,95,112,127,115};  // Display bits 0-9

byte dc1; // 1st display digit
byte dc2; // 2nd display digit
byte dc3; // 3rd display digit

String disp_num = String(disp);

dc1 = disp_num[0]-48;  // Subtract 48 to get decimal value - Normally returned as a CHAR
dc2 = disp_num[1]-48;
dc3 = disp_num[2]-48;

digitalWrite(latch_pin, LOW);

shiftOut(data_pin, clk_pin, LSBFIRST, num[dc1]);
shiftOut(data_pin, clk_pin, LSBFIRST, num[dc2]);
shiftOut(data_pin, clk_pin, LSBFIRST, num[dc3]);

digitalWrite(latch_pin, HIGH);
}


/****************************** Setup *********************************/

void setup() {

pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(en, OUTPUT);

pinMode(clk_pin, OUTPUT);
pinMode(latch_pin, OUTPUT);
pinMode(data_pin, OUTPUT);

// Serial.begin(9600);


}


/******************************** Main Loop ********************************/

void loop() {


btn1v = digitalRead(btn1);
btn2v = digitalRead(btn2);

s1_RPM = adj_dial(adj1, 100, 600);

display(s1_RPM);

fwd_rev(btn1v, btn2v, s1_RPM, 1);



}

AWOL

#3
Mar 03, 2013, 07:17 pm Last Edit: Mar 03, 2013, 07:21 pm by AWOL Reason: 1
Quote
String disp_num = String(disp);
Ditch String - simple arithmetic will save space and probably be more reliable.

Code: [Select]
int clk_pin = 10;Do you envisage pin numbers bigger than 255?

Code: [Select]
int adj_val = 0; // Value for Adj
int rval; // Return Value

adj_val = analogRead(pin);
You're about to give it a value, so why waste time and code setting to zero?
"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.


Quote
String disp_num = String(disp);
Ditch String - simple arithmetic will save space and probably be more reliable.


I'm new to this so that particular solution evades me, could you provide an example?

I'm already down to 28% YAY!, Thanks guys  XD

AWOL

If I give you the number 595, how would you reduce it to its component digits (hundreds, tens, units)?
"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.

OK so I'm down to 18% now and I've rid myself of String.
Never new about modulo operator, gonna have to get used to that one I like it.
You make me think AWOL i like that.

Optimized code:
Code: [Select]
/*
* XY_CNC.cpp
*
* Created: 3/2/2013 10:12:18 PM
* Author: Adam Bennett
* Description: Main Source
*/


#include <arduino.h>
#include <Stepper.h>

/********************** Define Global Variables Here *************************/

Stepper s1(48, 2,3,4,5);
word s1_RPM;

#define btn1 6 // Button 1 Pin
#define btn2 7 // Button 2 Pin
#define adj1 3 // Adjustment 1 input Pin

word btn1v = 0; // Value for Button 1
word btn2v = 0; // Value for Button 2


#define en 8 // Enable pin for SN754410 IC

#define clk_pin 10 // Pin for 595
#define latch_pin 11 // Pin for 595
#define data_pin 12 // Pin for 595



/********************** Custom Functions Here ***********************/



void fwd_rev(int b1, int b2, int RPM, int numstep){
/* This function controls the forward and reverse of the stepper motor.
Allows you to define the forward and reverse buttons to use.
As well as the minimum # of steps per function call the motor will operate at,

"b1" and "b2" are the values of the buttons controlling the motor.
"RPM" is self explanatory.
"numstep " is the minimum number of steps the motor should make as required by the stepper library.  */

s1.setSpeed(RPM);

if (b1 == 1 && b2 == 0){
digitalWrite(en, HIGH);
s1.step(numstep);
}

if (b1 == 0 && b2 == 1){
digitalWrite(en, HIGH);
s1.step(-numstep);

}

if(b1 == 0 && b2 == 0){
digitalWrite(en, LOW);
}
}




int adj_dial(int pin, int min, int max){
/* This function will read from an adjustment pin and return the value read.
the value is mapped between min and max values */

int rval; // Return Value

int adj_val = analogRead(pin);

rval = map(adj_val, 0, 1023, min, max);


return rval;
}




void display(int disp){
/* This function will take a 3 digit INT and display the number on the 7 segment display */

byte num[10] = {254,48,109,121,51,91,95,112,127,115};  // Display bits 0-9

word ones = disp % 10;
word tens = (disp - ones) % 100;
word hund = (disp - tens - ones) % 1000;

byte dc1 = ones;
byte dc2 = tens / 10;
byte dc3 = hund / 100;

digitalWrite(latch_pin, LOW);

shiftOut(data_pin, clk_pin, LSBFIRST, num[dc3]);
shiftOut(data_pin, clk_pin, LSBFIRST, num[dc2]);
shiftOut(data_pin, clk_pin, LSBFIRST, num[dc1]);

digitalWrite(latch_pin, HIGH);
}


/****************************** Setup *********************************/

void setup() {

pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(en, OUTPUT);

pinMode(clk_pin, OUTPUT);
pinMode(latch_pin, OUTPUT);
pinMode(data_pin, OUTPUT);

// Serial.begin(9600);


}


/******************************** Main Loop ********************************/

void loop() {


btn1v = digitalRead(btn1);
btn2v = digitalRead(btn2);

s1_RPM = adj_dial(adj1, 100, 600);

display(s1_RPM);

fwd_rev(btn1v, btn2v, s1_RPM, 1);



}

AWOL

Quote
You make me think AWOL i like that.
Thank you - you've made my day, and probably my week. :D
"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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy