Shop OBEX P1 Docs P2 Docs Learn Events
little project ... - Page 2 — Parallax Forums

little project ...

2456789

Comments

  • elecnoobelecnoob Posts: 123
    edited 2007-08-07 21:09
    ok,
    so i need an other way to compare the secuence of the led lighting up and the swith turning on/off
    because for the twister i need to first run the leds in a random order lets say for 5 diferent leds light up
    i need to be able to hold this order
    becasue after that i want to "remake " it with the switches
    if you get what i mean
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-07 21:11
    To review the basics of I/O on the Propeller:

    1) Each cog has a set of I/O registers including DIRA, OUTA, INA. They're wired up on the chip so that the default is for all I/O pins to be inputs and every cog will supply the actual state of the I/O pins if you read INA (whether the I/O pin is set for input or output).

    2) If any cog sets an I/O pin to output mode (by putting a 1 in the corresponding bit of its DIRA register), that pin becomes an output pin. The default logic level is low (roughly 0V).

    3) If any cog sets an output I/O pin to high (by putting a 1 in the corresponding bit of its OUTA register), that output pin will produce a high logic level (roughly 3.3V).

    4) Any cogs that are stopped (never started since the chip was reset or deliberately stopped by itself or a program in another cog) do not affect I/O. Their I/O registers are cleared to zeros which effectively prevents them from affecting the I/O pins.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-07 21:18
    elecnoob,
    You're going to need to define what the LEDs and the switches do very carefully and thoroughly. Obviously, you want to start with some kind of random sequence of LED lights. Do you want them to change simultaneously? Do you want the pattern to be random or do you want to randomly turn each LED on and off for some random period of time or what? Once the random sequence stops (when?), what do you want the switches to do? What kind of switches do you want to use ... pushbuttons? on-off toggle switches? on-off-on toggle switches? do you want them to be spring loaded, like momentary on-off-on/center off? Do you want one switch for each LED? If not, what do the switches do? Do you want to use a keypad? If so, what side? Again, what would you want the keypresses to do?

    You need to establish this before you do any coding.
  • elecnoobelecnoob Posts: 123
    edited 2007-08-07 21:33
    ok , i will try to explain what i want to do .
    i want to make a twister with 8 leds and 8 swithes (pushbuttons)
    i want the switches make a random sequence and you "remake" it with the switches
    i want to work in lvl's .. so first 1 led 1 push, 2 led's 2pushes,.... etc ( this is just a loop you make with a variable number)
    but i want to put on the led's on i/o port 10 till 18 and the siwtches on 19 till 27
    so for exaple i want to compare if led on i/oport 10 turned on/off ( a flash of a couple of seconds) that the switch on port 19 was switched on/off
  • elecnoobelecnoob Posts: 123
    edited 2007-08-08 14:26
    var
    long i
    long X 'random number
    long Z 'number for the outputarray

    con
    startpin = 10
    delay = 6_000_000

    pub main
    OutputReg (1,Z)

    pub twister
    repeat
    X:=startpin + ||i?//8 'set X to a random number from 0 to 8 , starpin for the output. if the random number =0 it will start at a defined pinnumber
    dira[noparse][[/noparse]X]:=1
    !outa[noparse][[/noparse]X]
    waitcnt(delay)

    if outa[noparse][[/noparse]x] := 1 'put pin X on 1 ( light op a led)
    outputreg (1,z) := x ' to save what outpin has gone to 1
    z:=z+1 ' z must add by 1 so the next won't overwrite the array

    can this work ..
    this would be the code for not ending loop that turns on a random led and saves the pin number in a array ?
    can this work ?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-08 14:54
    What's OutputReg? I don't see any call to twister. Spin executes the first PUB method in the source file (main) and that just calls OutputReg.

    Indenting is very important (crucial!) in Spin. Use [noparse][[/noparse] code ] (without the spaces) before your code and [noparse][[/noparse] /code ] after it in your postings to preserve the spacing.

    waitcnt(delay) won't do what you want. Remember that waitcnt waits for a specific time to arrive. The cnt register is the system clock and it's a 32 bit register that increments at the system clock rate. The waitcnt instruction (or Spin method) waits for cnt to equal the value given. If you want a delay of 6_000_000 system clock cycles, you have to use waitcnt(delay+cnt).

    I don't see any constant declarations for _xinfreq or _clkfreq and _clkmode. Without those, the Propeller will use the default RCFAST clock for the system clock. This is roughly 12MHz (see the manual for a description) and much slower than what you may expect.

    outa is a register and "remembers" which bits are set. Why not just do "if outa[noparse][[/noparse] z ] == 1" to see if that LED is on or off? (It's "==", not ":=") for an equal comparison.

    You're using a high level on an I/O pin to turn on an LED (a 1 bit). That's ok. That requires that the anode of the LED be connected to the I/O pin through a current limiting resistor and the cathode of the LED be connected to ground.

    Post Edited (Mike Green) : 8/8/2007 3:03:41 PM GMT
  • elecnoobelecnoob Posts: 123
    edited 2007-08-08 15:10
    outputreg would be the name of an array ..
    if i declare a array in the var section .. can i say that it hes no limit ?.. or do i just give it a very big numer ?

    and aboutr the _xinfreq they also don't do this in the excersise number 4 ( i think that this exc. has the most link with my code,
    or am i wrong ? )

    exc 4( pg 104 of the manual)

    and what does the "cnt " in the delay stand for . because i don't get that it's not defined in the program
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-08 15:28
    If you declare an array, you have to give a size. If you don't give a size, it's not an array. Look at the section of the manual on VAR for an explanation.

    They don't use things like _xinfreq in the early exercises because it's not needed there and would just complicate things. READ THE MANUAL about anything you don't recognize including _xinfreq and cnt. Read my postings ... I just gave an explanation of CNT. If you don't understand what I wrote, ask for clarification (like "OK, I understood this ..., but what does this ... mean?) or READ THE MANUAL. There's an index in the back and all of these are listed. There's also a list in the manual of pre-defined names, whether they're used in the assembly language or Spin or both.
  • elecnoobelecnoob Posts: 123
    edited 2007-08-08 15:51
    var
    byte outputreg[noparse][[/noparse]15]            ' array with 15 parts
    
    con
        _clkmode = xtal1 + pll2x              'external low-speed crystal
        _Xinfreq = 3_000_000                  '2*3Mhz = 6Mhz crystal is set on 6MHZ
    
    



    so this is for the array and the clock
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-08 16:18
    The array declaration is ok, but the clock stuff won't work the way you've done it.

    The manual states that, to use the phase locked loop (PLL), you have to have a crystal or resonator with a frequency between 4MHz and 8MHz.
    A 3MHz crystal doesn't fit this. You would have to leave off the "+ pll2x" and the resulting system clock would be 3MHz ... OK, but low speed. If you actually have a 6MHz crystal, you'd use _xinfreq = 6_000_000 and, if you leave the "+ pll2x" there, you'd have a 12MHz system clock.

    To reference elements of outputreg, you use the notation "outputreg[noparse][[/noparse] x ]" where x is a value from 0 to 14 (for 15 elements). There's no checking for a valid subscript value. If you use a value that's too high, you'll access some other variable or part of your program's instructions.
  • elecnoobelecnoob Posts: 123
    edited 2007-08-08 16:42
    on my crystal ther e is this number 5000 does this mean that i have a 5Mhz crystal?
    var
    byte outputreg[noparse][[/noparse]15]            ' array with 15 parts
    
    con
        _clkmode = xtal1 + pll2x              'external low-speed crystal
        _Xinfreq = 5_000_000                  '2*5Mhz = 10Mhz crystal is set on 10MHZ
    
    



    so this would be right?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-08 16:48
    The 5000 does probably mean that it's a 5MHz crystal and your constant settings are correct for that.
  • elecnoobelecnoob Posts: 123
    edited 2007-08-08 17:06
    
    
    var 
     byte outputreg[noparse][[/noparse]15]
     byte inputreg[noparse][[/noparse]15]
    ( the others i use are longs)
    pub twister
    repeat 15
          X == startpin + ||i?//8            ' set X to a random number from 0 to 8 , starpin for the output. if the random number =0 it will start at a defined pinnumber
          dira[noparse][[/noparse]X] == 1                                        
          !outa[noparse][[/noparse]X]                           ' put the led on
          waitcnt(delay + cnt )              ' waiting time to hold the led lighted
          if outa[noparse][[/noparse]x] == 1                    ' put pin X on 1 ( light op a led)
            outputreg [noparse][[/noparse]z]== x                ' to save what outpin has gone to 1
            z == z + 1                       ' z must add by 1 so the next won't overwrite the array
            y == X + 8                       ' add 8 by X for the inputpin number ( pin 10 + 8 makes pin 18)
            
          if ina[noparse][[/noparse]y] == 1
            inputreg [noparse][[/noparse]j] == y
     
    if outputreg[noparse][[/noparse]v] == inputreg[noparse][[/noparse]v]
    
    



    this is what i have for the " saving of the pin numbers and the start of comparing with the if function

    Post Edited (elecnoob) : 8/8/2007 5:11:05 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-08 17:16
    Please note that there are two different operators, := and ==. The first is the assignment operator while the second is the equal comparison.

    I still don't understand why you need outputreg and inputreg. If you have 9 LEDs on pins 10-18 and 9 switches on pins 19-27, you can compare the settings with if ina[noparse][[/noparse] 19..27 ] == outa[noparse][[/noparse] 10..18 ]
  • elecnoobelecnoob Posts: 123
    edited 2007-08-08 17:47
    ok tnx for the := and == diference

    and i need to do this because later on i need to get 2 led's light up and after the 2 leds i need to do the switches so i need to remeber
    the order of the led's that's why i need to make the registers
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-08 18:03
    OK, I see why you need this.· A couple of questions and comments:

    1) You describe 8 LEDs and 8 switches and use a byte to store the pattern,·yet you mention 9 LED pins and 9 switch pins.· Is the 9th LED and 9th switch used for some other purpose?

    2) You can use another form of REPEAT statement like "repeat z from 0 to 15".· This takes care of initializing and incrementing z.

    3) You calculate a random pin number from 0 to 7 for a sequence of up to 16 values.· Some of these will be identical.· The way your code is written, the LED will be turned off the second time the same pin is chosen.· Is that what you want?· It's reasonable, but complicates the game.
    ·
  • elecnoobelecnoob Posts: 123
    edited 2007-08-08 18:43
    1) i don't think i mentioned 9 led an 9 switches .. but if i did so .;
    i'm srry i ment 8
    2) about the repeat: i cant' you this because i need to work with lvl's
    first 1 led than 2 led's. and if i use your repeat i always take 15 and with mine repeat you not
    3) so the led's will stay of till the second time they are seleted?
    but i just want to lat them light them up for let's say 1 second.. i thougt that's why i needed the wait so the cor wou'ld hold that stand of the port and after the wait it woul'd reset ..

    but then i need to add this line :
    dira[noparse][[/noparse]X] := 0
    after the wait to turn the led off... or am i wrong ?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-08 19:07
    You can still get two identical numbers from a random number generator ... It's random after all

    You can exit from any repeat by using next (to start the next repeat) or quit to quit the repeat.

    How are you going to do the levels?· What rules will you use?

    You can use DIRA[noparse][[/noparse] X ] := 0 to turn off the LED (by making the pin an input) or you can use OUTA[noparse][[/noparse] X ] := 0 to make the output pin a logic low (ground) level which also turns off the LED.
    ·
  • elecnoobelecnoob Posts: 123
    edited 2007-08-08 19:47
    for the lvl it's just if you pushed right there will light up 1 led more ...
    that's why i need the repeat can't be a number
    because i need do be able top add 1 ..

    i will use the OUTA[noparse][[/noparse] X ] := 0 for turning of the led i think it's better
  • elecnoobelecnoob Posts: 123
    edited 2007-08-09 11:13
    
    var 
     long i
     long X                      'random number
     long Z                      'number for the outputarray
     long v
     long w
     long j                      'number for the inputarray
    
     byte outputreg[noparse][[/noparse]16]
     byte inputreg[noparse][[/noparse]16]  
      
    con
      _clkmode = xtal1 + pll2x                'external low-speed crystal
      _Xinfreq = 5_000_000                    '2*5Mhz = 10Mhz crystal is set on 10MHZ
      startpin = 10
      delay = 10_000_000                      ' how long the light stays on
      
    pub twister
    repeat
      x == 0
      w == 1
      repeat
       z == 1
       j == 1
      
         repeat w
           if w=16
           quit
           return
          
           X := startpin + ||i?//8           ' set X to a random number from 0 to 8 , starpin for the output. if the random number =0 it will start at a defined pinnumber
           dira[noparse][[/noparse]X] := 1                                       
           !outa[noparse][[/noparse]X]                          ' put the led on
           waitcnt(delay + cnt )             ' waiting time to hold the led lighted
           if outa[noparse][[/noparse]x] == 1                   ' put pin X on 1 ( light op a led)
             outputreg [noparse][[/noparse]z] == x              ' to save what outpin has gone to 1
             z == z + 1                      ' z must add by 1 so the next won't overwrite the array
           OUTA[noparse][[/noparse]X] := 0
          
           if ina[noparse][[/noparse]18] == 1                   ' next lines are to see what switsh is pressed
             inputreg [noparse][[/noparse]j] == 10
             elseif ina[noparse][[/noparse]19] == 1
               inputreg[noparse][[/noparse]j] == 11
             elseif ina[noparse][[/noparse]20] == 1
               inputreg[noparse][[/noparse]j] == 12
             elseif ina[noparse][[/noparse]21] == 1
               inputreg[noparse][[/noparse]j] == 13
             elseif ina[noparse][[/noparse]22] == 1
               inputreg[noparse][[/noparse]j] == 14
             elseif ina[noparse][[/noparse]22] == 1
               inputreg[noparse][[/noparse]j] == 15
             elseif ina[noparse][[/noparse]23] == 1
               inputreg[noparse][[/noparse]j] == 16
             elseif ina[noparse][[/noparse]24] == 1
               inputreg[noparse][[/noparse]j] == 17
              j == j + 1
              
         v == 1
         repeat w
          if outputreg[noparse][[/noparse]v] == inputreg[noparse][[/noparse]v]      ' compare the values in the 2 arrays
             v == v + 1                      ' next aray   
             w == w + 1                      ' w stands for the lvl( times the lights turn on/off) if these 2 arayas are correct w+1
          else
             quit
             return
    
    



    so this is what i have for the program ...
    maybe 1 question .. the quit AND reurn .. do i need them both or can i get with a only 1 return out of ALL the loops at once?
  • elecnoobelecnoob Posts: 123
    edited 2007-08-09 13:03
    i've got also an other question ... how can i send out a signal to a speaker
    because when you're wrong a want to make a little muziek?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-09 14:18
    The return will never execute because the quit exits from the immediate repeat loop. If you take out the quit, the return will exit from twister and the program will stop.

    You could connect a piezoelectric speaker to a Propeller pin and use one of the counters in the cog to put out a specific frequency. Have a look at the Frequency Synthesis and Counter objects in the Propeller Object Exchange (see the main Parallax website).
  • elecnoobelecnoob Posts: 123
    edited 2007-08-09 14:51
    ok tnx i will look for the speakers ..

    about the repeat .. i had in mind to quit from all repeat's exept from the first one ..
    the program woul'd run till the cog is shutdown
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-08-09 15:24
    Have a look at repeat while loops it is unlikely you need any quits.

    Graham
  • elecnoobelecnoob Posts: 123
    edited 2007-08-12 20:40
    ok tnx ..
    a little question on the speaker thing
    in the program written for me there are thing betweem braccets do i need to hold them like thi s.. or do i need to replace with something alse?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-12 21:05
    There are several different kinds of "brackets". What kind are you talking about?
    If you're talking about the ones that look like this: "{" "}"
    These are used for multi-line comments.
    If you're talking about the ones that look like this: "[noparse][[/noparse]" "]"
    These are used for subscripting.
  • elecnoobelecnoob Posts: 123
    edited 2007-08-12 21:19
    
    obj
       Freq : "Synth"
    
    Freq.Synth( "A",Pin, Frequency) 
    
    
    



    i'm talking about the " " ...
    what do i need to do whit them ?
    leave them there ? or delete them ?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-12 21:49
    You're probably referring to the double quote characters. If you read the Propeller manual, you'll see that they're used as string delimiters.
    In the case of "Synth", the text within the quotes is the file name of the text file that contains the object source (with .spin on the end).
    In the case of "A", the character is passed as a number to Freq.Synth. Look at the Synth method in Synth.spin to see how it's used.

    The general rule in programming is: Don't take anything out unless you understand why it's there and you still want to take it out.
    A second rule is: Don't add anything unless you understand what you're doing and you still want to add it.

    In my forum postings, I sometimes use the double quote (or single quote) characters to set off a piece of text that I want to be taken literally.
    It's not always grammatically correct, but is still useful at times even so.
  • elecnoobelecnoob Posts: 123
    edited 2007-08-14 14:11
    so the "synth" is a kind of call from an other program ..
    so if i'm uploading my code to the chip .. i need to upload the 2 files .. or what ?
    and how can i make a "call" inside the same program?
    because i want to make a "main" pub where i call the twister pub in so that if i make other games . i can call them aswell ..
    ( a looked in the manual but i didn't found something like a call comand)
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-14 15:15
    Please, please read the manual, particularly the introductory bits. You are asking very basic questions that are answered in the Propeller Programming Tutorial section. You will have to go through it. Then ask for clarification if you need it.
Sign In or Register to comment.