Show Posts
Pages: 1 2 3 [4]
46  Using Arduino / Project Guidance / Re: switch case question on: January 09, 2013, 08:22:27 am
Quote from: AWOL link=topic=141626.msg1064083#msg1064083 date=1357736627/
could be written
Code:
byte Display[23] =  //define 7seg digit pattern
{
  0b01100000,  // "1"
  0b11011010,  // "2"
  0b11110010,  // "3"
  0b01100110,  // "4"
//and so on until
  0  // off
};
?


OK, I think I see where you are going with this smiley
the way I wrote it, it has to remember every char each time the display is activated, where as the way you wrote it, cherry-picks the line requested and leaves out the rest?
at least that is the best way I can come up with to explain it, sorry communication isn't my strong suit...

forgive me for asking, but what exactly does the "0b" do at the beginning (starting to think I am answering my own question here) of each line.
My first thought on that would be, the "0" sets the start point, and "b" tells the arduino where to begin reading each line.  It digitalWrites each pin I/0 until it hits the next "0b" where it stops.
47  Using Arduino / Project Guidance / Re: switch case question on: January 09, 2013, 07:56:32 am
@AWOL

thanks for the reply smiley

TBH I have only had my arduino for less than two weeks, and haven't wrote a sketch large enough to even begin wondering how much is "too much".  (at least not as far as I know.  haven't "hit the wall" yet and had to figure out why)

If you don't mind, could you explain how it is too much, and maybe give me a clue as to how to start making it more efficient.

I do notice that your estimate of how much I am over-using RAM matches the [8] at the end of the quote, I am guessing there is a relationship?

The bit of code I learned that array function from was very sparsely commented, and I had to figure out what much of it meant by trial and error.
If I made a change that didn't work with my sketch, I would go back and change something else until I got closer to what I wanted.
There are even a few lines in there that I am not completely clear on what they are doing.  (which you may have guessed at by the vagueness of my own commenting)

Also, the original sketch where that line came from was made to just count down from 9 and start over automatically, with no control input.  I adapted it for what I wanted, the best I know how so far.  I assumed that the way it was wrote simply told the arduino how many digit to look for to define each byte that was displayed on the 7seg.

Thank you for the honest reply.  I know sometimes people don't want to crit someone else' work, but I am here to learn, and that means doing it wrong until I know how to do it right....

any suggestions for improvement, or a point in the right direction is greatly appreciated!
48  Using Arduino / Project Guidance / Re: switch case question on: January 09, 2013, 01:35:50 am
well, I got it done, and it works great!

I worked it out both ways, though the first way I did it was buggy and clumsy, but it worked.
I am quite sure I can improve upon it, and might try someday smiley

I also took the advice I was given on this forum and make a v2.0
It took a bit longer to finish than I was hoping for, but it's a pretty slick little device, if you don't mind translating a single digit display into a two digit answer...
but no ICs are used off the board, and it has a dice range of 2, 4, 6, 8, 10, 12, and 20
will post the code for review and possible suggestions for improvement, cause I love to learn, and there seem to be many ways to do about anything with the Arduino, and some are much easier than others smiley-razz

Quote
/*  this is a sketch to use a single 7 segment display as many different kinds of dice.
    the choices of dice value are 2, 4, 6, 8, 10, 12, and 20, based around a complete set of D&D dice values
    it uses a button press to select and display which mode of dice you are using
    and a tilt switch for shake-to-roll activation of random number generator.
    no external ICs were harmed (or used) in the making of this sketch */

byte Display[23][8] =  //define 7seg digit patern
{
  {0,1,1,0,0,0,0,0},  // "1"
  {1,1,0,1,1,0,1,0},  // "2"
  {1,1,1,1,0,0,1,0},  // "3"
  {0,1,1,0,0,1,1,0},  // "4"
  {1,0,1,1,0,1,1,0},  // "5"
  {1,0,1,1,1,1,1,0},  // "6"
  {1,1,1,0,0,0,0,0},  // "7"
  {1,1,1,1,1,1,1,0},  // "8"
  {1,1,1,0,0,1,1,0},  // "9"
  {1,1,1,1,1,1,0,1},  // "10"
  {0,1,1,0,0,0,0,1},  // "11"
  {1,1,0,1,1,0,1,1},  // "12"
  {1,1,1,1,0,0,1,1},  // "13"
  {0,1,1,0,0,1,1,1},  // "14"
  {1,0,1,1,0,1,1,1},  // "15"
  {1,0,1,1,1,1,1,1},  // "16"
  {1,1,1,0,0,0,0,0},  // "17"
  {1,1,1,1,1,1,1,1},  // "18"
  {1,1,1,0,0,1,1,1},  // "19"
  {0,1,1,0,1,1,0,1},  // "20"
  {1,1,1,1,1,1,0,0},  // "0"
  {0,1,1,1,1,0,1,0},  // "d"
  {0,0,0,0,0,0,0,0},  // off
};

int dValue[] =  //define dice value array
{2, 4, 6, 8, 10, 12, 20};

const int Button = 12;  //assign push button to pin 12
const int Tilt = 11;  //assign tilt switch to pin 11
int mode = 0;  //assign variable mode, and store
int oldMode = 0;  //assign variable old mode, and store
int val = 0;   //assign variable val, and store
int oldVal = 0;  //assign variable old val and store
int state = 0;   //assign variable state, and store

void setup()
{
  pinMode(Button, INPUT);  //assign each pin used as input or output
  pinMode(Tilt, INPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(13, OUTPUT);
  writeDot(0);  //start with "dot" off
 
  randomSeed(analogRead(0));  //read analog pin 0 to seed random number
}

void writeDot(byte dot)  //assign function writeDot as a byte named "dot"
{
  digitalWrite(9, dot);  //write dot to pin 9
}

void sevenSegWrite(byte digit)  //assign function sevenSegWrite to byte "digit"
{
  byte pin = 2;  //start writing sevenSegWrite at pin 2
  for (byte segCount = 0; segCount < 8; ++segCount)  // increpent segCount up by one
  {
    digitalWrite(pin, Display[digit][segCount]);  //Write "pin" 1 or 0 according to the defined array
    ++pin;  //advance to next pin
  }
}

void loop()
{
  {
    mode = digitalRead(Button);  //read push button
    if ((mode == HIGH) && (oldMode == LOW))  //check button state
    {
      state++;  //if button mode has changed, increment state up by one
      {
        sevenSegWrite(21);  //if state has gone up, display "d" on 7seg
        delay(500);      //delay .5 second
        sevenSegWrite(22);  //turn 7seg off
        delay(250);      // delay .25 second
        sevenSegWrite(dValue[state] - 1);  //display the dice value currently selected
        delay(500);     //delay .5 second
        sevenSegWrite(22);  //turn off 7seg
      }
    }
    if (state > 6)  //if state is greater than 6 roll back to 0 then delay 10mS
    {
      state = 0;
      delay(10);
    }
    oldMode = mode;  //old mode is now the mode
  }
 
  val = digitalRead(Tilt);  //read tilt switch
  if ((val == HIGH) && (oldVal== LOW))  //check for switch state change
  {
    {
      digitalWrite(9, HIGH);            //displays a . . . on 7 seg
      delay(250);                       //before showing dice result
      digitalWrite(9,LOW);              //did this for dice rolling effect
      delay(250);                       //dramatic pause kind of thing
      digitalWrite(9, HIGH);
      delay(250);
      digitalWrite(9,LOW);
      delay(250);
      digitalWrite(9, HIGH);
      delay(250);
      digitalWrite(9,LOW);
      delay(250);
    }
    {
      byte Result = random(dValue[state]);  //assign variable Result and set it equal to the random seed related to the state count as a upper limit
      sevenSegWrite(Result);  //display the result on 7seg
      delay(3000);  //delay 3 seconds
      sevenSegWrite(22);  //turn off 7seg
    }
  }
  digitalWrite(13, HIGH);  //turn on led @ pin 13 as "on" indicator light
}


let me know what you think!
49  Using Arduino / Project Guidance / Re: switch case question on: January 08, 2013, 04:50:26 pm
I don't see why you'd want to use a switch statement at all, for that problem. It seems to me that all you need is:

A constant array of integers holding the size of each die you're supporting.
A state variable which is an index into that array, identifying the currently-selected die.
Some event handling code to increment the selected die variable modulo the number of dice.
Some event handling code to generate a random number within the range of the currently-selected die and pass it to a display function.
A display function that renders a specified value on your display.

I'm not clear how you'd display any value over 9 on a seven-segment display, but presumably you have a scheme in mind.

ETA: Or in other words, I agree with Jimmy60. smiley

Thanks for the reply Peter!
since I will not need to display any number higher than 20, I am using the dot to indicate double digits.
1-9 are normal,
now for 10, you would see a 0 and the dot on the bottom lit up
like this - 0.
then 11 = 1.
12 = 2.
continue up to 19 = 9.
for 20 it will display what will look like l l.  (both sides lit up, all middle segments off and the dot on.  it's supposed to be the roman numeral II, best idea I could come up with) 

its a personal challenge, to learn as much as I can from each project by doing it two different ways.

edit:  plus, the random number generating method I am using here uses randomSeed(analogRead(0)) to generate the random number, then the switch case to light up the right segments of the display.
it was the easiest way I found to make the 6 sided I made last night.
when that worked out so well, I went for the bigger challenge of multiple dice modes.
50  Using Arduino / Project Guidance / Re: switch case question on: January 08, 2013, 04:40:41 pm
Yes you can but I don't see why you'd need to in this case.

It sounds to me like you select a mode once, then roll as often as you want.  You could use an array to store how many sides each mode represents then use the mode as the index. I don't really see a need for a switch statement at all but you must have another idea.

Code:
// I see it like this
int mode = 0;
int dice[] = {2, 4, 6, 8, 10, 12, 20}; // or whatever your dice are

void loop(){

// Check the button and increment mode with rollover (0..6)

// if the device is shaken call the random function (I'll assume it's seeded)

int rollResult = random(dice[mode]); // you may want to add one to this

// display roll result

}

// there is quite a bit of code assumed in these comments I was really just demonstrating my logic


lol, you actually jumped ahead of me a bit :p
right now, I am trying to work this out step by step.  Then once I have it working and I know why it works the way it does, I was planning on implementing an array to shorten it and learn some short cuts in the code.
I did this with my last project (sonar distance sensor with LED bar graph and Piezzo buzzer tones) and felt like I learned much more by figuring out two totally different ways to "skin this cat"

I have a lot of plans on things I want to build with the Arduino, and I am trying to focus on making projects that teach me code that I can use later for some of these bigger projects.
makes toys now to learn off of, and when I know better what I am doing, start working on bigger more complicated projects for my home smiley
working on getting "off grid" step by step, and I can see many ways in which Arduino will make this much easier!
Plus I am a tinker-er at heart, and thoroughly enjoy the process.

Thanks for the advice, and when I get this one working today, I am sure I will be back to work out how to work in arrays and lose the switches smiley
51  Using Arduino / Project Guidance / Re: switch case question on: January 08, 2013, 03:41:14 pm
excellent!
thank you both for the quick replies!

I had this idea last night, then changed my mind when I started writing the code.  I got about halfway through the code as I was writing it last night, and decided to test it.  At first it would just run the first step in a loop, regardless of any buttons or switches.  I managed to fix that, but then it wouldn't change modes, I was stuck at mode 0(2 sided).  Not to mention it was getting quite long and complicated.

Today I have decided to scrap that, and go with my original idea, because I think it will write up much more cleanly.
errr, I hope smiley-wink


@HazardsMind Yes, this would be the pyramid dice.  My inspiration for this project is a digital set of D&D dice, so those are the modes I am using.
I only have one 7seg display at the moment, so I am using the dot at the bottom to indicate double digit numbers.

it's been an educational project, and really builds on many of the tutorials I have done so far smiley
52  Using Arduino / Project Guidance / (not a ) switch case question (anymore...) on: January 08, 2013, 03:18:46 pm
Ok, I have a quick question about using switch cases.

Is it possible to use a switch case within a switch case?

my goal here is to make a 7 segment display dice roller.  I have already successfully made a shake-to-roll 6 sided dice (shake the breadboard and the 7seg will display 1-6 "randomly")
Now as a personal challenge, I am trying to make one 7seg display work as many different kinds of dice.
2 sided (coin flip), 4 sided, 6 sided, 8 sided.... so on, up to a 20 sided dice.
this would basically be a whole set of gaming dice in one little unit, fully controlled with the Arduino, no ICs.

I am doing this by using a press button to switch between dice modes (how many sides)
and using a tilt switch to trigger the shake-to-roll function, which will give a result on the 7seg that will be limited by which dice mode you are in.

I hope I am not being confusing...

here is an example -

default mode will be 0
mode 0 is a 2 sided coin flip or yes/no
button press
now it is mode 1 which is a 4 sided dice.
so on and so on up to mode 7 which is 20 sided, and then reset to default  mode 0

I am not to the point of posting the code and asking specific questions yet, I am trying to work this out on my own.  I am very new to writing code and micro controllers, and I am trying to learn, which I do best while problem solving.  
All I really need to know for the moment is if you can use a switch case to change dice modes, and use a sub-switch case? to give the results also.

sorry for being so long, but I really wanted to be clear, because I am also still learning how to communicate with the Arduino crowd smiley-razz
53  Using Arduino / Project Guidance / Re: What instrument displays a histogram of voltage and current? on: January 07, 2013, 07:31:19 pm
I have heard of DMMs with outputs on them, which you could hook up to a data logger.
with two of them, you could track both voltage and amps and log the data and graph it.
I am sure it wouldn't be too hard to set up either.

hope this helps
54  Using Arduino / Project Guidance / Re: What instrument displays a histogram of voltage and current? on: January 07, 2013, 07:11:59 pm
they do make oscilloscopes with a memory in them to store data.  Not sure how much they cost.

Now I am wondering if you could use an arduino data logger with SD card to track that kind of data and make the histogram on a computer.

do they make oscilloscopes with a serial output?  I am sure someone makes them.

went and did a search, came up with this...

http://www.seeedstudio.com/depot/dso-nano-v2-p-681.html?cPath=174

$90 isn't too bad on price either
55  Using Arduino / Project Guidance / Re: Looking for input on: January 04, 2013, 06:40:34 pm
@wildbill
Thank you for the clarification smiley

I have to head off to work, but when I get home, I will give it a shot.  I have some idea of how to make both suggestions work, so I will see what I come up with.

I very much appreciate the advice!
thanks
N8
56  Using Arduino / Project Guidance / Re: Looking for input on: January 04, 2013, 08:31:10 am
@PeterH
Thank you so much for the reply and advice!

I was already tinkering with the sketch so I have been able to try out your suggestions.

I found the problem right away, and without having to change much at all.

I guess the problem was my own misunderstanding of the switch function.  I had mapped it out from 0-5 originally, and wrote my switch starting from zero, which would turn on the buzz early, and wouldn't allow it to cycle properly.  Of course at the time I was using a delay to pause the tone, which was screwing up everything...
I had changed my buzz map to start from one, which fixed most of my issues, but still had the buzz coming on a fraction of a second early, maybe 2cm before the LED came on.
after I changed the code to work around the delay, so I could take it out, I never changed the buzz map back to 0. 
After reading your suggestion, I looked over my code, and noticed I have the two maps valued differently, so I set them to match, and now the beeping and the solid tone both start immediately when they are supposed to.

Now to reinitializing the BUZZstate to HIGH.  I changed the int for BUZZstate to HIGH from LOW, and didn't really notice a difference. 
Every so often, the first beep will start 1/2 second after the LED lights up, as you said it might.  The tone starts perfectly though, so I am pleased about that.
is there another way to reinitialize the BUZZstate to HIGH that I am not thinking of?
perhaps somewhere at the end of the switch code?
sorry for my lack of experience here, it is very possible I haven't understood you correctly smiley-razz
57  Using Arduino / Project Guidance / Looking for input on: January 04, 2013, 05:05:18 am
Hello all!
I am new both to this forum and micro controllers (specifically Arduino, obviously smiley-razz ).
After a week of incredibly proud Arduino ownership, I have learned quite a lot.
I have been trying to fumble my way through using a Ping))) sensor, with a 6 LED bar graph, and have also tied in a piezo buzzer, to fashion a functional backup sensor for a car, or any other type of thing that having a "too close" alarm would come in handy for.
I know there are several examples of this on the web, but I am trying to learn my way through the programming language with this project, so I have adapted several different codes to fit my design, and tried to do it without help.
until now...

I will post the code below, but first, everything works beautifully and exactly how I want it,  Save for one tiny thing that really doesn't bother me, but if there is a fix for it, I would like to learn it and why it works.

I can get the Ping sensor to light up my LEDs as an object gets closer, in the right order, and when I want them to come on.  I am using 6 LEDs for my meter, and for the last two LEDs I want sound effects too.
a 1/2 second repeating beep for LED 5 to light up, and a solid tone for LED 6(the last one to light up, at the closest distance)


Right now, that is more or less what I get, but the tone kicks in just a bit before the LED actually lights up.
To finally get to my point, I would like the LED to light at the same time the pulse tone starts, and the last to light at the same time the solid tone starts.
Here is my code
Quote
#include <NewPing.h>
#define TRIGGER_PIN 8
#define ECHO_PIN 8
#define MAX_DISTANCE 400
#define ITERATIONS 10

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

int BUZZ = 10;

const int minDist = 0;
const int maxDist = 150;
int BUZZstate = LOW;
long prevBUZZ = 0;
long interval = 500;

const int ledCount = 6;

int ledPins[] =
{
  2, 3, 4, 5, 6, 7
};

void setup()
{
  pinMode(BUZZ, OUTPUT);
 
  for (int thisLED = 0; thisLED < ledCount; thisLED++)
  {
    pinMode(ledPins[thisLED], OUTPUT);
  }
  Serial.begin(115200);
}

void loop()
{
  delay(30);
  unsigned int uS = sonar.ping_median(ITERATIONS);
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM);
  Serial.print("cm");
 
  int ledLevel = map(uS / US_ROUNDTRIP_CM, 0, 161, ledCount, 0);
 
  for (int thisLED = 0; thisLED < ledCount; thisLED++)
  {
    if (thisLED < ledLevel)
    {
      digitalWrite(ledPins[thisLED], HIGH);
    } else {
      digitalWrite(ledPins[thisLED], LOW);
    }
  }
  {
    int Distance = map(uS / US_ROUNDTRIP_CM, minDist, maxDist, 6, 1);
    switch (Distance)
    {
      case 1:
      digitalWrite(BUZZ, LOW);
      break;
      case 2:
      digitalWrite(BUZZ, LOW);
      break;
      case 3:
      digitalWrite(BUZZ, LOW);
      break;
      case 4:
      digitalWrite(BUZZ, LOW);
      break;
      case 5:
      {
      unsigned long BUZZmillis = millis();
      if(BUZZmillis - prevBUZZ > interval)
      {
        prevBUZZ = BUZZmillis;
        if (BUZZstate == LOW)
        BUZZstate = HIGH;
        else
        BUZZstate = LOW;
        digitalWrite(BUZZ, BUZZstate);
      }
      }
      break;
      case 6:
      digitalWrite(BUZZ, HIGH);
      break;
    }
  } 
    }

sorry for the lack of comments in my code, this has been revised so many times, I was getting myself more confused with them in than out, so I took them out.  If anyone has any questions on why I used a particular line or section, ask away, and I will do my best to clarify.

if anyone can see anywhere I can make it more efficient and also get the last two LEDs to light and the correct tones to sound simultaneously, I would greatly appreciate it!

Looking for any advice, really, though a solution to my specific problem would be wonderful.

Thanks in advance to anyone who can shed some light on my path smiley
Pages: 1 2 3 [4]