what’s your query?

The below comment section in this page is dedicated to the discussions regarding the category of contents apt for this site. Feel free to share with us any of your doubts, Questions, messages, etc

5 Responses

  1. Diego k Axtmann says:

    Hi there can anyone help me combine these two codes

    /*
    *Blink
    *Turns on an LED on for one second, then off for one second, repeatedly.
    *The circuit:
    * LED connected from digital pin 13 to ground.
    * Note: On most Arduino boards, there is already an LED on the board
    * connected to pin 13, so you don’t need any extra components for this example.

    *Created 1 June 2005
    *By David Cuartielles
    *http://arduino.cc/en/Tutorial/Blink
    *based on an orginal by H. Barragan for the Wiring i/o board
    */

    int ledPin = 13; // LED connected to digital pin 13

    // The setup() method runs once, when the sketch starts

    void setup() {
    // initialize the digital pin as an output:
    pinMode(ledPin, OUTPUT);
    }

    // the loop() method runs over and over again,
    // as long as the Arduino has power

    void loop()
    {
    digitalWrite(ledPin, HIGH); // set the LED on
    delay(1000); // wait for a second
    digitalWrite(ledPin, LOW); // set the LED off
    delay(1000); // wait for a second
    }

    With this speaker code

    /* Melody
    * (cleft) 2005 D. Cuartielles for K3
    *
    * This example uses a piezo speaker to play melodies. It sends
    * a square wave of the appropriate frequency to the piezo, generating
    * the corresponding tone.
    *
    * The calculation of the tones is made following the mathematical
    * operation:
    *
    * timeHigh = period / 2 = 1 / (2 * toneFrequency)
    *
    * where the different tones are described as in the table:
    *
    * note frequency period timeHigh
    * c 261 Hz 3830 1915
    * d 294 Hz 3400 1700
    * e 329 Hz 3038 1519
    * f 349 Hz 2864 1432
    * g 392 Hz 2550 1275
    * a 440 Hz 2272 1136
    * b 493 Hz 2028 1014
    * C 523 Hz 1912 956
    *
    */

    int speakerPin = 9;

    int length = 15; // the number of notes
    char notes[] = “ccggaagffeeddc “; // a space represents a rest
    int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
    int tempo = 300;

    void playTone(int tone, int duration) {
    for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
    }
    }

    void playNote(char note, int duration) {
    char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
    int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

    // play the tone corresponding to the note name
    for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
    playTone(tones[i], duration);
    }
    }
    }

    void setup() {
    pinMode(speakerPin, OUTPUT);
    }

    void loop() {
    for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
    delay(beats[i] * tempo); // rest
    } else {
    playNote(notes[i], beats[i] * tempo);
    }

    // pause between notes
    delay(tempo / 2);
    }
    }

    • admin says:

      Try this,

      int ledPin = 13; // LED connected to digital pin 13
      // The setup() method runs once, when the sketch starts
      int speakerPin = 9;
      int length = 15; // the number of notes
      char notes[] = "ccggaagffeeddc"; // a space represents a rest
      int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
      int tempo = 300;
      void setup() {
       // initialize the digital pin as an output:
       pinMode(ledPin, OUTPUT);
       pinMode(speakerPin, OUTPUT);
      }
      // the loop() method runs over and over again,
      // as long as the Arduino has power
      void loop() {
       for (int i = 0; i < length; i++) {
         if (notes[i] == ' ') {
           delay(beats[i] * tempo); // rest
         } else {
           playNote(notes[i], beats[i] * tempo);
         }
         // pause between notes
         delay(tempo / 2);
       }
       digitalWrite(ledPin, HIGH); // set the LED on
       delay(1000); // wait for a second
       digitalWrite(ledPin, LOW); // set the LED off
       delay(1000); // wait for a second
      }
      void playTone(int tone, int duration) {
       for (long i = 0; i < duration * 1000L; i += tone * 2) {
         digitalWrite(speakerPin, HIGH);
         delayMicroseconds(tone);
         digitalWrite(speakerPin, LOW);
         delayMicroseconds(tone);
       }
      }
      void playNote(char note, int duration) {
       char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
       int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
       // play the tone corresponding to the note name
       for (int i = 0; i < 8; i++) {
         if (names[i] == note) {
           playTone(tones[i], duration);
         }
       }
      }
      
  2. kimarni says:

    how can i make an motor fan with 3 different push button,by pressing each i get a different speed using arduino

    • admin says:
      /* Set a speed value between 0 to 255 for each variable
      speed_value1,speed_value2,speed_value3*/
      if (digitalRead(2) == HIGH) {
       // Pin - o/p to the motor from any of the PWM pin.
       analogWrite(Pin, speed_value1);
      }
      if (digitalRead(3) == HIGH) {
       analogWrite(Pin, speed_value2);
      }
      if (digitalRead(4) == HIGH) {
       analogWrite(Pin, speed_value3);
      }
      
  3. Santiago says:

    Hi, can anyone help me with my project? I am trying to control one servo motor with two different inputs. The first input is a photosensitive module that reads a high or low light reading to move to either 0 degrees or 95 degrees. The second input is a momentary n/o switch used as a type of manual control. It changes the servo to 0,95,135, or 180 degrees. The problem I’m having is when the switch is activated, the servo doesn’t stay at that position, instead it returns to the positions dictated by the light sensor. So how do I edit my code to do what I want? Thank you in advance! Here’s my code:
    #include

    //CONFIGURATION
    int servoPin = 9; //Input pin for servo
    int photocellPin = 7; // Input pin for photocell module
    const int button_pin = 2 ; //Button for manual control
    const int killswitch = 4 ; //Disables all automated actions when enabled
    int sensorPin = 1; //IR Reflective motion sensor pin
    const int ledPin = 13; // select the pin for the LED
    int pos = 90; // variable to store the servo position
    int spd = 20; // how fast the servo moves

    Servo myservo; // create servo object to control a servo
    int photocell_value; // the digital reading from the photocell module
    int button_value; //high or low button values
    int killswitch_value; //high or low killswitch values
    int state = 0; //Keep track of state so we don’t send signal to the servo without reason
    int prevstate = 0; //Keeps history of prior state
    //int sensorValue = 0; // variable to store the value coming from the sensor
    int dest = 0; // Servo destination depending on photocell reading
    int buttonpos = 1; //Keep state of motion sensor/button for movement
    int manualmode = 0;
    int time_base = 0;
    int time_now;
    int time_diff;

    //ENABLE DEBUG
    int debug = 1; //Set this to 1 for serial debug output
    void setup(void) {
    Serial.begin(9600); //Comment out this line if you don’t want debugging enabled

    myservo.attach(servoPin);
    pinMode(ledPin, OUTPUT);
    pinMode(button_pin, INPUT_PULLUP);
    pinMode(killswitch, INPUT_PULLUP);

    }

    //MAIN LOOP
    void loop(void) {
    killswitch_value = digitalRead(killswitch);
    if (killswitch_value == HIGH) //IF kill switch isn’t set then run main loop
    {
    photocell_value = digitalRead(photocellPin); //Query photo cell
    debug and Serial.print(“Light Reading :”);
    debug and Serial.print(photocell_value); // the raw digital reading
    debug and Serial.print(” | Pos: “);
    debug and Serial.print(pos);
    debug and Serial.print(” | State: “);
    time_now = millis();
    //Define the modes based on how bright it is, and set corresponding servo position
    if (photocell_value != HIGH) {
    button_press();
    debug and Serial.println(“Night”);
    dest = 0;
    state = 1;
    manualmode = 0;
    }

    //After setting LDR position, hold until not longer within current threshold
    else if (photocell_value == HIGH) {
    button_press();
    debug and Serial.println(“Day”);
    dest = 95;
    state = 2;
    }
    else {
    debug and Serial.println(photocell_value);
    debug and Serial.println(“Not in range”);
    button_press();
    }

    time_diff = time_now – time_base;
    if (state != prevstate && time_diff > 60000 && manualmode == 0); { //IF the photocell reading is different from last sample then execute servo controls
    debug and Serial.print(“State Change to “);
    debug and Serial.println(state);
    ldr_press();
    time_base = millis();
    }
    prevstate = state; //Remember state so we can compare it again next round
    }
    else
    {
    debug and Serial.println(“Kill Switch”);
    delay(4000);
    }
    }

    void ldr_press() { //This executes when the photocell readings have dictated
    debug and Serial.println(“State Change”);
    myservo.attach(servoPin); //Connect to servo
    if (pos > dest) { // If the current position is great than the destination then we must subtract
    while (pos > dest) // Change current position to desired position, one degree at a time.
    {
    debug and Serial.print(“Was :”);
    debug and Serial.print(pos);
    myservo.write(pos); // tell servo to go to position in variable ‘pos’
    delay(spd); // waits desired time for the servo to reach the position
    pos–;
    debug and Serial.print(” | Is :”);
    debug and Serial.println(pos);
    }
    myservo.detach(); //Detach from Servo
    }
    else { // If the curren’t position is greater than the destination then we must add
    myservo.attach(9); //Connect to servo
    while (pos < dest) // goes from 180 degrees to 0 degrees
    {
    debug and Serial.print("Was :");
    debug and Serial.print(pos);
    myservo.write(pos); // tell servo to go to position in variable 'pos'
    delay(spd); // waits desired time for the servo to reach the position
    pos++;
    debug and Serial.print(" | Is :");
    debug and Serial.println(pos);
    }
    }
    myservo.write(pos); // Doing a write out side of the loop because I had a feeling the last position value was being skipped. I think I'm wrong though
    delay(spd);
    myservo.detach(); //Detach servo to keep it from humming on strain
    }

    //This runs when the system is idle.. as in not moving already from one position to another
    void button_press() {

    //sensorValue = analogRead(sensorPin); //Query motion sensor
    button_value = digitalRead(button_pin); //Query button or relay switch
    debug and Serial.print("Motion Sensor Value: ");
    //debug and Serial.println(sensorValue);

    //Define the modes based on button press. Cycle through options
    //if(button_value==LOW || sensorValue < 900){ // button press
    if (button_value == LOW) { // button press
    spd = 20; // how fast the servo moves
    manualmode = 1;
    debug and Serial.println("button press");
    // debug and Serial.println(sensorValue);

    if (pos <= 0) { //0 degrees closed downward.
    dest = 95; //new dest is lower than prev dest (servo arrangement is 180 is fully down an 0 is fully up in other words backwards for what youd expect
    buttonpos = 0;
    debug and Serial.print(dest);
    debug and Serial.println(" Degree");
    }

    else if (pos <= 95 && buttonpos == 0 ) { //move to next position
    dest = 135;
    debug and Serial.print(dest);
    debug and Serial.println(" Degree");
    }

    else if (pos <= 135 && buttonpos == 0 ) {//move to next position
    dest = 180;
    debug and Serial.print(dest);
    debug and Serial.println(" Degree");
    }

    else if (pos <= 95 && buttonpos == 1 ) { //move to next position
    dest = 0;
    debug and Serial.print(dest);
    debug and Serial.println(" Degree");
    }

    else if (pos <= 135 && buttonpos == 1) {//move to next position
    dest = 95;
    debug and Serial.print(dest);
    debug and Serial.println(" Degree");
    }

    else if (pos <= 180) {//move to next position
    dest = 135;
    buttonpos = 1;
    debug and Serial.print(dest);
    debug and Serial.println(" Degree");
    }
    ldr_press();
    spd = 20;
    delay(500);
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *