Pages: [1]   Go Down
Author Topic: Doing multiplication in trinary not displaying correctly.  (Read 559 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone.  I am trying to make a program that multiplies two numbers in trinary and then displays the resulting number on 6 LEDs.  There are two input switches: the first one on pin 2 allows you to input the trinary number you want, and the second switch moves the program along in the multiplication process.  Here is the specifics of what I'm trying to accomplish:

Quote
Have your arduino do multiplication in trinary as follows:

Install two buttons.  The first button will be for entering numbers as above (push the button once to enter a 1, twice to enter a 2, etc).  The second button will be for multiplication and for clear.

Your arduino should operate like this procedure:

1.      Enter the first number on button 1.

2.      Press the button 2 for the multiplication symbol.

3.      Enter the second number on button 1.

4.      Arduino should display the first number times the second number in trinary.

5.      Press the button 2 to clear.

6.      Can repeat these steps to multiply a different two numbers without resetting the arduino.

I have created code that I think should work, but its not doing what I expect of it.  It allows me to enter in the first number on the first switch, and then when I click the second switch it allowed me to input the second number with the second switch.  Then when I hit the second switch again, nothing happens, and if I hit it again my program just restarts.  So what am I doing wrong, since this looks like it should work fine to me.

I will post my code below since it exceeds the max message size if I put it into this post.


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This part of the code seems to work fine:

Code:
int switchPin = 2;
int switchPin2 = 12;
int number1 = 0;
int number2 = 0;
int multNumber = 0;

int ledPins[] = {
  3,5,6,9,10,11};
int counter[] = {
  0,0,0,0,0,0};
int pinOn[] = {
  0,0,0,0,0,0};

boolean lastButton = LOW;
boolean lastButton2 = LOW;
boolean currentButton = LOW;
boolean currentButton2 = LOW;
boolean Stop = LOW;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(switchPin2, INPUT);

  for( int i = 0; i < 6; i++)
  {
    pinMode(ledPins[i], OUTPUT);
  }
}


void loop()
{
  do
  {
    currentButton = debounce( lastButton );
    currentButton2 = debounce2 ( lastButton2 );

    if(lastButton == LOW && currentButton == HIGH)
    {
      number1 = number1 + 1;
      counter[0] = counter[0] + 1;

      //For 1 slot.
      if(counter[0] == 1)
      {
        pinOn[0] = 128;
      }
      else if(counter[0] == 2)
      {
        pinOn[0] = 254;
      }
      else if(counter[0] == 3)
      {
        counter[0] = 0;
        pinOn[0] = 0;
        counter[1] = counter[1] + 1;
      }
      else
      {
        counter[0] = 0;
        pinOn[0] = 0;
      }

      //For 2 slot.
      if(counter[1] == 1)
      {
        pinOn[1] = 128;
      }
      else if(counter[1] == 2)
      {
        pinOn[1] = 254;
      }
      else if(counter[1] == 3)
      {
        counter[1] = 0;
        pinOn[1] = 0;
        counter[2] = counter[2] + 1;
      }
      else
      {
        counter[1] = 0;
        pinOn[1] = 0;
      }

      //For slot 4
      if(counter[2] == 1)
      {
        pinOn[2] = 128;
      }
      else if(counter[2] == 2)
      {
        pinOn[2] = 254;
      }
      else if(counter[2] == 3)
      {
        counter[2] = 0;
        pinOn[2] = 0;
        counter[3] = counter[3] + 1;
      }
      else
      {
        counter[2] = 0;
        pinOn[2] = 0;
      }

      //For slot 8
      if(counter[3] == 1)
      {
        pinOn[3] = 128;
      }
      else if(counter[3] == 2)
      {
        pinOn[3] = 254;
      }
      else if(counter[3] == 3)
      {
        counter[3] = 0;
        pinOn[3] = 0;
        counter[4] = counter[4] + 1;
      }
      else
      {
        counter[3] = 0;
        pinOn[3] = 0;
      }

      //For slot 16
      if(counter[4] == 1)
      {
        pinOn[4] = 128;
      }
      else if(counter[4] == 2)
      {
        pinOn[4] = 254;
      }
      else if(counter[4] == 3)
      {
        counter[4] = 0;
        pinOn[4] = 0;
        counter[5] = counter[5] + 1;
      }
      else
      {
        counter[4] = 0;
        pinOn[4] = 0;
      }

      //For slot 32
      if(counter[5] == 1)
      {
        pinOn[5] = 128;
      }
      else if(counter[5] == 2)
      {
        pinOn[5] = 254;
      }
      else if(counter[5] == 3)
      {
        counter[5] = 0;
        pinOn[5] = 0;
      }
      else
      {
        counter[5] = 0;
        pinOn[5] = 0;
      }
    }

    for( int i = 0; i < 6; i++)
    {
      analogWrite(ledPins[i], pinOn[i]);
    }

    if(lastButton2 == LOW && currentButton2 == HIGH)
    {       
      Stop = HIGH;
    }

    lastButton = currentButton;
    lastButton2 = currentButton2;
  }
  while (Stop == LOW);

  lastButton = LOW;
  lastButton2 = LOW;
  currentButton = LOW;
  currentButton2 = LOW;
  pinOn[0] = 0;
  pinOn[1] = 0;
  pinOn[2] = 0;
  pinOn[3] = 0;
  pinOn[4] = 0;
  pinOn[5] = 0;
  counter[0] = 0;
  counter[1] = 0;
  counter[2] = 0;
  counter[3] = 0;
  counter[4] = 0;
  counter[5] = 0;
  Stop = LOW;

  do
  {
    currentButton = debounce( lastButton );
    currentButton2 = debounce2 ( lastButton2 );

    if(lastButton == LOW && currentButton == HIGH)
    {
      number2 = number2 + 1;
      counter[0] = counter[0] + 1;

      //For 1 slot.
      if(counter[0] == 1)
      {
        pinOn[0] = 128;
      }
      else if(counter[0] == 2)
      {
        pinOn[0] = 254;
      }
      else if(counter[0] == 3)
      {
        counter[0] = 0;
        pinOn[0] = 0;
        counter[1] = counter[1] + 1;
      }
      else
      {
        counter[0] = 0;
        pinOn[0] = 0;
      }

      //For 2 slot.
      if(counter[1] == 1)
      {
        pinOn[1] = 128;
      }
      else if(counter[1] == 2)
      {
        pinOn[1] = 254;
      }
      else if(counter[1] == 3)
      {
        counter[1] = 0;
        pinOn[1] = 0;
        counter[2] = counter[2] + 1;
      }
      else
      {
        counter[1] = 0;
        pinOn[1] = 0;
      }

      //For slot 4
      if(counter[2] == 1)
      {
        pinOn[2] = 128;
      }
      else if(counter[2] == 2)
      {
        pinOn[2] = 254;
      }
      else if(counter[2] == 3)
      {
        counter[2] = 0;
        pinOn[2] = 0;
        counter[3] = counter[3] + 1;
      }
      else
      {
        counter[2] = 0;
        pinOn[2] = 0;
      }

      //For slot 8
      if(counter[3] == 1)
      {
        pinOn[3] = 128;
      }
      else if(counter[3] == 2)
      {
        pinOn[3] = 254;
      }
      else if(counter[3] == 3)
      {
        counter[3] = 0;
        pinOn[3] = 0;
        counter[4] = counter[4] + 1;
      }
      else
      {
        counter[3] = 0;
        pinOn[3] = 0;
      }

      //For slot 16
      if(counter[4] == 1)
      {
        pinOn[4] = 128;
      }
      else if(counter[4] == 2)
      {
        pinOn[4] = 254;
      }
      else if(counter[4] == 3)
      {
        counter[4] = 0;
        pinOn[4] = 0;
        counter[5] = counter[5] + 1;
      }
      else
      {
        counter[4] = 0;
        pinOn[4] = 0;
      }

      //For slot 32
      if(counter[5] == 1)
      {
        pinOn[5] = 128;
      }
      else if(counter[5] == 2)
      {
        pinOn[5] = 254;
      }
      else if(counter[5] == 3)
      {
        counter[5] = 0;
        pinOn[5] = 0;
      }
      else
      {
        counter[5] = 0;
        pinOn[5] = 0;
      }
    }

    if(lastButton2 == LOW && currentButton2 == HIGH)
    {
      Stop = HIGH;
    }


    for( int i = 0; i < 6; i++)
    {
      analogWrite(ledPins[i], pinOn[i]);
    }

    lastButton = currentButton;
    lastButton2 = currentButton2;

  }
  while (Stop == LOW);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This part of the code does not work correctly:

Code:
multNumber = number1 * number2;
  lastButton2 = LOW;
  currentButton2 = LOW;
  pinOn[0] = 0;
  pinOn[1] = 0;
  pinOn[2] = 0;
  pinOn[3] = 0;
  pinOn[4] = 0;
  pinOn[5] = 0;
  counter[0] = 0;
  counter[1] = 0;
  counter[2] = 0;
  counter[3] = 0;
  counter[4] = 0;
  counter[5] = 0;
  Stop = LOW;

  for( int i = 0; i < multNumber; i++)
  {
    counter[0] = counter[0] + 1;

    //For 1 slot.
    if(counter[0] == 1)
    {
      pinOn[0] = 128;
    }
    else if(counter[0] == 2)
    {
      pinOn[0] = 254;
    }
    else if(counter[0] == 3)
    {
      counter[0] = 0;
      pinOn[0] = 0;
      counter[1] = counter[1] + 1;
    }
    else
    {
      counter[0] = 0;
      pinOn[0] = 0;
    }

    //For 2 slot.
    if(counter[1] == 1)
    {
      pinOn[1] = 128;
    }
    else if(counter[1] == 2)
    {
      pinOn[1] = 254;
    }
    else if(counter[1] == 3)
    {
      counter[1] = 0;
      pinOn[1] = 0;
      counter[2] = counter[2] + 1;
    }
    else
    {
      counter[1] = 0;
      pinOn[1] = 0;
    }

    //For slot 4
    if(counter[2] == 1)
    {
      pinOn[2] = 128;
    }
    else if(counter[2] == 2)
    {
      pinOn[2] = 254;
    }
    else if(counter[2] == 3)
    {
      counter[2] = 0;
      pinOn[2] = 0;
      counter[3] = counter[3] + 1;
    }
    else
    {
      counter[2] = 0;
      pinOn[2] = 0;
    }

    //For slot 8
    if(counter[3] == 1)
    {
      pinOn[3] = 128;
    }
    else if(counter[3] == 2)
    {
      pinOn[3] = 254;
    }
    else if(counter[3] == 3)
    {
      counter[3] = 0;
      pinOn[3] = 0;
      counter[4] = counter[4] + 1;
    }
    else
    {
      counter[3] = 0;
      pinOn[3] = 0;
    }

    //For slot 16
    if(counter[4] == 1)
    {
      pinOn[4] = 128;
    }
    else if(counter[4] == 2)
    {
      pinOn[4] = 254;
    }
    else if(counter[4] == 3)
    {
      counter[4] = 0;
      pinOn[4] = 0;
      counter[5] = counter[5] + 1;
    }
    else
    {
      counter[4] = 0;
      pinOn[4] = 0;
    }

    //For slot 32
    if(counter[5] == 1)
    {
      pinOn[5] = 128;
    }
    else if(counter[5] == 2)
    {
      pinOn[5] = 254;
    }
    else if(counter[5] == 3)
    {
      counter[5] = 0;
      pinOn[5] = 0;
    }
    else
    {
      counter[5] = 0;
      pinOn[5] = 0;
    }
  }

  for( int i = 0; i < 6; i++)
  {
    analogWrite(ledPins[i], pinOn[i]);
  }

  do
  {
    currentButton2 = debounce2 ( lastButton2 );

    if(lastButton2 == LOW && currentButton2 == HIGH)
    {
      Stop = HIGH;
    }

    lastButton2 = currentButton2;
  }
  while (Stop == LOW);

  lastButton = LOW;
  lastButton2 = LOW;
  currentButton = LOW;
  currentButton2 = LOW;
  pinOn[0] = 0;
  pinOn[1] = 0;
  pinOn[2] = 0;
  pinOn[3] = 0;
  pinOn[4] = 0;
  counter[0] = 0;
  counter[1] = 0;
  counter[2] = 0;
  counter[3] = 0;
  counter[4] = 0;
  Stop = LOW;
  number1 = 0;
  number2 = 0;
  multNumber = 0;
}

boolean debounce( boolean last )
{
  boolean current = digitalRead(switchPin);

  if( last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }

  return current;
}


boolean debounce2( boolean last2 )
{
  boolean current2 = digitalRead(switchPin2);

  if( last2 != current2)
  {
    delay(5);
    current2 = digitalRead(switchPin2);
  }

  return current2;
}


Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It allows me to enter in the first number on the first switch, and then when I click the second switch it allowed me to input the second number with the second switch.  Then when I hit the second switch again, nothing happens, and if I hit it again my program just restarts.

Is the bit in bold above correct? It doesn't seem to meet the requirements.

If it was me, I'd remove a lot of the duplication in that sketch by defining a function to input a number on switch 1, that ended when switch 2 was operated. This would be easy to test in isolation. THen your sketch would just call this twice, multiply the two numbers together and display the result.

I would also hold the numbers just as plain old integers instead of your array of trinary characters, and I'd do the number-to-trinary-display-format conversion at the point of displaying the number. You have to implement this anyway to display the answer, so you might as well do it to display the entered values. (This is easy enough to do by using div and mod to get the least significant digit repeatedly until the remainder is zero.)

IMO the crucial thing to keep in mind is that binary, hex, trinary, decimal etc are all just ways to display the number and do not change the number itself - you don't need to define a special representation of trinary numbers and write your own arithmetic operators and so on - they are just numbers, and only become trinary numbers at the point you format them (in trinary) for display. I would guess that your six LEDs are intended to provide three trinary digits each consisting of 0, 1 or 2 LEDs on. But maybe you have some other cunning way in mind to represent trinary digits using LEDs?
« Last Edit: April 30, 2012, 03:52:47 am by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
        pinOn[0] = 128;
 ...
        pinOn[0] = 254;
...
        pinOn[0] = 0;
...
 
and so on ...


I dislike "magic numbers". What are these 128, 254 and 0 of which you speak? What does it all mean? If they are "equivalent" given them a constant name, like foo, bar, and fubar. And explain what the constants mean. (I'm jesting about the exact names ... use ones that mean something).
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  counter[0] = 0;
  counter[1] = 0;
  counter[2] = 0;
  counter[3] = 0;
  counter[4] = 0;
  counter[5] = 0;

And read up on "for" loops.

Code:
for (byte i = 0; i <= 5; i++)
  counter [i] = 0;
Logged

Pages: [1]   Go Up
Jump to: