Show Posts
Pages: [1] 2 3 4
1  Community / Exhibition / Gallery / Re: My Arduino based Useless Box on: December 29, 2013, 01:08:08 pm
Hi all. Thank you for the great comments. smiley
I just used a giant switch case statement. I used 3 servos on this project. I will post the code and some more details soon. I am away at my parents for Christmas at the moment. My sketch is at home on my PC.
Regards
Jezza
2  Community / Exhibition / Gallery / My Arduino based Useless Box on: December 25, 2013, 08:59:14 pm
This is my take on the useless box. I hope you like it. If you want to see the code i will post it for you. It is pretty simple. (well it must be. I made it lol)
3  Using Arduino / Programming Questions / Re: Random State Machine on: December 18, 2013, 12:41:32 pm
Thank you all for your advice. I seem to have it sorted for this project now. I am going to start writing the servo code and will post an update soon.
Thanks
4  Using Arduino / Programming Questions / Re: Random State Machine on: December 17, 2013, 06:45:09 am

Quote
Code:
int range = (randNumber);
Why do this and why the brackets ?
The brackets yes that is a mistake. but as for the line itself
The sketch will not work without it.
int range = randNumber; // is what I should have used.

int range;
or
int range=0;
do not work.
What do you suggest i do ?
5  Using Arduino / Programming Questions / Re: Random State Machine on: December 17, 2013, 05:22:39 am
Thank you Guys I think have got it. I have made a little tester sketch using some led's. I also used a simple debounce routine to help keep everything tight. I have posted the code below (Please be aware that I am fairly new to programming.) I know there are probably lots of things I could do to improve my sketch. Please be merciful. smiley
Thanks again..

Code:
long randNumber;// variable for the random number
int button = 12;// button pin connected to digital pin 12

int val;//variable to read the pin status
int timer=10;//milliseconds we need to debounce the button
int val2;//variable to read pin status again after a short delay.

int buttonState;//this holds the state of the button
void setup(){
  Serial.begin(9600);
  for (int i=2;i<10;i++){// I had an led strip I used for testing
    pinMode(i,OUTPUT);    // Just as a visual aid to see if it works
  }                       // Although I only used 4 of them
  pinMode (button,INPUT);
  //
  //************ read the button in setup*******
  buttonState = digitalRead(button); // give us a starting state
}


void loop() {


  //The Debounce
  val=digitalRead(button);//read the button
  delay(timer);// time for button to stop bouncing.
  val2=digitalRead(button);//read the button again
  //this debounces the switch


  // use the lines below if the switch is not working to debug
  // Serial.print("Val =  ");// prints the word (val =) and some spaces
  // Serial.println(val);//the button reading just to make sure it works
  // if the circuit does not work you can see if the switch is working or not


  if (val == val2){//read the input and compare the readings
    if(val !=buttonState){//if something has changed the button has been pressed

      if (val==LOW){// button has been pressed
        randNumber = random(0, 4);// Random number generated for the switch
        Serial.println(randNumber);// print the random number
        int range = (randNumber);
        switch (range) {
        case 0: 
          Serial.println("Case 0");
          digitalWrite(2,HIGH);
          digitalWrite(3,LOW);
          digitalWrite(4,LOW);
          digitalWrite(5,LOW);

          break;
        case 1:   
          Serial.println("Case 1");
          digitalWrite(3,HIGH);
          digitalWrite(2,LOW);
          digitalWrite(4,LOW);
          digitalWrite(5,LOW);

          break;
        case 2:   
          Serial.println("Case 2");
          digitalWrite(4,HIGH);
          digitalWrite(2,LOW);
          digitalWrite(3,LOW);
          digitalWrite(5,LOW);


          break;
        case 3:   
          Serial.println("Case 3");

          digitalWrite(5,HIGH);
          digitalWrite(2,LOW);
          digitalWrite(3,LOW);
          digitalWrite(4,LOW);


          break;

        }
      }
    }
    buttonState=val;// save the button state in the variable
    //the arduino is ready for the next button event
  }
  delay(50);
}

6  Using Arduino / Programming Questions / Random State Machine on: December 16, 2013, 09:15:51 pm
Hi I am building a Useless Machine which will have 3 servos which act in different ways when the switch is pressed.The servos will not be a problem for me. I have been experimenting with them and seem to have that part sorted.
I have been thinking of using a state machine to control the box. This bit is very easy I know. The thing is I want to have around 9 or 10 different consequences when the switch is activated. But I do not want it to be predictable. How do I make it choose a random state each time.
If you have never seen a useless machine just take a look on you tube.

Obviously i am not very experienced but I suppose what I want is staring me in the face.
Thank you for your time guys.
If you need any more information please ask.
A little snippet of code

Code:
if (val==LOW){// button has been pressed
        if(nextState == 0){
          nextState=1;//   I want to jump to a random state here
         //Do something

        }
        else if(nextState == 1){
          nextState=2;//and here too
         //Do something else
        }
        else if(nextState == 2){
          nextState=3;// change the mode  again
          //Do something different
        }
        

Moderator edit: tags corrected
7  Community / Exhibition / Gallery / Re: Scrolling LED matrix display - Parola for Arduino on: September 12, 2013, 07:24:41 pm
That is a very cool display. Very nice work and your wiring is so neat. My projects look like a spider orgy lol. smiley-red
8  Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please on: June 03, 2013, 07:00:56 pm
The positive side of the capacitor cap is attached to Digital pin 2 (cap charger) via the resistor and directly connected to analog pin 5.
This reads the cap val. The other end of the capacitor is connected to ground.
 If you read the completed code the notes on the code lines will help you.
Jeremy
9  Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please on: June 02, 2013, 06:54:18 pm
hi the picture should be on this post if not check on page 1 look for full keypad diagram.jpg
Regards and good luck
Jeremy
10  Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please on: May 29, 2013, 03:50:41 pm
Hi atricio The keypad schematic is on page 1 of this thread. I do not have a full circuit schematic but it is obvious from the code where everything goes. It should not be too hard to follow.
Good luck
Jezza
11  Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please on: February 11, 2013, 08:25:12 am
Here is the completed code although I have set the state to unlocked when the system starts now because I intend to have a rechargeable battery in the case the alarm will be housed in. If I were to run it without I would set the state to locked. I have added another pin for the capacitor charging but would stress the need for a large value resistor(I used 100k) for the capacitor.
Jeremy

Code:
/* This sketch uses a 16 key 4X4 analog keypad. The password is 1234 press #
 to check the password. if correct the Red LED goes out and the Green Led comes on
 to rearm the alarm press *
 If when the system is in the disarmed state the C key is pressed both LED's light
 and you can enter a new 4 digit password.
 The sensor was triggering the alarm when armed from the unlocked state but was solved by using a capacitor to hold a small charge
which decays slowly when the alarm is set allowing the PIR sensor time to settle down.
 */
//Thank you Liudr for your guidance and help with the code.
// Your phi interface is a valuable set of tools.

#include <phi_interfaces.h>

#define buttons_per_column 16 // Each analog pin has five buttons with resistors.
#define buttons_per_row 1 // There are two analog pins in use.

byte keypad_type=Analog_keypad;
char mapping[]={
  '1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'}; // This is an analog keypad.
byte pins[]={
  0}; // The pin numbers are analog pin numbers.
int values[]={
  204,370,480,559,146,333,455,541,78,292,428,521,0,245,397,499}; //These numbers need to increase monotonically.
phi_analog_keypads panel_keypad(mapping, pins, values, buttons_per_row, buttons_per_column);
multiple_button_input* pad1=&panel_keypad;

// pin assignments
int speakerOut = 11;
int redLedpin = 12;
int greenLedpin = 10;
int alarmPin=3;
int capCharger=2;
int sensePin=A4;
int cap=A5;
int capVal;

// variables
char* inputString = "0000";  // holds the user input
char password[5] = "1234";  // holds the default correct password that can later be changed
int curpos = 0; // cursor position
int locked = 0; // status of the system: if locked = 1, if unlocked = 0
int motion;
long previousMillis = 0;
long interval = 10000;
int alarmPinstate = LOW;



void setup(){
  Serial.begin(9600);
  pinMode(sensePin,INPUT);// sensor
  pinMode(speakerOut, OUTPUT);//  keypad beeps and if passcode accepted tone
  pinMode(redLedpin, OUTPUT);// system armed
  pinMode(alarmPin,OUTPUT);
  pinMode(greenLedpin, OUTPUT);//system dissarmed
  pinMode(capCharger,OUTPUT);
  /*
The cap charger will charge a capacitor that stops the PIR
   sensor from showing an alert when setting the alarm.
   The pir shows high when power is applied so the cap stops the
   alarm triggering as soon as the alarm is powered on.
   */
}
void scanning(){
  capVal=analogRead(cap);//This Has Fixed It
  Serial.println (capVal);

  motion=analogRead(sensePin);
  //Serial.println(motion);// used for debugging
  unsigned long startTime = 0;
  unsigned long interval = 5000;

  if ((locked==1)&&(motion>=500)&&(capVal<100)){// if the alarm is on read the sensor
    if (startTime + interval < millis()) {
      digitalWrite(alarmPin, HIGH);
    }
  }
  else
  {
    digitalWrite (alarmPin, LOW);

  }


}


void loop(){

  if(locked ==1)//if the alarm is on
  {
    scanning();
    digitalWrite(redLedpin, HIGH);
    digitalWrite(capCharger,LOW);
    digitalWrite(greenLedpin, LOW);

  }


  if(locked ==0)// if the alarm is off
  {

    digitalWrite(redLedpin, LOW);// if this pin is low the alarm will be off
    digitalWrite(greenLedpin, HIGH);
    digitalWrite(capCharger,HIGH);// this starts the capacitor charging
    scanning();
  }

  // receive input from keypad
  byte temp=panel_keypad.getKey();

  if (temp != NO_KEY){   //  a key is pressed
    Serial.write(temp);
    playKeyTone(); // play a beep to acknowledge that key pressed

    switch(temp) // look for the special keys to initiate an event
    {
    case '#':
      parseString(); // try unlock routine
      curpos = 0;
      break;
    case '*': // lock the system

      Serial.println("Armed");

      locked = 1;
      clearPassword(); // clear the password so that user needs to enter 4 digits again before unlocking
      curpos = 0; // set cursor back to start of 4 digits
      break;
    case 'C':
      changePassword(); // change password routine
      curpos = 0;
      break;
    }

    if(temp != '*' && temp != '#' && temp != 'C')
    {
      inputString[curpos] = temp; // if the key was none of the above three special ones, add the digit to the password string
      curpos = curpos + 1; // move cursor to next position
    }

    if(curpos > 3) // if the cursor has reached the end of 4 digits, return it to the start
    {
      curpos = 0;
    }


  }
}

void playKeyTone(){ // beep  key press
  int elapsedtime = 0;
  while (elapsedtime < 100)
  {
    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(500);


    digitalWrite(speakerOut, LOW);
    delayMicroseconds(500);
    elapsedtime++;
  }
}

void playWarningTone(){ // long beep for wrong password
  int elapsedtime = 0;
  while (elapsedtime < 200)
  {
    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(1000);


    digitalWrite(speakerOut, LOW);
    delayMicroseconds(1000);
    elapsedtime++;
  }
}

void playSuccessTone(){ // short beep for correct password
  int elapsedtime = 0;
  while (elapsedtime < 10)
  {
    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(1000);

    digitalWrite(speakerOut, LOW);
    delayMicroseconds(1000);

    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(1000);

    digitalWrite(speakerOut, LOW);
    delayMicroseconds(1000);
    elapsedtime++;
  }
}


void parseString() // parse password to see if it matches
{
  Serial.println("Code Entered");
  Serial.println(inputString);
  if(matchString(password, inputString) == 1) // did they match?
  {
    Serial.println("SYSTEM IS OFF. PRESS * TO ARM");
    locked = 0; // correct code entered, unlock system
    playSuccessTone();
    return;
  }
  else
    playWarningTone(); // nope, wrong password
  Serial.println("Wrong Password");
}

void changePassword()
{
  Serial.println("PASSWORD CHANGE MODE");
  Serial.println("ENTER NEW PASSWPORD");
  if(locked == 0) // only get into this mode if unlocked! (otherwise anyone can change password without knowing correct one! that would be dumb!)
  {
    digitalWrite(redLedpin, HIGH); // turn on RED LED
    digitalWrite(greenLedpin, HIGH); // turn on GREEN LED
    char pkey = NO_KEY;
    int c = 0;
    while(c < 4)
    {

      pkey = panel_keypad.getKey(); // get new password 4 digits
      if(pkey != NO_KEY)
      {
        playKeyTone();
        password[c] = pkey; // store it in the password string
        Serial.println(pkey);
        c = c + 1;
      }

    }
    Serial.println("THANK YOU YOUR NEW PASSWORD IS:");
    Serial.println(password);

  }
}


int matchString(char* string1, char*string2) // function to match two strings, returns 1 if they match, 0 if they dont
{

  for(int i=0; i < 5; i++)
  {
    if(string1[i] != string2[i])
    {
      return 0;
    }
  }
  return 1;
}

void clearPassword() // clears the entered password after system is locked (so person can't just press # to unlock, needs to enter 4 digit code again!)
{
  for(int a=0; a < 4; a++)
  {
    inputString[a] = '0';
  }
}




12  Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please on: February 10, 2013, 06:51:30 pm
YES YES YES
It works. The answer was staring me in the face. It did not need any more states just 2 lines of code, a Capacitor and a resistor.
All I did was add a 220uf cap via 100k resistor to the yellow led. I then put a wire from the capacitor to A5. the current draw is so tiny but the cap does slowly charge. When you arm the system the reading on A5 drops over time to allow the PIR to settle down before the capval  drops to 3.
I am aware that I owe you a massive thank you Liudr for your patience with a novice and  for you spending your time to write some code for me. Also for pointing me in the right direction.
Again Thank you
Jeremy

 
Code:
void scanning(){
  capVal=analogRead(cap);//This Has Fixed It go down to the next if statement
  //Serial.println (capVal);
 
  motion=analogRead(sensePin);
  //Serial.println(motion);// used for debugging
  unsigned long startTime = 0;
  unsigned long interval = 5000;

  if ((locked==1)&&(motion>=500)&&(capVal<3)){//HERE IS THE FIX
    if (startTime + interval < millis()) {
      digitalWrite(alarmPin, HIGH);
    }//added
  }
  else
  {//added
    digitalWrite (alarmPin, LOW);

  }


}
13  Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please on: February 10, 2013, 09:27:14 am
Hi Liudr. The PIR is basically just a switch which sends out a low when there is no movement and a High when movement is detected. It will stay high for about 5 seconds after it stops seeing motion. The time the PIR stays high can be adjusted using an on board trimmer pot. This I will eventually make it 30 seconds for the siren (alarm Pin) to sound. Obviously i will have to change the timing on this part of the sketch
Code:
void scanning(){
  motion=analogRead(sensePin);
  //Serial.println(motion);// used for debugging
  unsigned long startTime = 0;
  unsigned long interval = 6000;

  if ((locked==1)&&(motion>=500)){// if the alarm is on read the sensor
    if (startTime + interval < millis()) {
      digitalWrite(alarmPin, HIGH);
    }//added
  }
  else
  {//added
    digitalWrite (alarmPin, LOW);

  }


}
the unsigned long interval will be changed to accommodate the time needed for the PIR to go low. I have tried to add a timer state called alert as you suggested but have not been able to get it to work. I know I am missing something simple but I just cannot sort it out. I will keep trying though.
Jeremy
14  Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please on: February 07, 2013, 07:39:41 pm
Hi I have now got it working but before I throw my hat in the air there is one slight problem. The PIR sensor takes 5 seconds to calibrate. When you power the system it works just fine but when you press the arming button on the keypad the alarm goes off because the sensor is giving a High signal.
Can anyone suggest anything that will stop it please.
Jeremy
Code:

/* This sketch uses a 16 key 4X4 analog keypad. The password is 1234 press #
 to check the password. if correct the Red LED goes out and the Green Led comes on
 to rearm the alarm press *
 If when the system is in the disarmed state the C key is pressed both LED's light
 and you can enter a new 4 digit password.
 The sensor part is still a work in progress.
 */



#include <phi_interfaces.h>

#define buttons_per_column 16 // Each analog pin has five buttons with resistors.
#define buttons_per_row 1 // There are two analog pins in use.

byte keypad_type=Analog_keypad;
char mapping[]={
  '1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'}; // This is an analog keypad.
byte pins[]={
  0}; // The pin numbers are analog pin numbers.
int values[]={
  204,370,480,559,146,333,455,541,78,292,428,521,0,245,397,499}; //These numbers need to increase monotonically.
phi_analog_keypads panel_keypad(mapping, pins, values, buttons_per_row, buttons_per_column);
multiple_button_input* pad1=&panel_keypad;

// pin assignments
int speakerOut = 11;
int redLedpin = 12;
int greenLedpin = 10;
int alarmPin=3;
int sensePin=A4;


// variables
char* inputString = "0000";  // holds the user input
char password[5] = "1234";  // holds the default correct password that can later be changed
int curpos = 0; // cursor position
int locked = 1; // status of the system: if locked = 1, if unlocked = 0
int motion;
long previousMillis = 0;
long interval = 5000;
int alarmPinstate = LOW;



void setup(){
  Serial.begin(9600);
  pinMode(sensePin,INPUT);// sensor
  pinMode(speakerOut, OUTPUT);//  keypad beeps and if passcode accepted tone
  pinMode(redLedpin, OUTPUT);// system armed
  pinMode(alarmPin,OUTPUT);
  pinMode(greenLedpin, OUTPUT);//system dissarmed

}
void scanning(){
  motion=analogRead(sensePin);
  //Serial.println(motion);// used for debugging
  unsigned long startTime = 0;
  unsigned long interval = 5000;

  if ((locked==1)&&(motion>=500)){// if the alarm is on read the sensor
    if (startTime + interval < millis()) {
      digitalWrite(alarmPin, HIGH);
    }//added
  }
  else
  {//added
    digitalWrite (alarmPin, LOW);

  }


}


void loop(){

  if(locked ==1)//if the alarm is on
  {
    scanning();
    digitalWrite(redLedpin, HIGH);

    digitalWrite(greenLedpin, LOW);

  }


  if(locked ==0)// if the alarm is off
  {

    digitalWrite(redLedpin, LOW);// if this pin is low the alarm will be off
    digitalWrite(greenLedpin, HIGH);
    scanning();
  }

  // receive input from keypad
  byte temp=panel_keypad.getKey();

  if (temp != NO_KEY){   //  a key is pressed
    Serial.write(temp);
    playKeyTone(); // play a beep to acknowledge that key pressed

    switch(temp) // look for the special keys to initiate an event
    {
    case '#':
      parseString(); // try unlock routine
      curpos = 0;
      break;
    case '*': // lock the system

      Serial.println("Armed");

      locked = 1;
      clearPassword(); // clear the password so that user needs to enter 4 digits again before unlocking
      curpos = 0; // set cursor back to start of 4 digits
      break;
    case 'C':
      changePassword(); // change password routine
      curpos = 0;
      break;
    }

    if(temp != '*' && temp != '#' && temp != 'C')
    {
      inputString[curpos] = temp; // if the key was none of the above three special ones, add the digit to the password string
      curpos = curpos + 1; // move cursor to next position
    }

    if(curpos > 3) // if the cursor has reached the end of 4 digits, return it to the start
    {
      curpos = 0;
    }


  }
}

void playKeyTone(){ // beep  key press
  int elapsedtime = 0;
  while (elapsedtime < 100)
  {
    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(500);


    digitalWrite(speakerOut, LOW);
    delayMicroseconds(500);
    elapsedtime++;
  }
}

void playWarningTone(){ // long beep for wrong password
  int elapsedtime = 0;
  while (elapsedtime < 200)
  {
    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(1000);


    digitalWrite(speakerOut, LOW);
    delayMicroseconds(1000);
    elapsedtime++;
  }
}

void playSuccessTone(){ // short beep for correct password
  int elapsedtime = 0;
  while (elapsedtime < 10)
  {
    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(1000);

    digitalWrite(speakerOut, LOW);
    delayMicroseconds(1000);

    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(1000);

    digitalWrite(speakerOut, LOW);
    delayMicroseconds(1000);
    elapsedtime++;
  }
}


void parseString() // parse password to see if it matches
{
  Serial.println("Code Entered");
  Serial.println(inputString);
  if(matchString(password, inputString) == 1) // did they match?
  {
    Serial.println("SYSTEM IS OFF. PRESS * TO ARM");
    locked = 0; // correct code entered, unlock system
    playSuccessTone();
    return;
  }
  else
    playWarningTone(); // nope, wrong password
  Serial.println("Wrong Password");
}

void changePassword()
{
  Serial.println("PASSWORD CHANGE MODE");
  Serial.println("ENTER NEW PASSWPORD");
  if(locked == 0) // only get into this mode if unlocked! (otherwise anyone can change password without knowing correct one! that would be dumb!)
  {
    digitalWrite(redLedpin, HIGH); // turn on RED LED
    digitalWrite(greenLedpin, HIGH); // turn on GREEN LED
    char pkey = NO_KEY;
    int c = 0;
    while(c < 4)
    {

      pkey = panel_keypad.getKey(); // get new password 4 digits
      if(pkey != NO_KEY)
      {
        playKeyTone();
        password[c] = pkey; // store it in the password string
        Serial.println(pkey);
        c = c + 1;
      }

    }
    Serial.println("THANK YOU YOUR NEW PASSWORD IS:");
    Serial.println(password);

  }
}


int matchString(char* string1, char*string2) // function to match two strings, returns 1 if they match, 0 if they dont
{

  for(int i=0; i < 5; i++)
  {
    if(string1[i] != string2[i])
    {
      return 0;
    }
  }
  return 1;
}

void clearPassword() // clears the entered password after system is locked (so person can't just press # to unlock, needs to enter 4 digit code again!)
{
  for(int a=0; a < 4; a++)
  {
    inputString[a] = '0';
  }
}





15  Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please on: February 06, 2013, 06:01:33 pm
Hi all. I am still unable to get the sensor to work in the sketch. I have tried all sorts but to no avail. I have been playing with state machines until I almost used my Arduino for a Frisbee. I managed to stop myself though.
On another note
I got one of those 4x4 membrane keypads yesterday for £3 on that well known auction site. I have stuck a few resistors on a breadboard and thanks to Liudr's libraries and sketch have it displaying all of the symbols perfectly.
1 2 3 A
4 5 6 B
7 8 9 C
* 0 # D
I just thought you would like to know that some things I do work
Jeremy
Pages: [1] 2 3 4