Any advise for code?(help) Nearly done Door lock

Hello there, Im seeking alittle advise about a small projekt im doing at home.
Im trying to make a servo lock and unlock my door through my Samsung S5, and ofcourse be able to trigger it from a button from the inside.

Bear with me, I'm fairly new to arduino /programming, trying to learn....

I was lucky to find a code and replicated it so it nearly matches to my idea about how it should work
(Code and Source will be at the bottom)

My problem is I can't figure out how to add a bluetooth(hc5) trigger command for what the physical button does,
how would you have done this ?

Its not that

Regards Thomas
DK

My:

Code/Source:
http://thenscaler.com/?page_id=174

#include <Servo.h>

// constant variables used to set servo angles, in degrees
const int straight = 50; 
const int divergent = 145;

// constant variables holding the ids of the pins we are using
const int divergent_led = 6;
const int straight_led = 7;
const int buttonpin = 8;
const int servopin = 9;

// servo movement step delay, in milliseconds
const int step_delay = 10;

// create a servo object
Servo myservo;  
 
// global variables to store servo position
int pos = straight; // current
int old_pos = pos; // previous

void setup() 
{ 
  // set the mode for the digital pins in use
  pinMode(buttonpin, INPUT);
  pinMode(straight_led, OUTPUT);
  pinMode(divergent_led, OUTPUT);
  
  // setup the servo
  myservo.attach(servopin);  // attach to the servo on pin 9
  myservo.write(pos); // set the initial servo position
  
  // set initial led states
   digitalWrite(straight_led, HIGH);
    digitalWrite(divergent_led, LOW);
}

void loop() 
{ 
 // start each iteration of the loop by reading the button
 // if the button is pressed (reads HIGH), move the servo
  int button_state = digitalRead(buttonpin);
  if(button_state == HIGH){
    // turn off the lit led
    if(pos == straight){
              digitalWrite(straight_led, LOW);
          } else {
              digitalWrite(divergent_led, LOW);
          }
    old_pos = pos;   // save the current position
    
    // Toggle the position to the opposite value
    pos = pos == straight ? divergent: straight;
       
    // Move the servo to its new position
    if(old_pos < pos){   // if the new angle is higher
      // increment the servo position from oldpos to pos
      for(int i = old_pos + 1; i <= pos; i++){  
        myservo.write(i); // write the next position to the servo
        delay(step_delay); // wait
      }
    } else {  // otherwise the new angle is equal or lower
      // decrement the servo position from oldpos to pos
      for(int i = old_pos - 1; i >= pos; i--){ 
        myservo.write(i); // write the next position to the servo
        delay(step_delay); // wait
      }
    }
    // turn on the appropriate LED.
    if(pos == straight){
            digitalWrite(straight_led, HIGH);
        } else {
            digitalWrite(divergent_led, HIGH);
       }
  } 
}// end of loop

If you have not already done so you need to write a short program to receive data using Bluetooth and display it on the Serial Monitor. Your Phone app probably just needs to send 'U' for unlock and 'L' for lock. The first example in Serial Input Basics is intended for that sort of simple system.

When you can receive the characters from the phone you probably just need some code to use make your variable buttonState = HIGH or LOW as approporiate depending on the character received.

...R

Robin2:
If you have not already done so you need to write a short program to receive data using Bluetooth and display it on the Serial Monitor. Your Phone app probably just needs to send 'U' for unlock and 'L' for lock. The first example in Serial Input Basics is intended for that sort of simple system.

When you can receive the characters from the phone you probably just need some code to use make your variable buttonState = HIGH or LOW as approporiate depending on the character received.

...R

First off all thanks you very much for your help, much needed,

I can now successfully receive characters on serial monitor, with this code hopefully i did added it correct under Void Loop.

Working:

#include <Servo.h>
String message; //string that stores the incoming message

// constant variables used to set servo angles, in degrees
const int straight = 50; 
const int divergent = 145;

// constant variables holding the ids of the pins we are using
const int divergent_led = 6;
const int straight_led = 7;
const int buttonpin = 8;
const int bluetoothclick = 3;
const int servopin = 9;

// servo movement step delay, in milliseconds
const int step_delay = 10;

// create a servo object
Servo myservo;  
 
// global variables to store servo position
int pos = straight; // current
int old_pos = pos; // previous

void setup() 
{ 
  // set the mode for the digital pins in use
  pinMode(buttonpin, INPUT);
  pinMode(straight_led, OUTPUT);
  pinMode(divergent_led, OUTPUT);
  Serial.begin(9600); //set baud rate
  
  // setup the servo
  myservo.attach(servopin);  // attach to the servo on pin 9
  myservo.write(pos); // set the initial servo position
  
  // set initial led states
   digitalWrite(straight_led, HIGH);
    digitalWrite(divergent_led, LOW);
}

void loop() 
{ 

while(Serial.available())
  {//while there is data available on the serial monitor
    message+=char(Serial.read());//store string from serial command
  }
  if(!Serial.available())
  {
    if(message!="")
    {//if data is available
      Serial.println(message); //show the data
      message=""; //clear the data
    }
  }
  delay(5000); //delay

  
 // start each iteration of the loop by reading the button
 // if the button is pressed (reads HIGH), move the servo
  int button_state = digitalRead(buttonpin);
  if(button_state == HIGH){
    // turn off the lit led
    if(pos == straight){
              digitalWrite(straight_led, LOW);
          } else {
              digitalWrite(divergent_led, LOW);
          }
    old_pos = pos;   // save the current position
    
    // Toggle the position to the opposite value
    pos = pos == straight ? divergent: straight;
       
    // Move the servo to its new position
    if(old_pos < pos){   // if the new angle is higher
      // increment the servo position from oldpos to pos
      for(int i = old_pos + 1; i <= pos; i++){  
        myservo.write(i); // write the next position to the servo
        delay(step_delay); // wait
      }
    } else {  // otherwise the new angle is equal or lower
      // decrement the servo position from oldpos to pos
      for(int i = old_pos - 1; i >= pos; i--){ 
        myservo.write(i); // write the next position to the servo
        delay(step_delay); // wait
      }
    }
    // turn on the appropriate LED.
    if(pos == straight){
            digitalWrite(straight_led, HIGH);
        } else {
            digitalWrite(divergent_led, HIGH);
       }
  } 
}// end of loop

So after I got that, I tried to fumble it to working as I would say , :slight_smile:
and ended up with this code and that does not work, It will not receive any characters,

I dont think its the right way I done below, But my idea was from bluetooth to trigger a shorting on the switch, ?

Hope It make sense .. thanks !

#include <Servo.h>
String message; //string that stores the incoming message

// constant variables used to set servo angles, in degrees
const int straight = 50; 
const int divergent = 145;

// constant variables holding the ids of the pins we are using
const int divergent_led = 6;
const int straight_led = 7;
const int buttonpin = 8;
const int servopin = 9;
const int clicker = 3; // New extra wire to trigger bluetooth

// servo movement step delay, in milliseconds
const int step_delay = 10;

// create a servo object
Servo myservo;  
 
// global variables to store servo position
int pos = straight; // current
int old_pos = pos; // previous

void setup() 
{ 
  // set the mode for the digital pins in use
  pinMode(buttonpin, INPUT);
  pinMode(straight_led, OUTPUT);
  pinMode(divergent_led, OUTPUT);
  pinMode(clicker, OUTPUT); // New extra wire to ground trigger
  Serial.begin(9600); //set baud rate
  
  // setup the servo
  myservo.attach(servopin);  // attach to the servo on pin 9
  myservo.write(pos); // set the initial servo position
  
  // set initial led states
   digitalWrite(straight_led, HIGH);
    digitalWrite(divergent_led, LOW);
}

void loop() 

{ 

while(Serial.available())
  {//while there is data available on the serial monitor
    message+=char(Serial.read());//store string from serial command
  }
  if(!Serial.available())
  {
    if(message!="L")
    {//if data is available
      Serial.println(message); //show the data
      digitalWrite(3, LOW); // Should trigger it from bluetooth ?????
      message=""; //clear the data
      
   }
  }
  
 // start each iteration of the loop by reading the button
 // if the button is pressed (reads HIGH), move the servo
  int button_state = digitalRead(buttonpin);
  if(button_state == HIGH){
    // turn off the lit led
    if(pos == straight){
              digitalWrite(straight_led, LOW);
          } else {
              digitalWrite(divergent_led, LOW);
          }
    old_pos = pos;   // save the current position
    
    // Toggle the position to the opposite value
    pos = pos == straight ? divergent: straight;
       
    // Move the servo to its new position
    if(old_pos < pos){   // if the new angle is higher
      // increment the servo position from oldpos to pos
      for(int i = old_pos + 1; i <= pos; i++){  
        myservo.write(i); // write the next position to the servo
        delay(step_delay); // wait
      }
    } else {  // otherwise the new angle is equal or lower
      // decrement the servo position from oldpos to pos
      for(int i = old_pos - 1; i >= pos; i--){ 
        myservo.write(i); // write the next position to the servo
        delay(step_delay); // wait
      }
    }
    // turn on the appropriate LED.
    if(pos == straight){
            digitalWrite(straight_led, HIGH);
        } else {
            digitalWrite(divergent_led, HIGH);
       }
  } 
}// end of loop

I had in mind that you would just put the complete functions from Serial Input Basics into your program so that it would look like this

void loop() {
	recvOneChar(); // or receiveWithEndMarker() if that is more suitable
	showNewData();
        // your other code
}

Using Strings (capital S) is not a good idea in the small memory of an Arduino. They can cause memory corruption. Use strings (small s) as in my examples.

...R