Show Posts
Pages: 1 2 [3] 4 5 ... 17
31  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

Code:
#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
  }
}
32  Using Arduino / Motors, Mechanics, and Power / Re: Grumbling servo following swap from one to another on: April 20, 2012, 01:35:23 pm
Read reply #4 again -

Ah right-I guess he didn't see cyborgcnc had already posted them. Thanks for clearing that up.
33  Using Arduino / Programming Questions / Re: why does this code execute twice in quick succession, pray? on: April 20, 2012, 01:27:08 pm
Is the block of code in that conditional getting executing twice as well?
Yes - it is. smiley
34  Using Arduino / Motors, Mechanics, and Power / Re: Grumbling servo following swap from one to another on: April 20, 2012, 11:13:28 am
Its 544 to 2400us
Sorry, what is? Are these the trim "values that will make the servo not slam into it's extremes, and maybe stop twitching"

And if it is, would you mind letting me know how you found these? Did you find the datasheet?

Thanks

35  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! smiley-wink  smiley-sweat  smiley-razz

No - seriously, my bad.

Basically the code snippet I posted initially

Code:
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.
36  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
Code:
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?

Code:
  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?
}
37  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.

Code:
#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();
}
38  Using Arduino / Motors, Mechanics, and Power / Re: Grumbling servo following swap from one to another on: April 20, 2012, 07:57:26 am
I'm very curious to try this! Thank you. The servo is a Hextronik MG14 and I have totally failed in my bid to find a datasheet for it! I imagine having the datasheet would make specifying the values that would make the servo not slam into its extremes and not twitch trivial...

Thanks
39  Using Arduino / Motors, Mechanics, and Power / Re: Grumbling servo following swap from one to another on: April 20, 2012, 06:51:53 am
How is it powered?

with a wallwart. A different wallwart to the one which powers the Arduino. Grounds tied.
40  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:

Code:
Servo.write(180);
with the new servo, it grumbled massively (as if it had hit a mechanical stop). I changed the code to
Code:
Servo.write(150);
and all was well.

I still don't understand why this should be any different between servos! smiley Does this mean I can use
Code:
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
41  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!
42  Using Arduino / Programming Questions / why does this code execute twice in quick succession, pray? on: April 19, 2012, 10:46:11 am
Code:

  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();
    }
  }

Code:
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!
43  Using Arduino / Programming Questions / How to monitor a switch's state and do X if switch has remained in state B on: April 19, 2012, 10:38:25 am
How to monitor a switch's state and do X if switch has remained in state B for length of time T?

Basically I want to alert the user audibly if a toggle switch remains in and "on" position for longer than 2hrs but don't know how to do it logically.

Would appreciate any tips

Thanks
44  Using Arduino / Motors, Mechanics, and Power / Isn't this a dream servo?? Cheap, powerful, fast, metal geared, 360deg on: April 19, 2012, 05:37:40 am
I'm struggling to see the catch? I've just bought a hextronik MG14 for £9:-

Voltage 4.8V ~ 7.2V
Gears Metal
Speed 60° in 0.11sec
Model MG14
Weight 14gr
Torque 2.5kg



Whereas the "dream servo" seems much higher specced for only a pound more at £10!

Voltage 4.8V ~ 7.2V
Gears Metal
Speed 60° in 0.16sec
Model FS5113R
Weight 58 gr
Torque 13.2kg/cm

Thanks ...
45  Using Arduino / General Electronics / Re: Solder Breadboard on: April 17, 2012, 11:07:22 am
Would you guys mind clarifying this AVR/socket thing?

if it is what I think it is - I like the sound of it.

I am having a massive faff with my tangle of wires surrounding my arduino project because I can't re-program the arduino from where it sits due to the length of the USB cable so I am having to unplug about 20 cables when I need to take the arduino to the computer. Nightmare!
Pages: 1 2 [3] 4 5 ... 17