Show Posts
Pages: 1 ... 6 7 [8] 9 10 ... 231
106  Products / Arduino Yún / Re: Arduino Yún REST + Servomotor on: March 29, 2014, 03:29:47 am
What sort of value for valx do you get from
Code:
    valx = client.parseInt();
Serial.print() it to make sure that it is what you think it should be.
107  Using Arduino / Programming Questions / Re: Programming 2 hand anti tie down on: March 29, 2014, 03:07:43 am
Quote
As I consider the next steps, I believe I'll need to use an interrupt to restart the loop should either of the buttons be released during the cycle. 
An interrupt may, or may not be appropriate depending on what the 'cycle' is doing.  If you do use one then at the end of the interrupt the program will return to exactly the same place in the program that was being executed when it occurred and you have no control over that.  It may be more effective to check that both buttons are still pressed, easy to do with a function, before carrying out the dangerous part(s) of the cycle.

Quote
Of course this means I'll have to use hardware to de-bounce the switches, which should make my sketch a lot simpler.
I don't see that one follows the other, but I assume from what you have said that you are more comfortable with hardware than software so it could be your preferred solution.
108  Using Arduino / Programming Questions / Re: RGB LED Change color with button press help on: March 28, 2014, 01:52:57 pm
Code:
#include <Bounce2.h>
Bounce2 ?

Code:
// Instantiate a Bounce object with a 20 millisecond de-bounce time
Bounce::Bounce(const Bounce&)
Looks wrong, but then I have never used this library
109  Using Arduino / Programming Questions / Re: help with code for my sequential converter on: March 28, 2014, 01:08:35 pm
What's that delay() doing inside the ISR ?  It won't work.  Use the ISR to set a flag and turn the LED on/off in loop() depending on the value of the flag.  Make the flag a global volatile variable.
110  Using Arduino / Programming Questions / Re: Help with Arduino Uno servo code PLEASE!? on: March 28, 2014, 12:26:24 pm
Is this some what you were looking for?

You don't have to use this, but I thought it would nice to base something off of. This code is based off your original code, but isn't complete either so there is still room for improvment.


Code:
#include <Servo.h>
 
Servo myservo;
 
int pos = 0;    // variable to store the servo position
 
void setup()
{
  Serial.begin(9600); //start a serial port 9600 for debugging
  pinMode(7, INPUT);  //make pin 9 our input. This is port will determines power input or no power input (1 or 0)
}
 
 
void loop()
{
 
 
  Serial.println(digitalRead(7));  //print the value from pin 7 to the serial monitor
 
  if(digitalRead(7) == 1)        //if the value of pin 7 is equal to 1 (power is applied)
  {
    myservo.attach(9);     //attach the servo to pin 9
     
      while(pos < 180 && digitalRead(7) == 1)  // while'pos' is less than 180 AND pin 9 still equals 1
        {                                 
          myservo.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                    // waits 15ms for the servo to reach the position
          pos += 1 ;                //add 1 to 'pos'
        }
       
     myservo.detach();         //stop the servo

  }
 
    else if(digitalRead(7) == 0)   //else if pin 7 is equal to 0 (power is not applied)
      {
        myservo.attach(9);       //re-attach the servo to pin 9
         
          while(pos > 0 || pos < 5 && digitalRead(7) == 0)     //while 'pos' is greater than zero OR less than 5 AND pin 7 still equals 0 
            {                               
              myservo.write(pos);              // tell servo to go to position in variable 'pos'
              delay(15);                       // waits 15ms for the servo to reach the position
              pos -= 1;                     //subtract 1 from 'pos' 
            } 
     
      myservo.detach();                //stop the servo
     }
}
Why are the attach and detach commands where they are.  One attach in setup() would suffice.

Code:
      while(pos < 180 && digitalRead(7) == 1)  // while'pos' is less than 180 AND pin 9 still equals 1
Comment correct or code correct ?

Code:
while(pos > 0 || pos < 5 && digitalRead(7) == 0)     //while 'pos' is greater than zero OR less than 5 AND pin 7 still equals 0
Why 2 checks on the value of pos ?

Code:
pinMode(7, INPUT);  //make pin 9 our input. This is port will determines power input or no power input (1 or 0)
Comments don't match the code again and
Code:
pinMode(7, INPUT_PULLUP);
would probably be a better choice but we don't know how the OP has got the button wired.
111  Using Arduino / Programming Questions / Re: help with code for my sequential converter on: March 28, 2014, 12:15:12 pm
That sounds right unless the Micro is like the Leonardo and the interrupt pins are reversed.

Put your big program aside for now and write a small one that changes the state of an LED when the interrupt occurs.  Cause the interrupt with a button/switch/shorting 2 wires together.  Does it work ?
112  Using Arduino / Programming Questions / Re: help with code for my sequential converter on: March 28, 2014, 11:13:18 am
From this page http://arduino.cc/en/Main/arduinoBoardMicro it would seem that the Micro uses pins 2 and 3 for interrupts
Quote
External Interrupts: 0(RX), 1(TX), 2 and 3. These pins can be configured to trigger an interrupt on a low value, a rising or falling edge, or a change in value. See the attachInterrupt() function for details.

Which pin have you got interrupt input connected to ?
113  Using Arduino / Programming Questions / Re: PWM motor speed control with feedback on: March 28, 2014, 10:53:03 am
Code:
  switch (buttoncount){
       
    case 1:
        PORTB ^= sn_enable;
        PORTB ^= pwm1;
      while(true){
        OCR0A = 150;
      }
     
    case 2:
You don't have a break at the end of this case, which will cause problems, and won't
Code:
while(true)
always be true ?
114  Using Arduino / Programming Questions / Re: help with code for my sequential converter on: March 28, 2014, 10:05:21 am
Code:
attachInterrupt(0, injection, LOW);
Attaches the injection function to interrupt 0 which is on pin 2, at least on some, but not all Arduinos
See http://arduino.cc/en/Reference/AttachInterrupt
What board are you using ?
115  Using Arduino / Programming Questions / Re: Name variable with contents of a variable on: March 28, 2014, 07:18:24 am
Quote
What I was thinking of was runtime variable creation based on user input.
What would that achieve that cannot be done in other ways ?
116  Using Arduino / Programming Questions / Re: RGB LED Change color with button press help on: March 28, 2014, 07:16:22 am
Can I be clear ?  The examples compile but your program doesn't.  Is that right ?
Please bear with me and post the full path to the library.
117  Using Arduino / Programming Questions / Re: Pin Mode on: March 28, 2014, 06:48:49 am
Quote
i want to readanlog pin reading during this delay period ???
Then it's time for you to look at the BlinkWithoutDelay example in the IDE.
Note the start time and check frequently whether the timing period is up.  If yes then act on it, if no the do something else like reading a pin.
118  Using Arduino / Programming Questions / Re: PWM motor speed control with feedback on: March 28, 2014, 06:45:42 am
Code:
speed = 128;

while(true){
   OCR0A = speed;

duration = pulseIn(PD6,HIGH);

if (duration > desired){
speed --
}
else{
speed++
}
That's not your real code is it ?
119  Using Arduino / Programming Questions / Re: Pin Mode on: March 28, 2014, 06:37:06 am
The easy way is to use
Code:
delayMicroseconds(100);
between turning it on and turning it off.  Depending how accurate you want the timing to be you will need to tweak the parameter.

NOTE - during the delays nothing else can happen such as reading pins.  There are ways round this though.
120  Using Arduino / Programming Questions / Re: Programming 2 hand anti tie down on: March 28, 2014, 05:51:29 am
It is considerably easier to program this if you make one of the buttons the master and look for that being pressed first.  Save the time it was pressed and start looking for the second one to be pressed.  If/when it is pressed measure the time between the two presses and act accordingly.

Here is something to get you started
Code:
const byte buttonPin1 = 7;
const byte buttonPin2 = 8;
unsigned long firstButtonPressTime = 0;
unsigned long secondButtonPressTime = 0;
byte currentButton1State;
byte previousButton1State;
byte currentButton2State;
byte previousButton2State;

boolean waitingForSecondButton = false;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
}

void loop()
{
  currentButton1State = digitalRead(buttonPin1);
  if (currentButton1State == LOW && previousButton1State == HIGH)
  {
    firstButtonPressTime = millis();
    Serial.print("Button 1 pressed at ");
    Serial.println(firstButtonPressTime);
    waitingForSecondButton = true;
  }
  previousButton1State = currentButton1State;

  currentButton2State = digitalRead(buttonPin2);
  if (currentButton2State == LOW && previousButton2State == HIGH && waitingForSecondButton == true)
  {
    secondButtonPressTime = millis();
    Serial.print("Button 2 pressed at ");
    Serial.println(secondButtonPressTime);
    waitingForSecondButton = false;
    if (secondButtonPressTime - firstButtonPressTime <= 500 && waitingForSecondButton == false)
    {
      Serial.println("OK to do stuff");
    }
  }
  previousButton2State = currentButton2State;
}
As it stands the code contains no debouncing which will cause multiple button presses to be registered but that can be added.
Pages: 1 ... 6 7 [8] 9 10 ... 231