Show Posts
Pages: [1]
1  Using Arduino / Project Guidance / Ping sensor and speaker on: May 29, 2013, 01:52:07 pm
Ive have stitched together this code just for fun to try to make some sound experiments with ping sensors. This however get boring quite fast. smiley You might have some suggestions to further experiments?  Thanks alot! 

Code:
const int pingPin = 7;
int soundpin = 9;

void setup() {

  Serial.begin(9600);
  pinMode(soundpin, OUTPUT);
}

void loop()
{
  long duration, inches, cm;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  cm = microsecondsToCentimeters(duration);
 
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);

   int level = cm;
    level = map(level, 0, 40, 100, 400);
 
    if(cm < 40){
       
       tone(9, level);
    }
    else {
      noTone(9);
    }

}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}
2  Using Arduino / Programming Questions / Re: Ultra sensor triggering a music shield on: September 08, 2012, 08:51:33 am
it does'nt seem to work.
3  Using Arduino / Programming Questions / Re: Ultra sensor triggering a music shield on: September 01, 2012, 10:52:50 am
so like

Code:
if (elapsedtime < 1500) {
        playcomplete("FA.WAV");
    }
4  Using Arduino / Programming Questions / Re: Ultra sensor triggering a music shield on: September 01, 2012, 09:29:18 am
i was thinking of doing something like

if reading of ultrasonic sensor is under the threshold then play a certain file from the SD card.
5  Using Arduino / Programming Questions / Ultra sensor triggering a music shield on: September 01, 2012, 05:15:12 am
Hey guys.

I wanted to know if you could give me a push in the right direction, 'cause i'm new to this coding.

What i need to do is that i need to merge two codes in order to have my ultrasonic sensor to trigger my music shield which is attached to my arduino board.

These are the two codes:

Code:
int signalPin = 8;     // sensor signal pin connected to wiring pin 0
long elapsedtime = 0;
int ledPin =3;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  elapsedtime = 0;
  pinMode(signalPin, OUTPUT);  // set signalPin as OUTPUT

  // Send 0-1-0 pulse to activate the sensor
  digitalWrite(signalPin, LOW);
  delayMicroseconds(2);
  digitalWrite(signalPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(signalPin, LOW);

  // Listen to pulse
  pinMode(signalPin, INPUT);               // set signalPin as INPUT
  elapsedtime = pulseIn(signalPin, HIGH);  // get the length of the pusle while it is HIGH

  // print value through Serial

  Serial.println(elapsedtime, DEC);
  delay(20);

if (elapsedtime < 1500) {
    digitalWrite(ledPin, HIGH);
      }
  else {
    digitalWrite(ledPin,LOW);

}
}


and

Code:
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"


SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card
FatReader f;      // This holds the information for the file we're play

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

#define DEBOUNCE 5  // button debouncer

// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
byte buttons[] = {A0, A1, A2, A3, A4, A5};
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'pressed' (the current state
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

// this handy function will return the number of bytes currently free in RAM, great for debugging!   
int freeRam(void)
{
  extern int  __bss_end;
  extern int  *__brkval;
  int free_memory;
  if((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__bss_end);
  }
  else {
    free_memory = ((int)&free_memory) - ((int)__brkval);
  }
  return free_memory;
}

void sdErrorCheck(void)
{
  if (!card.errorCode()) return;
  putstring("\n\rSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  putstring(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}

void setup() {
  byte i;
 
  // set up serial port
  Serial.begin(9600);
  putstring_nl("WaveHC with ");
  Serial.print(NUMBUTTONS, DEC);
  putstring_nl("buttons");
 
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam());      // if this is under 150 bytes it may spell trouble!
 
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
 
  // pin13 LED
  pinMode(13, OUTPUT);
 
  // Make input & enable pull-up resistors on switch pins
  for (i=0; i< NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }
 
  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  if (!card.init()) {         //play with 8 MHz spi (default faster!) 
    putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
    sdErrorCheck();
    while(1);                            // then 'halt' - do nothing!
  }
 
  // enable optimize read - some cards may timeout. Disable if you're having problems
  card.partialBlockRead(true);
 
// Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {     // we have up to 5 slots to look in
    if (vol.init(card, part))
      break;                             // we found one, lets bail
  }
  if (part == 5) {                       // if we ended up not finding one  :(
    putstring_nl("No valid FAT partition!");
    sdErrorCheck();      // Something went wrong, lets print out why
    while(1);                            // then 'halt' - do nothing!
  }
 
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?
 
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    putstring_nl("Can't open root dir!"); // Something went wrong,
    while(1);                             // then 'halt' - do nothing!
  }
 
  // Whew! We got past the tough parts.
  putstring_nl("Ready!");
 
  TCCR2A = 0;
  TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;

  //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;


}

SIGNAL(TIMER2_OVF_vect) {
  check_switches();
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  byte index;

  for (index = 0; index < NUMBUTTONS; index++) {
    currentstate[index] = digitalRead(buttons[index]);   // read the button
   
    /*     
    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");
    */
   
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}


void loop() {
  byte i;
 
  if (justpressed[0]) {
      justpressed[0] = 0;
      playcomplete("DO.WAV");
  }
  if (justpressed[1]) {
      justpressed[1] = 0;
      playcomplete("RE.WAV");
  }
  if (justpressed[2]) {
      justpressed[2] = 0;
      playcomplete("MI.WAV");
  }
  if (justpressed[3]) {
      justpressed[3] = 0;
      playcomplete("FA.WAV");
  }
  if (justpressed[4]) {
      justpressed[4] = 0;
      playcomplete("SO.WAV");
  }
  if (justpressed[5]) {
      justpressed[5] = 0;
      playcomplete("LA.WAV");
  }
}



// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
  // call our helper to find and play this name
  playfile(name);
  while (wave.isplaying) {
  // do nothing while its playing
  }
  // now its done playing
}

void playfile(char *name) {
  // see if the wave object is currently doing something
  if (wave.isplaying) {// already playing something, so stop it!
    wave.stop(); // stop it
  }
  // look in the root directory and open the file
  if (!f.open(root, name)) {
    putstring("Couldn't open file "); Serial.print(name); return;
  }
  // OK read the file and turn it into a wave object
  if (!wave.create(f)) {
    putstring_nl("Not a valid WAV"); return;
  }
 
  // ok time to play! start playback
  wave.play();
}

What confuses me the most is the whole NUMBUTTONS thing.

Thanks!
6  Using Arduino / Displays / Re: LiquidCrystal.h for 8*2 display? on: August 24, 2012, 08:56:38 am




Code:
/*
  Project:   LCD til test
  Hardware:  LCD 8x2
  Filename:  LCD_test_8x2_standard.pde
  Rev. nr.:  001
  Author :   Hans Erik Tjelum
  Company:   www.let-elektronik.dk
  Date:      2011-03-31
  Copyright: Copyleft

  Detaljeret beskrivelse:
  LCD-program til at teste et LCD
 
  The circuit:

  LCD pins:
  1:  GND - digital common
  2:  +5V - digital supply
  3:  Vo - LCD contrast input*
  4:  RS - Register Select -> Arduino pin 12
  5:  Enable - Active low -> GND
  6:  R/W - Read/Write 1=Read -> Arduino pin 11
  7:  D0 - Not used
  8:  D1 - Not used
  9:  D2 - Not used
  10: D3 - Not used
  11: D4  - Used for 4-bit mode -> Arduino pin 5
  12: D5  - Used for 4-bit mode -> Arduino pin 4
  13: D6  - Used for 4-bit mode -> Arduino pin 3
  14: D7  - Used for 4-bit mode -> Arduino pin 2
  15: LED Anode -> Arduino pin 9 (PWM)
  16: LED Cathode -> GND

 * 10K trimmer resistor:
   ends to +5V and ground, wiper to LCD Vo

 */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(8, 2);
  // Print a message to the LCD.
  lcd.print(" LE 2011");
  pinMode(9, OUTPUT);
  analogWrite(9, 128);
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);
  analogWrite(9, 64);
  delay(1000);
  analogWrite(9, 255);
  delay(1000);
}

Thanks alot!
7  Using Arduino / Displays / LiquidCrystal.h for 8*2 display? on: August 24, 2012, 08:12:05 am
Does anyone know if the LiquidCrystal.h library is working with a 8*2 display?

I use the EONE high cost-effective 8*2 Characters LCD display but i can't seem to get it to do anything.

Thanks alot!
8  Using Arduino / Sensors / Re: Problem reading PIR sensor on: August 16, 2012, 09:06:50 am
no i hav'nt tried that. Would it make any difference? This is what is says from the site where i got it from:

"This is a simple to use motion sensor. Power it up and wait 1-2 seconds for the sensor to get a snapshot of the still room. If anything moves after that period, the 'alarm' pin will go low.

Red wire is power (5 to 12V). Brown wire is GND. Black wire is open collector Alarm.

This unit works great from 5 to 12V (datasheet shows 12V). You can also install a jumper wire past the 5V regulator on board to make this unit work at 3.3V. Sensor uses 1.6mA@3.3V.

The alarm pin is an open collector meaning you will need a pull up resistor on the alarm pin. The open drain setup allows multiple motion sensors to be connected on a single input pin. If any of the motion sensors go off, the input pin will be pulled low.

The connector is slightly odd but has a 0.1" pitch female connector making it compatible with jumper wires and 0.1" male headers."


edit: waiting did'nt give anything.
9  Using Arduino / Sensors / Re: Problem reading PIR sensor on: August 16, 2012, 08:53:28 am
I don't have that no.. :/

But i do have three of the sensors laying here and they are all acting the same way.

edit: and yes that is the sensors i use.
10  Using Arduino / Sensors / Problem reading PIR sensor on: August 16, 2012, 06:15:37 am
Hello people

I have trouble reading og a PIR sensor and i can't see what im doing wrong. Im following the tutorial on http://bildr.org/2011/06/pir_arduino/

The problem is that it keep printing motion detected no matter what i do.  smiley-sad

I used the code used in the tutorial:

Code:
int pirPin = 2; //digital 2

void setup(){
 Serial.begin(9600);
 pinMode(pirPin, INPUT);
}

void loop(){
  int pirVal = digitalRead(pirPin);

  if(pirVal == LOW){ //was motion detected
    Serial.println("Motion Detected");
    delay(2000);
  }

}


I've added a picture as the hook up aswell:
11  Using Arduino / Sensors / Average of reading on IR sensor on: May 03, 2012, 06:31:20 am
Hey people

I'm using a SHARP IR sensor for a school project. When im testing the sensor, it works but in between there are some irregular
readings when nothing is infront of the sensor (so i should read zero) but there are some very sort signal (of high value mostly).
I need to use it as a trigger and therefore need a way around this. Is it maybe poissble to gather lets say the last six or seven last
read values and create a average of this? Or is there some other way. I guess its because the sensor has some kind of loose connection?

Thanks!
Pages: [1]