Show Posts
|
|
Pages: 1 2 3 [4] 5 6 ... 8
|
|
49
|
Using Arduino / Programming Questions / Re: compare sensor data range over time to shut off
|
on: April 27, 2012, 10:27:14 am
|
|
I'm trying to disect your code, without much luck or understanding. am i on the right track with how I'm going about my sketch? something I'm missing? the time read just becomes continous and i want it to obviously change the second the new distance value changes. i know I'm missing somethingg quick but i can't get it.
|
|
|
|
|
50
|
Using Arduino / Programming Questions / Re: compare sensor data range over time to shut off
|
on: April 27, 2012, 10:07:56 am
|
int ultraSoundSignal = 8; // Ultrasound signal pin int val = 0; int oldDistance; int newDistance; int timecount; // Echo counter int ledPin = 5; int red = 9; int blue = 10; int count = 0; unsigned long start, finished, elapsed;
void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); int oldDistance = 0; pinMode(red,OUTPUT); pinMode(blue,OUTPUT); // oldDistance = read_the_sensor(); } void displayResult() { float h,m,s,ms; unsigned long over; elapsed=finished-start; h=int(elapsed/3600000); over=elapsed%3600000; m=int(over/60000); over=over%60000; s=int(over/1000); ms=over%1000; Serial.print("Raw elapsed time: "); Serial.println(elapsed); Serial.print("Elapsed time: "); Serial.print(h,0); Serial.print("h "); Serial.print(m,0); Serial.print("m "); Serial.print(s,0); Serial.print("s "); Serial.print(ms,0); Serial.println("ms"); Serial.println(); }
void loop() { if(timecount > 0){ digitalWrite(ledPin, HIGH); }
oldDistance = read_the_sensor(); delay(300); newDistance = read_the_sensor();
//newDistance = timecount; // echo pulse time to newDistance Serial.print("New Distance = "); // Example identifier for the sensor Serial.print(newDistance); Serial.write(10); Serial.write(13);
//oldDistance = timecount; // echo pulse time to newDistance Serial.print("Old Distance = "); // Example identifier for the sensor Serial.print(oldDistance); Serial.write(10); Serial.write(13);
/* Lite up LED if any value is passed by the echo pulse * ------------------------------------------------------------------- */ if (newDistance =! oldDistance){ start=millis(); delay(200); // for debounce Serial.println("Started...");
//digitalWrite(led, HIGH); }else{ finished=millis(); delay(200); // for debounce displayResult(); }} // //do our logic test // if(oldDistance > newDistance) // { // digitalWrite(red,HIGH); // digitalWrite(blue,LOW); // } // else // if(oldDistance < newDistance) // { // digitalWrite(red,LOW); // digitalWrite(blue,HIGH); // } // // }
int read_the_sensor() { timecount = 0; val = 0; pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
/* Send low-high-low pulse to activate the trigger pulse of the sensor * ------------------------------------------------------------------- */
digitalWrite(ultraSoundSignal, LOW); // Send low pulse delayMicroseconds(2); // Wait for 2 microseconds digitalWrite(ultraSoundSignal, HIGH); // Send high pulse delayMicroseconds(5); // Wait for 5 microseconds digitalWrite(ultraSoundSignal, LOW); // Holdoff
/* Listening for echo pulse * ------------------------------------------------------------------- */
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input val = digitalRead(ultraSoundSignal); // signal value to val while(val == LOW) { // Loop until pin reads a high value val = digitalRead(ultraSoundSignal); }
while(val == HIGH) { // Loop until pin reads a high value val = digitalRead(ultraSoundSignal); timecount = timecount +1; // Count echo pulse time }
return timecount; }
|
|
|
|
|
51
|
Using Arduino / Programming Questions / Re: compare sensor data range over time to shut off
|
on: April 27, 2012, 09:48:03 am
|
If it is the same, you need to compare the time since the change (Hint: Millis() - timeSinceChange) to see if it's greater than a certain interval. i want to summarize that when this sensor value has not changed over a specific period of time, do something. I'm not understanding how what i quoted will do that. i may be wrong. i can't visually understand it.
|
|
|
|
|
52
|
Using Arduino / Programming Questions / Re: compare sensor data range over time to shut off
|
on: April 26, 2012, 01:10:53 pm
|
PaulS, im unclear then how to implement millis. code thus far: int lastVal = 0; unsigned long change = 0; int signalPin = 8; long elapsedtime = 0;
int led = 5; void setup() {
Serial.begin(9600); }
void loop() {
elapsedtime = 0; pinMode(signalPin, 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); elapsedtime = pulseIn(signalPin, HIGH);
// print value through Serial
Serial.println(elapsedtime, DEC);
int valueCurrent = digitalRead(signalPin); if (valueCurrent =! lastVal) { //should lastval be millis?
digitalWrite(led, HIGH); //do something
} else {
digitalWrite(led, LOW);
} }
|
|
|
|
|
54
|
Using Arduino / Programming Questions / Re: compare sensor data range over time to shut off
|
on: April 26, 2012, 09:10:29 am
|
so i did a little quick mach up but i know I'm missing some key parts. I'm just confused on specific syntax to use. heres what i got: boolean lastVal = 0; //last reading int change = 0;
int sensorPin = 8;
if(sensorPin =! lastVal ){
//blink led or whatever action
} else {
change //confused here on how to set the "change to now". use millis?
}
|
|
|
|
|
55
|
Using Arduino / Programming Questions / compare sensor data range over time to shut off
|
on: April 25, 2012, 05:32:06 pm
|
I'm trying to compare if a sensor datas range stays the same over "x" amount of time, to do something. I'm kinda struggling on the syntax, so any help would be great. heres a basic ping sensor code I'm using: int signalPin = 8; // sensor signal pin connected to wiring pin 0 long elapsedtime = 0;
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(100); }
|
|
|
|
|
59
|
Using Arduino / Programming Questions / bitshift mapping a number with sine wave generator
|
on: April 20, 2012, 10:00:41 am
|
so i found this sine wave sketch and i want to make it adjustable. reading through documentation, I'm trying to take a number that is a bit shift in the sketch and map it to a pot. everything I've tried has disabled the first mapped pot in the sketch. heres the code: as you read below, the number i want to map from 1-28 with a pot is this portion: phaccu=phaccu+tword_m; icnt=phaccu >> 24; <<this number. refer to rest of code below. #include "avr/pgmspace.h"
// table of 256 sine values / one sine period / stored in flash memory PROGMEM prog_uchar sine256[] = { 127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240, 242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223, 221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78, 76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31, 33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124
}; #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
int ledPin = 13; // LED pin 7 int testPin = 7; int t2Pin = 6; byte bb; double dfreq; // const double refclk=31372.549; // =16MHz / 510 const double refclk=31376.6; // measured
// variables used inside interrupt service declared as voilatile volatile byte icnt; // var inside interrupt volatile byte icnt1; // var inside interrupt volatile byte c4ms; // counter incremented all 4ms volatile unsigned long phaccu; // pahse accumulator volatile unsigned long tword_m; // dds tuning word m
void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin as output Serial.begin(115200); // connect to the serial port Serial.println("DDS Test");
pinMode(6, OUTPUT); // sets the digital pin as output pinMode(7, OUTPUT); // sets the digital pin as output pinMode(11, OUTPUT); // pin11= PWM output / frequency output
Setup_timer2();
// disable interrupts to avoid timing distortion cbi (TIMSK0,TOIE0); // disable Timer0 !!! delay() is now not available sbi (TIMSK2,TOIE2); // enable Timer2 Interrupt
dfreq=1000.0; // initial output frequency = 1000.o Hz tword_m=pow(2,32)*dfreq/refclk; // calulate DDS new tuning word
} void loop() { // while(1) { // if (c4ms > 250) { // timer / wait fou a full second // c4ms=0; dfreq=analogRead(0); tword_m = dfreq; tword_m = map(tword_m, 0, 140032752, 0, 1023 ); // read Poti on analog pin 0 to adjust output frequency from 0..1023 Hz
cbi (TIMSK2,TOIE2); // disble Timer2 Interrupt tword_m=pow(2,32)*dfreq/refclk; // calulate DDS new tuning word sbi (TIMSK2,TOIE2); // enable Timer2 Interrupt
Serial.print(dfreq); Serial.print(" "); Serial.println(tword_m);
sbi(PORTD,6); // Test / set PORTD,7 high to observe timing with a scope cbi(PORTD,6); // Test /reset PORTD,7 high to observe timing with a scope } //****************************************************************** // timer2 setup // set prscaler to 1, PWM mode to phase correct PWM, 16000000/510 = 31372.55 Hz clock void Setup_timer2() {
// Timer2 Clock Prescaler to : 1 sbi (TCCR2B, CS20); cbi (TCCR2B, CS21); cbi (TCCR2B, CS22);
// Timer2 PWM Mode set to Phase Correct PWM cbi (TCCR2A, COM2A0); // clear Compare Match sbi (TCCR2A, COM2A1);
sbi (TCCR2A, WGM20); // Mode 1 / Phase Correct PWM cbi (TCCR2A, WGM21); cbi (TCCR2B, WGM22); }
//****************************************************************** // Timer2 Interrupt Service at 31372,550 KHz = 32uSec // this is the timebase REFCLOCK for the DDS generator // FOUT = (M (REFCLK)) / (2 exp 32) // runtime : 8 microseconds ( inclusive push and pop) ISR(TIMER2_OVF_vect) {
sbi(PORTD,7); // Test / set PORTD,7 high to observe timing with a oscope
phaccu=phaccu+tword_m; // soft DDS, phase accu with 32 bits icnt=phaccu >> 24; // use upper 8 bits for phase accu as frequency information (this is also what i want to map from 1-28) // read value fron ROM sine table and send to PWM DAC OCR2A=pgm_read_byte_near(sine256 + icnt);
if(icnt1++ == 125) { // increment variable c4ms all 4 milliseconds c4ms++; icnt1=0; }
cbi(PORTD,7); // reset PORTD,7 }
|
|
|
|
|