Show Posts
|
|
Pages: 1 [2] 3 4 ... 16
|
|
16
|
Using Arduino / Programming Questions / An error is causing my code to work! Bizarre...
|
on: April 23, 2012, 07:21:20 am
|
Whoops - in stripping back the extraneous code I also removed the description. Basically, in the form below: const int FrontDoorPin = 4; const int relay = 4;
void setup() { pinMode(FrontDoorPin,INPUT); digitalWrite(FrontDoorPin, HIGH); //pull up pinMode(relay, OUTPUT); } void loop() { if (digitalRead(FrontDoorPin) == LOW) { //trigger here } } my code works. A press on a button (connected to pin 4 and ground) is noticed by the Arduino. If I remove “const int relay = 4” the Arduino thinks the button is pressed - constantly? THANKS
|
|
|
|
|
17
|
Using Arduino / Motors, Mechanics, and Power / Re: Grumbling servo following swap from one to another
|
on: April 20, 2012, 03:03:34 pm
|
OK - I've used zoomkat's code and discovered the servo likes values between 3 and 180. Or 600 and 2300 microseconds. Thanks zoomkat! . P.S. With the feline code, the servo doesn't twitch. Well done the man who identified it was the use of the library that was causing the twitch. I was poised with the WD40! // zoomkat 10-22-11 serial servo test // type servo position 0 to 180 in serial monitor // or for writeMicroseconds, use a value like 1500 // Powering a servo from the arduino usually *DOES NOT WORK*.
String readString; #include <Servo.h> Servo myservo; // create servo object to control a servo
void setup() { Serial.begin(57600); myservo.writeMicroseconds(1500); //set initial servo position if desired myservo.attach(6); //the pin for the servo control Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded }
void loop() { while (Serial.available()) { char c = Serial.read(); //gets one byte from serial buffer readString += c; //makes the string readString delay(2); //slow looping to allow buffer to fill with next character }
if (readString.length() >0) { Serial.println(readString); //so you can see the captured string int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code. if(n >= 500) { Serial.print("writing Microseconds: "); Serial.println(n); myservo.writeMicroseconds(n); } else { Serial.print("writing Angle: "); Serial.println(n); myservo.write(n); }
readString=""; //empty for next input } }
|
|
|
|
|
18
|
Using Arduino / Programming Questions / Re: How to monitor a switch's state and do X if switch has remained in state B
|
on: April 20, 2012, 02:36:30 pm
|
(This was posted before tgm175 posted his explanation.) OK I've made some changes to the code and tested it and with acknowledgements and thanks to tgm1175 who was the chef here, I wasn't even the sous chef, I was the washer-upper. This is tested working. THANKS #include <Bounce.h> const int switchPin1 = 7; long switchPin1TriggeredTime = 10000; Bounce bouncer = Bounce( switchPin1,5 );
void setup() { Serial.begin(57600); pinMode(switchPin1, INPUT); digitalWrite(switchPin1, HIGH); //pull up Serial.println("TEST"); pinMode(13, OUTPUT); } void loop() { bouncer.update ( ); int value = !bouncer.read(); static unsigned long switch_timestamp = 0; if(value && switch_timestamp && (millis() - switch_timestamp) >= switchPin1TriggeredTime) { Serial.println("HEY SWITCH HAS BEEN 'ON' TOO LONG!!"); } else if (!switch_timestamp && value) { //The !switch_timestamp is to make sure we don't refresh the timer if the switch remained on. //The pin just changed to on, start the timer switch_timestamp = millis(); } else if(!value && switch_timestamp) { //Switch went off, while timer was running, disable the timer switch_timestamp = 0; Serial.println("TURNED OFF"); } if(value) { digitalWrite(13, HIGH); // LED on } if(!value) { digitalWrite(13, LOW); // LED off } }
|
|
|
|
|
22
|
Using Arduino / Programming Questions / Re: why does this code execute twice in quick succession, pray?
|
on: April 20, 2012, 11:06:24 am
|
We don't know what you mean by "code execute twice in quick succession", nor why that is wrong. Code is executing continuously in quick sucession all the time!!!
Sorry to have provoked a code red three exclamation mark response!  No - seriously, my bad. Basically the code snippet I posted initially RTCInitiallySet = 1; // RTC has been told to initially set Serial.print("RTC before: "); PrintDateTime(RTC.now()); Serial.println(); sendNTPpacket(timeServer); delay(1000); if ( Udp.parsePacket() ) { Udp.read(pb, NTP_PACKET_SIZE); unsigned long t1, t2, t3, t4; t1 = t2 = t3 = t4 = 0; for (int i=0; i< 4; i++) { t1 = t1 << 8 | pb[16+i]; t2 = t2 << 8 | pb[24+i]; t3 = t3 << 8 | pb[32+i]; t4 = t4 << 8 | pb[40+i]; }
float f1,f2,f3,f4; f1 = ((long)pb[20] * 256 + pb[21]) / 65536.0; f2 = ((long)pb[28] * 256 + pb[29]) / 65536.0; f3 = ((long)pb[36] * 256 + pb[37]) / 65536.0; f4 = ((long)pb[44] * 256 + pb[45]) / 65536.0; const unsigned long seventyYears = 2208988800UL; t1 -= seventyYears; t2 -= seventyYears; t3 -= seventyYears; t4 -= seventyYears; t4 += 1; if (f4 > 0.4) t4++; RTC.adjust(DateTime(t4)); Serial.print("RTC after : "); PrintDateTime(RTC.now()); Serial.println(); Serial.println("Time sync successful."); Serial.println(); playTone(10, 3000); SetRTC = 0; // RTC has been set
is called twice when I would expect it to be called only once. It sets the RTC from an NTP server twice in quick succession. This is undesirable. I am trying to call that chunk of code once per hour on the 30th second. Somehow - either because of the theory I described in my first post - or because I have a logic error, the code runs twice around the 30th second. If this is still unclear, please let me know - I will do my best to clarify.
|
|
|
|
|
23
|
Using Arduino / Programming Questions / Re: How to monitor a switch's state and do X if switch has remained in state B
|
on: April 20, 2012, 10:48:30 am
|
Thank you for the proto code! I will test it tonight. I don't unnerstand this switch_state && switch_timestamp && (millis() - switch_timestamp) >= switchPin1TriggeredTime) (i.e. how it is able to do what it does!) which worries me but maybe after playing with it I will. Is there an easy way to call a function (to play a tone on a piezo speaker) every two minutes when the switch has remained "on" for the pre-determined time limit? Or would I need to use variables to a) track when the piezo function was called and b) create an "alert" state which would control when the function was called? static unsigned long switch_timestamp = 0; bool switch_state = digitalRead(switchPin1); const int switchPin1 = 1; long switchPin1TriggeredTime = 7200000;
if(switch_state && switch_timestamp && (millis() - switch_timestamp) >= switchPin1TriggeredTime) { //play a short alert tone every minute } else if (!switch_timestamp && switch_state) { //The !switch_timestamp is to make sure we don't refresh the timer if the switch remained on. //The pin just changed to on, start the timer switch_timestamp = millis(); } else if(!switch_state && switch_timestamp) { //Switch went off, while timer was running, disable the timer switch_timestamp = 0; } if(switch_state) {
//actual code for when switch is on goes here? }
|
|
|
|
|
24
|
Using Arduino / Programming Questions / Re: why does this code execute twice in quick succession, pray?
|
on: April 20, 2012, 10:24:51 am
|
difficult to see why code is being called without seeing what's calling it. I thought I included the code's calls? Here is the entire sketch. #include <Bounce.h> #include <Wire.h> #include "RTClib.h" #include <Servo.h> #include <SPI.h> #include <Ethernet.h> #include <EthernetUdp.h> byte mac[] = { 0x00, 0x11, 0x22, 0x33, 0xFB, 0x11}; EthernetClient client; ////////////NTP UPDATE//////////// unsigned int localPort = 8888; // NTP Port byte timeServer[] = { 193, 79, 237, 14}; // ntp1.nl.net NTP server const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message byte pb[NTP_PACKET_SIZE]; // Buffer to hold incoming and outgoing packets EthernetUDP Udp; unsigned long currentMillis = 0; ////////////CONNECTIVITY//////////// boolean EthernetRunning = true; ////////////SERVER//////////// IPAddress server(207,7,108,203); // Joyent ////////////SERVO//////////// Servo BellControlServo; ////////////MISC//////////// const int PiezoPin = 7; // Piezo buzzer pin 7
///////////SWITCHES/////////// const int switchPin1 = 1; long switchPin1TriggeredTime = 0; // When was the analog read triggered // const int switchPin2 = 2; // const int switchPin3 = 3; // ///////PHONE RELAY const int relay = 4; // ////////////THE GMT PIPS//////////// unsigned int pip1 = 0; unsigned int pip2 = 0; unsigned int pip3 = 0; unsigned int pip4 = 0; unsigned int pip5 = 0; unsigned int pip6 = 0; unsigned int SetRTC = 0; unsigned int RTCInitiallySet = 0; ////////////RTC////////////
RTC_DS1307 RTC; enum { OFF, ON, FLASH }; void setup() {
//////////// MISC//////////// pinMode(PiezoPin, OUTPUT); pinMode(switchPin1, INPUT); pinMode(switchPin2, INPUT); pinMode(switchPin3, INPUT); pinMode(relay, OUTPUT); BellControlServo.attach(6); Serial.begin(57600); Wire.begin(); RTC.begin();
Serial.println("Annunciator Panel v0.9d"); Ethernet.begin(mac); if (Ethernet.begin(mac) == 0) { EthernetRunning = false; Serial.println("Error: No DHCP!"); } if(EthernetRunning) { Udp.begin(localPort); Serial.println("DHCP is working - enabling UDP."); } Serial.println(); if (! RTC.isrunning()) { Serial.println("Error: The RTC is NOT available!"); } } void loop() { //Don't forget warning pips for when sound is turned off for more than 2hrs!// DateTime now = RTC.now(); currentMillis = millis(); NurseryVoltsRead = analogRead(NurseryPin); BathroomVoltsRead = analogRead(BathroomPin); TradesmenVoltsRead = analogRead(TradesmenPin); MasterBedroomVoltsRead = analogRead(MasterBedroomPin); DrawingRoomVoltsRead = analogRead(DrawingRoomPin);
if (currentMillis > 15000 && RTCInitiallySet < 1) { Serial.println("Sync the clock."); Serial.println(); SetRTC = 1; } if (SetRTC == 1 && EthernetRunning == false) { SetRTC = 0; // Cancels the sync RTCInitiallySet = 1; // Cancels the sync Serial.println("Error: RTC could not be synced because there is no internet connectivity."); } if (SetRTC == 1 && EthernetRunning == true) { RTCInitiallySet = 1; // RTC has been told to initially set Serial.print("RTC before: "); PrintDateTime(RTC.now()); Serial.println(); sendNTPpacket(timeServer); delay(1000); if ( Udp.parsePacket() ) { Udp.read(pb, NTP_PACKET_SIZE); unsigned long t1, t2, t3, t4; t1 = t2 = t3 = t4 = 0; for (int i=0; i< 4; i++) { t1 = t1 << 8 | pb[16+i]; t2 = t2 << 8 | pb[24+i]; t3 = t3 << 8 | pb[32+i]; t4 = t4 << 8 | pb[40+i]; }
float f1,f2,f3,f4; f1 = ((long)pb[20] * 256 + pb[21]) / 65536.0; f2 = ((long)pb[28] * 256 + pb[29]) / 65536.0; f3 = ((long)pb[36] * 256 + pb[37]) / 65536.0; f4 = ((long)pb[44] * 256 + pb[45]) / 65536.0; const unsigned long seventyYears = 2208988800UL; t1 -= seventyYears; t2 -= seventyYears; t3 -= seventyYears; t4 -= seventyYears; t4 += 3600; //British Summer Time t4 += 1; if (f4 > 0.4) t4++; RTC.adjust(DateTime(t4)); Serial.print("RTC after : "); PrintDateTime(RTC.now()); Serial.println(); Serial.println("Time sync successful."); Serial.println(); playTone(10, 3000); SetRTC = 0; // RTC has been set } else { Serial.println("Error: UDP Unavailable."); Serial.println("Cancelling sync."); SetRTC = 0; // Cancels the sync RTCInitiallySet = 1; // Cancels the sync } }
if (now.minute() == 59 && now.second() == 30) { if (SetRTC == 0) { SetRTC = 1; Serial.println("Syncing the clock ready for the pips: "); Serial.println(); } }
} void playTone(long duration, int freq) { duration *= 1000; int period = (1.0 / freq) * 1000000; long elapsed_time = 0; while (elapsed_time < duration) { digitalWrite(PiezoPin,HIGH); delayMicroseconds(period / 2); digitalWrite(PiezoPin, LOW); delayMicroseconds(period / 2); elapsed_time += (period); } } void PrintDateTime(DateTime t) { char datestr[24]; sprintf(datestr, "%04d-%02d-%02d %02d:%02d:%02d ", t.year(), t.month(), t.day(), t.hour(), t.minute(), t.second()); Serial.print(datestr); } unsigned long sendNTPpacket(byte *address) { // set all bytes in the buffer to 0 memset(pb, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request // (see URL above for details on the packets) pb[0] = 0b11100011; // LI, Version, Mode pb[1] = 0; // Stratum, or type of clock pb[2] = 6; // Polling Interval pb[3] = 0xEC; // Peer Clock Precision // 8 bytes of zero for Root Delay & Root Dispersion pb[12] = 49; pb[13] = 0x4E; pb[14] = 49; pb[15] = 52;
// all NTP fields have been given values, now // you can send a packet requesting a timestamp: Udp.beginPacket(address, 123); //NTP requests are to port 123 Udp.write(pb,NTP_PACKET_SIZE); Udp.endPacket(); }
|
|
|
|
|
27
|
Using Arduino / Motors, Mechanics, and Power / Grumbling servo following swap from one to another
|
on: April 20, 2012, 06:17:08 am
|
I recently replaced a micro servo with nylon gears rated for 1.2kg/cm - with a mini servo with metal gears rated for 2.2kg/cm. Both are standard servos, not 360deg or continous rotation. With my previous servo, I could tell it: Servo.write(180); with the new servo, it grumbled massively (as if it had hit a mechanical stop). I changed the code to Servo.write(150); and all was well. I still don't understand why this should be any different between servos!  Does this mean I can use Servo.write(-20) ; to regain the 20deg of travel I've otherwise lost? Does this make sense? This new servo also "twitches" at idle (the old one didn't). Should I return it or spray it with WD40 (as I've seen recommended here). Thanks all
|
|
|
|
|
28
|
Using Arduino / Programming Questions / Re: How to monitor a switch's state and do X if switch has remained in state B
|
on: April 19, 2012, 10:54:40 am
|
Declare global variable to keep track of the time that the switch has remained in it's position. Record the edge time for when the switch changed states. Keep checking for the the time it's been since it's been switched (millis() - timeSwitched) and compare that to your interval time.
Yes that's what I thought initially, but how do I avoid overwriting the timeSwitched with millis() everytime the IF statement checks to see if the switch is still HIGH? Thanks!
|
|
|
|
|
29
|
Using Arduino / Programming Questions / why does this code execute twice in quick succession, pray?
|
on: April 19, 2012, 10:46:11 am
|
if (currentMillis > 15000 && RTCInitiallySet < 1) { Serial.println("Sync the clock."); Serial.println(); SetRTC = 1; }
if (now.minute() == 59 && now.second() == 30) { if (SetRTC == 0) { SetRTC = 1; Serial.println("Syncing the clock ready for the pips: "); Serial.println(); } } if (SetRTC == 1 && EthernetRunning == true) { RTCInitiallySet = 1; // RTC has been told to initially set Serial.print("RTC before: "); PrintDateTime(RTC.now()); Serial.println(); sendNTPpacket(timeServer); delay(1000); if ( Udp.parsePacket() ) { Udp.read(pb, NTP_PACKET_SIZE); unsigned long t1, t2, t3, t4; t1 = t2 = t3 = t4 = 0; for (int i=0; i< 4; i++) { t1 = t1 << 8 | pb[16+i]; t2 = t2 << 8 | pb[24+i]; t3 = t3 << 8 | pb[32+i]; t4 = t4 << 8 | pb[40+i]; }
float f1,f2,f3,f4; f1 = ((long)pb[20] * 256 + pb[21]) / 65536.0; f2 = ((long)pb[28] * 256 + pb[29]) / 65536.0; f3 = ((long)pb[36] * 256 + pb[37]) / 65536.0; f4 = ((long)pb[44] * 256 + pb[45]) / 65536.0; const unsigned long seventyYears = 2208988800UL; t1 -= seventyYears; t2 -= seventyYears; t3 -= seventyYears; t4 -= seventyYears; t4 += 1; if (f4 > 0.4) t4++; RTC.adjust(DateTime(t4)); Serial.print("RTC after : "); PrintDateTime(RTC.now()); Serial.println(); Serial.println("Time sync successful."); Serial.println(); playTone(10, 3000); SetRTC = 0; // RTC has been set } else { Serial.println("Error: UDP Unavailable."); Serial.println("Cancelling sync."); SetRTC = 0; // Cancels the sync RTCInitiallySet = 1; // Cancels the sync } } I have a theory...and that's because the sync causes the time to be second 30 twice by adjusting the clock...what's weird is that this happens EVERY SINGLE HOUR...and I would have thought it would only happen now and then...and it's odd that the RTC adds a second every hour. Otherwise I'm somehow cancelling/setting the sync ilogically in the code. THANKS!
|
|
|
|
|