Hey, this is my first post but I've been messing with the Arduino for several months now. My question is how can I separate a number, like if x=29 how can I make that into a 2 and a 9?
Like x=29 so y=2 and z=9? Thanks!
Divide the number by 10, and store the result. 29 / 10 = 2.
Then, use the modulo function, and store the result. 29 % 10 = 9;
Then, use the modulo function, and store the result 29 % 10 = 9;
I wonder which would be faster (or if the compiler optimizes) - modulo, or multiplying what you got in step 1 by 10, then subtracting from the original value?
:-?
I suspect that avoiding the division in the modulo function will have time, since that's already been done.
But, it's easier to see and explain what is happening with the modulo function. At least, for me.
Wow! That's so simple thank you so much for both of your answers!
So basically I was working on some code to display whatever numbers I wanted on a 2 digit 7 segment led. It works perfectly but I assume it uses way more cycles than necessary and that would probably show if I wanted to do more things at a time then just count up or down. I'm posting the code if anybody wants to critique it for me. Thanks!
int A = 12; // Declares the pin numbers for the various segments of the led
int B = 11;
int C = 10;
int D = 9;
int E = 8;
int F = 7;
int G = 6;
int DIGIT1 = 2; // Declares the pin numbers for the two digits of the display
int DIGIT2 = 3;
int X = 0; // Number to be displayed
int dTens; // Number in Tens place
int dOnes; // Number in the Ones place
long previousMillis = 0; //Works with the timer down below
void setup(){
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(DIGIT1, OUTPUT);
pinMode(DIGIT2, OUTPUT);
}
void loop(){
dTens = X / 10;
dOnes = X % 10;
if (millis() - previousMillis > 1000) { // Counts up by one every 1000 milliseconds
previousMillis = millis();
X = X + 1;
}
// If statements to tell what segments to display for the ones place
if (dOnes == 0) {
digitZero();
charOne();
}
if (dOnes == 1) {
digitOne();
charOne();
}
if (dOnes == 2) {
digitTwo();
charOne();
}
if (dOnes == 3) {
digitThree();
charOne();
}
if (dOnes == 4) {
digitFour();
charOne();
}
if (dOnes == 5) {
digitFive();
charOne();
}
if (dOnes == 6) {
digitSix();
charOne();
}
if (dOnes == 7) {
digitSeven();
charOne();
}
if (dOnes == 8) {
digitEight();
charOne();
}
if (dOnes == 9) {
digitNine();
charOne();
}
// If statements to declare the segments to display in the tens place
if (dTens == 0) {
digitZero();
charTen();
}
if (dTens == 1) {
digitOne();
charTen();
}
if (dTens == 2) {
digitTwo();
charTen();
}
if (dTens == 3) {
digitThree();
charTen();
}
if (dTens == 4) {
digitFour();
charTen();
}
if (dTens == 5) {
digitFive();
charTen();
}
if (dTens == 6) {
digitSix();
charTen();
}
if (dTens == 7) {
digitSeven();
charTen();
}
if (dTens == 8) {
digitEight();
charTen();
}
if (dTens == 9) {
digitNine();
charTen();
}
// Resets to 0
if (X==99){
X = 0;
}
}
// FUNCTIONS!!!!!
void charOne(){
digitalWrite(DIGIT1,HIGH);
delay(5);
digitalWrite(DIGIT1,LOW);
}
void charTen(){
digitalWrite(DIGIT2,HIGH);
delay(5);
digitalWrite(DIGIT2,LOW);
}
void digitZero(){
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
digitalWrite(F,LOW);
digitalWrite(G,HIGH);
}
void digitOne(){
digitalWrite(A,HIGH);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
digitalWrite(G,HIGH);
}
void digitTwo(){
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,HIGH);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
digitalWrite(F,HIGH);
digitalWrite(G,LOW);
}
void digitThree(){
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
digitalWrite(G,LOW);
}
void digitFour(){
digitalWrite(A,HIGH);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
}
void digitFive(){
digitalWrite(A,LOW);
digitalWrite(B,HIGH);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,HIGH);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
}
void digitSix(){
digitalWrite(A,LOW);
digitalWrite(B,HIGH);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
}
void digitSeven(){
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
digitalWrite(G,HIGH);
}
void digitEight(){
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
}
void digitNine(){
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,HIGH);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
}
void loop(){
dTens = X / 10;
dOnes = X % 10;
if (millis() - previousMillis > 1000) { // Counts up by one every 1000 milliseconds
previousMillis = millis();
X = X + 1;
}
// If statements to tell what segments to display for the ones place
if (dOnes == 0) {
digitZero();
charOne();
}
dTens and dOnes are computed before X is adjusted. Why? Looks to me like they should be computed AFTER X is adjusted, and only if X is adjusted.
if (dOnes == 0) {
digitZero();
charOne();
}
if (dOnes == 1) {
digitOne();
charOne();
}
Is there any value that dOnes could hold that would cause BOTH if tests to be true?
A switch statement would be a lot shorter.
Without thought for presentation. Shortened some for readability. And completely untested.
Another possibility.
/*
* <http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1283987316>
*/
#include "WProgram.h"
enum
{
// LED digit place pins
pinOnesDigit = 2
, pinTensDigit = 3
// LED segment pins
, pinS0 = 12
, pinS1 = 11
, pinS2 = 10
, pinS3 = 9
, pinS4 = 8
, pinS5 = 7
, pinS6 = 6
};
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
#define ONE_SECOND (1000)
void displayDigit(uint8_t digit)
{
static uint8_t pins[] = { pinS0, pinS1, pinS2, pinS3, pinS4, pinS5, pinS6 };
static uint8_t digits[] = { 0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, 0x7F, 0x7B };
uint8_t* p = pins;
uint8_t bit = 0x80;
while ( bit >>= 1)
{
digitalWrite(*p++, ((digits[digit] & bit) ? LOW : HIGH));
}
}
void onesPlace()
{
digitalWrite(pinOnesDigit, HIGH);
delay(5);
digitalWrite(pinOnesDigit, LOW);
}
void tensPlace()
{
digitalWrite(pinTensDigit, HIGH);
delay(5);
digitalWrite(pinTensDigit, LOW);
}
void loop()
{
// Number to be displayed
static int s_counter = 0;
static long s_ticksPrevious = 0;
// increment counter every second
long ticks = millis();
if ( (ticks - s_ticksPrevious) > ONE_SECOND)
{
s_counter = ++s_counter % 100;
s_ticksPrevious = ticks;
}
displayDigit(s_counter % 10);
onesPlace();
displayDigit(s_counter / 10);
tensPlace();
}
void setup()
{
static uint8_t pinsOut[] = { pinS0, pinS1, pinS2, pinS3, pinS4, pinS5, pinS6, pinOnesDigit, pinTensDigit };
for ( int i = 0; i < ARRAY_LENGTH(pinsOut); i++ )
{
pinMode(pinsOut[i], OUTPUT);
}
}