Show Posts
|
|
Pages: [1] 2 3 ... 5
|
|
3
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Random max & min/max function C code
|
on: November 12, 2009, 06:15:00 pm
|
Correct. I would like to make a function for future use. So what I am really interested in is the code used in the the Arduino function or comparable code to write a similar function. I could always rewrite that example or the code inside the Arduino function each time but I'm much to lazy for that.
|
|
|
|
|
4
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Random max & min/max function C code
|
on: November 12, 2009, 04:43:16 pm
|
To generate a random number within a specified range. I saw this example online however I wanted to see the Arduino code version. If it differs I would prefer to use it as opposed to what I found. //generates a psuedo-random integer between 0 and max int randint(int max) { return int(max*rand()/(RAND_MAX+1.0)); }
//generates a psuedo-random integer between min and max int randint(int min, int max) { if (min>max) { return max+int((min-max+1)*rand()/(RAND_MAX+1.0)); } else { return min+int((max-min+1)*rand()/(RAND_MAX+1.0)); } }
|
|
|
|
|
6
|
Forum 2005-2010 (read only) / Syntax & Programs / Random max & min/max function C code
|
on: November 11, 2009, 04:58:30 pm
|
Hello, Would someone be able to point me to where I could find the C code used for the random max and random min/max functions. I would like to port them to an ATtiny project I am working on among other things. I've found various versions online however I've never had any issues using this function so If I could continue to use it that would be great.  Thanks
|
|
|
|
|
7
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Interrupts, 2 questions
|
on: October 10, 2008, 02:59:50 pm
|
Can any pin on arduino be used with interrupts, and can I have several of them? You can use digital pins 2 or 3 for interrupts see this page, http://arduino.cc/en/Reference/AttachInterruptIf the interrupt interrupts a main loop, after execution the function does it get back to the next command of the loop before interrupting or to beginning of the loop? In my experience to the next command in the loop, and I think the interrupt should always interrupt the main loop. Thats the only code running, nothing else need interruption  . Someone correct me if I am wrong.
|
|
|
|
|
8
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Interrupt question
|
on: September 08, 2008, 03:57:37 pm
|
Done! At lease with the frame of the sketch. I added "if (Mode != 2) break;" to each for statement, adjusting the 2 to whatever the # Mode it was in obviously. if (Mode == 2) { reset(); for (int greenVal=0; greenVal<=255; greenVal++) { analogWrite(greenLedPin, greenVal); if (Mode != 2) break; delay(10); } for (int greenVal=255; greenVal>=0; greenVal--) { analogWrite(greenLedPin, greenVal); if (Mode != 2) break; delay(10); } That seems to have done the trick. Ironic how after reading you advice the next chapter in my C book explained continue, break, and switch case. In any case thanks again.
|
|
|
|
|
9
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Interrupt question
|
on: September 04, 2008, 06:00:58 pm
|
For starters, thanks for the info. As for the pot input being more responsive, as soon as I read your response the fact that I should have put an analog read in each for statement became painfully obvious to me. I adjusted the code and it works great. I plan on looking into your second suggestion for this issue later just to expand my knowledge. When next I get some free time I plan on adding code to monitor the button presses in the fades as suggested, I'll let you know how that turns out. As far as the structure of my code, listed below is the hopefully not too poorly written sketch. I am slowly adding functionality as I go and troubleshooting as req'd. /* *RGB LED LAMP PROJECT * */ volatile int Mode = 0;
int fast = 0;
int redPotPin = 1; // select the input pin for the potentiometer int greenPotPin = 2; int bluePotPin = 3;
int redLedPin = 9; // select the pin for the LED int greenLedPin = 10; int blueLedPin = 11;
int redVal = 0; // variable to store the value int greenVal = 0; int blueVal = 0;
void setup() {
attachInterrupt(0, next, RISING); // Button attached to interrupt 0, digital pin 2, // to trigger fuction next when button goes from low to high Serial.begin(9600);
pinMode(redLedPin, OUTPUT); // declare the ledPin as an OUTPUT pinMode(greenLedPin, OUTPUT); pinMode(blueLedPin, OUTPUT); }
void loop(){
// Now do whatever the Mode indicates if (Mode == 0) { redVal = analogRead(redPotPin); // read the value from the sensor analogWrite(redLedPin, redVal/4); // convert from 0-1023 to 0-255 and apply value to LED
greenVal = analogRead(greenPotPin); analogWrite(greenLedPin, greenVal/4);
blueVal = analogRead(bluePotPin); analogWrite(blueLedPin, blueVal/4); }
if (Mode == 1) { { analogWrite(greenLedPin, 0); analogWrite(redLedPin, 0); analogWrite(blueLedPin, 0); fast = analogRead(redPotPin)/50; fast = max(fast, 1); // limits fast to a min. of 1 by selecting } // The higher of the two values ,fast or 1, // and asigining it to fast. for (int redVal=0; redVal<=255; redVal++) { analogWrite(redLedPin, redVal); fast = analogRead(redPotPin)/50; fast = max(fast, 1); delay(fast); } for (int redVal=255; redVal>=0; redVal--) { analogWrite(redLedPin, redVal); fast = analogRead(redPotPin)/50; fast = max(fast, 1); delay(fast); } }
if (Mode == 2) { for (int greenVal=0; greenVal<=255; greenVal++) { analogWrite(greenLedPin, greenVal); delay(10); } for (int greenVal=255; greenVal>=0; greenVal--) { analogWrite(greenLedPin, greenVal); delay(10); }
} if (Mode == 3) { for (int blueVal=0; blueVal<=255; blueVal++) { analogWrite(blueLedPin, blueVal); delay(10); } for (int blueVal=255; blueVal>=0; blueVal--) { analogWrite(blueLedPin, blueVal); delay(10); }
} }
void next() { if (Mode == 0) { // Mode = 1; // } else { if (Mode == 1) { // Mode = 2; // } else { if (Mode == 2) { // Mode = 3; // } else { if (Mode == 3) { // above 3 it returnes to 0 Mode = 0; // } } } } }
Any further input/suggestions are welcome. Thanks again.
|
|
|
|
|
10
|
Forum 2005-2010 (read only) / Syntax & Programs / Interrupt question
|
on: September 04, 2008, 04:10:17 pm
|
|
Greetings,
I used AttachInterupt and a tactile switch to cycle between several RGB LED sequences/fades. When the button is pressed the mode will change however it only changes after the full code of the current sequence completes. For instance if you press the button in during the fades two for statements the current sequence completes prior to moving to the next sequence, or the one after the next if you press the button twice.
Is there a way to have the program respond to a button press in real time?
Also I use a pot to control the speed of the fade in one of the sequences. Again I must wait till both for statements complete until the duration of the delay, controlled by the pot, is reassessed. Is it possible to have this value update in real time?
Thanks again.
|
|
|
|
|
12
|
Forum 2005-2010 (read only) / Syntax & Programs / speed..... function?
|
on: September 01, 2008, 08:31:01 am
|
|
I was writhing a sketch and went to name an int "speed" however the word turned orange, as far as I could tell as I am partially color blind, as if it was a function or something. When I searched the extended reference section of the website I found no mention of such a command.
Anyone know what this is?
|
|
|
|
|
14
|
Forum 2005-2010 (read only) / Syntax & Programs / Error w/auto format, but compiles fine?
|
on: June 27, 2008, 02:43:05 pm
|
Maybe i should not even care because the program compiles and runs fine as far as i can tell but when i hit auto format i get the following error message. Binary sketch size: 6776 bytes (of a 14336 byte maximum)
java.lang.ArrayIndexOutOfBoundsException: 10 at processing.app.tools.AutoFormat.show(AutoFormat.java:561) at processing.app.Editor$15.actionPerformed(Editor.java:728) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1882) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2202) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258) at javax.swing.AbstractButton.doClick(AbstractButton.java:334) at javax.swing.AbstractButton.doClick(AbstractButton.java:282) at javax.swing.plaf.basic.BasicMenuItemUI$Actions.actionPerformed(BasicMenuItemUI.java:1020) at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1576) at javax.swing.JComponent.processKeyBinding(JComponent.java:2772) at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(JMenuBar.java:656) at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(JMenuBar.java:664) at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(JMenuBar.java:664) at javax.swing.JMenuBar.processKeyBinding(JMenuBar.java:640) at javax.swing.KeyboardManager.fireBinding(KeyboardManager.java:255) at javax.swing.KeyboardManager.fireKeyboardAction(KeyboardManager.java:242) at javax.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:2849) at javax.swing.JComponent.processKeyBindings(JComponent.java:2841) at javax.swing.JComponent.processKeyEvent(JComponent.java:2735) at java.awt.Component.processEvent(Component.java:5360) at java.awt.Container.processEvent(Container.java:2010) at java.awt.Component.dispatchEventImpl(Component.java:4050) at java.awt.Container.dispatchEventImpl(Container.java:2068) at java.awt.Component.dispatchEvent(Component.java:3885) at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1826) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:681) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:938) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:810) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:645) at java.awt.Component.dispatchEventImpl(Component.java:3923) at java.awt.Container.dispatchEventImpl(Container.java:2068) at java.awt.Window.dispatchEventImpl(Window.java:1791) at java.awt.Component.dispatchEvent(Component.java:3885) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
This started popping up after i added 3 else statements in addition to the original 5. any ideas.... should i even be concerned?
|
|
|
|
|