Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Controlling servo position with 2 switches, but servo shakes out of position on: March 13, 2014, 10:32:14 pm
After a few days of wrestling I found a solution that works for what I need. I could not figure out why the servo was doing what it was doing. tonight I learned about "else if" here is my solution and it works. A few days and a bunch of help from other posts. This forum is great.


Code:
#include <Servo.h>

Servo myservo;
const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin2 = 4;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the first pushbutton status
int buttonState2 = 0;         // variable for reading the second pushbutton status
void setup() {
 
  myservo.attach(9);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {   
       myservo.writeMicroseconds(600);  // set servo to one extreme
  }
 
  else if (buttonState2 == HIGH) {   
       myservo.writeMicroseconds(2300);  // set servo to other extreme
  }     
  else if (buttonState2 == LOW) {
      myservo.writeMicroseconds(1500);  // sets servo back to center when no input is given
                                        // or the servo will stay at the other positions until
                                        // a different input is made
  }
}

2  Using Arduino / Programming Questions / Controlling servo position with 2 switches, but servo shakes out of position on: March 13, 2014, 07:49:16 pm
I am trying to use two momentary contact switches to control the position of a servo.
I want the servo to rest at zero degrees. When I press Sw1 it should go to ninety degrees. When the button is released it should go back to zero degrees.

 When I press Sw2 it should go to 160 degrees. When the button is released it should go back to zero degrees.

I have coded the Arduino and Sw1 will do as programmed individually if I "comment out" the code related to Sw2. Sw2 will do as programmed individually if I "comment out" the code related to Sw1. but if I leave both sets of code in, the servo just shutters without moving to either position. This one set of my code wherein I used the button library.
I can not figure out why it is happening I am very new to programming so I am wondering if it is a hardware problem. I have an Arduino Uno and I have 10K resistors on both inputs. The servo has its own battery and I have tied the battery ground to the Arduino ground.


Code:
#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
 
int pos = 0;    // variable to store the servo position


const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin2 = 4;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;         // variable for reading the pushbutton status
void setup()
{
   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  // initialize the LED pin as an output:
  myservo.writeMicroseconds(1500);  // set servo to mid-point
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {   
    // turn LED on:   
    digitalWrite(ledPin, HIGH); 
    //myservo.write (170);
    myservo.writeMicroseconds(600);  // set servo to mid-point
  }
  else {
    myservo.writeMicroseconds(1500);  // set servo to mid-point
    //myservo.write (10);
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }

{
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState2 == HIGH) {   
    // turn LED on:   
    digitalWrite(ledPin, HIGH); 
    //myservo.write (170);
    myservo.writeMicroseconds(2300);  // set servo to mid-point
  }
  else {
    myservo.writeMicroseconds(1500);  // set servo to mid-point
    //myservo.write (10);
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

}

3  Using Arduino / Sensors / Meas sensor reading on: February 03, 2013, 11:39:14 pm
I have a circuit built using a Piezo vibration sensor large Meas with mass weight. I used the code similar to the knock sensor.

void setup()
{
  Serial.begin(9600); //
}
void loop()
{
      int val;
      val=analogRead(0);//Connect the sensor to analog pin 0
      //if (val>100)
      float rating = val*(500/1023.0);
      Serial.println(rating);
      //Serial.println(val,DEC);//
      //else Serial.println();
      delay(100);
}

It works very well and gives me streaming data to the serial monitor. What I can't figure out is a way to make it print just the highest value per impact. Each impact will occur and be followed by no activity for a couple seconds. Currently I am seeing pretty reliable data but I can't figure out what to do. I tried to change the delay and I tried to assign a value to different ranges without success.
Pages: [1]