By continuing to use this site, you agree to our use of cookies. Find out more
Forum sponsored by:
Forum sponsored by allandale

Arduino Uno Programming Assistance Request

Struggling with While Loop

All Topics | Latest Posts

Search for:  in Thread Title in  
James Alford20/01/2018 09:28:11
321 forum posts
68 photos

Good morning.

Thank you for the replies and for the suggested code. My aim seems to be a lot harder to achieve that I had initially realised and I am wondering whether I should be better using a mechanical approach, as suggested. I had two reasons for wanting to use PWM: firstly, possibly mistakenly, I had thought that ramping up the power would save energy. However, if this is not the case, it lessens the point of the additional complexity of code. Secondly, it was to lessen the initial blow caused by the solenoid. However, this can be overcome, if no other way, by putting a firm rubber pad on the end of the actuator to absorb some of the impact.

I have gone down the route of solenoids, rather than stepper motors, simply because of familiarity. I agree that they might well prove rather noisy, though, and that stepper motors would be less intrusive. I shall have a look at the coding for these as I have never played with them before.

Regards,

James.

SillyOldDuffer20/01/2018 10:06:24
3111 forum posts
620 photos

I'm finding your questions and the answers fruitful James! For instance, I didn't know of the AccellStepper library, thanks Marcus!

Despite the attractions of steppers, my inclination would be to ring bells with solenoids as you propose. It might pay to strike the bell via a spring loaded arm : something like a piano key mechanism perhaps. Before leaping in to that complexity I'd experiment with a simple ON/OFF solenoid that directly hits the bell when activated. If it sounds OK, it is OK. If not, think again.

An interesting challenge in this sort of project is deciding what's best done by the Arduino, and what's best done externally. It's fairly obvious that using an Arduino and RTC to keep time eliminates a mass of difficult mechanical clockmaking. It's not so obvious where best to manage something like the acceleration of a striker. It could be done with an Arduino, but my feeling is a simple mechanism would produce better results from less effort.

It's all part of the learning curve. Don't be discouraged by difficulties - it may be painful at the time but overcoming them is pure gold in terms of gaining experience. At this stage I wouldn't worry too much about saving power. Get the thing working first and then refine it.

Dave

Neil Wyatt20/01/2018 12:41:32
avatar
Moderator
13829 forum posts
583 photos
68 articles
Posted by James Alford on 19/01/2018 07:38:18:

Good morning.

With the help that I received from this forum, I have now rewritten large parts of my code, replacing my original method of turning the output pins on and off with the t.pulse code. This has made the code tidier and made it much easier to control the length of time for which the pins remain high and the duration between pulses.

I am now looking to see whether I can incorporate PWM into this code so that I can give the solenoids that the pins will drive a soft start, rather than coming on with a bang: I also hope that it will reduce overall power consumption.

I have written a separate piece of code that works on a spare pin, but I am now trying to integrate it with the pulse.

Is there any way that I can put this "soft start" code into a routine on its own and then replace pulseLength_ms in the line t.pulse( LED_PIN, pulseLength_ms, LOW ); with a call to this sub-routine?

I have only just started to look at this, so have tried little yet.

Regards,

James.

Solenoids are inductors, which 'resit' rapid changes in current.

This means solenoids 'soft start' by their very nature. Applying a 'soft start' current is likely to result in missed movements as ideally you want the current to rise rapidly to overcome inertia/stiction.

The ideal way to drive a solenoid is to reduce the current to just above where they operate reliably. if you want to save milliamps, you can safely reduce the hold-in current by a considerable amount, assuming you need to keep them energised.

Bear in mind that a high PWM frequency may not drive solenoids reliably.

Neil

Ian P20/01/2018 13:29:18
avatar
1902 forum posts
80 photos

In addition to what Neil said bear in mind that normal plunger type solenoids have a very non linear action regarding produced output. Their maximum force is only available when the plunger is at or near the end of its travel (when the magnetic circuit is complete (or almost complete).

Over the first part of the solenoid plunger movement relatively little force is available, many applications I have seen put a high voltage/current into the winding so get the armature moving and then reduce it to a small level to keep the plunger extended.

I think quite a bit of experimentation and engineering would be needed to keep audible noise to an acceptable level. I cannot imagine any amount of software tweaking that would improve the action. Certainly software can be optimised to change the audible noise that the stepper motor makes, HDD manufacturers have had years of practice!

I apologise for not going back through the whole thread, but what is the device producing the sound (bell, rod, or what)?

Ian P

Les Jones 120/01/2018 15:04:51
1989 forum posts
135 photos

I don't know the design of this clock but I imagine the solenoid will be stepping a ratchet type wheel one step at a time. Some alternatives are a model servo motor. The position of these is controlled by a pulse betwee 1.5 and 2.5 mS (From memory) at a PRF of about 50 hz. You could just cange the pulse width for a short time to make it move out and back to step the ratchet one position. You could use a small geared motor with a cam on it with one notch cut out. A micro switch would engage against the cam so its contacts were open at the notch position. The contacts would be in series with the motor. A relay or transistor in parallel with these contacts could be pulsed for just longer than the time required for the motor to move enough for the micro switch to close. The motor would then continue for a full rotation and stop at the notch. A crank on the motor could be arranged to step the ratchet wheel one position. A variation on this is to have a number of notches (say 6 , 10, 12, 24) so the wheel would move one position for each pulse. An optical sensor could be used in place of the micro switch.

Les.

James Alford21/01/2018 09:52:28
321 forum posts
68 photos

Good morning.

Thank you for the continued and additional suggestions.

Les: I did consider motors, cams and micro-switches initially and they are still a possibility for some elements of the finished design.

Neil and Ian: thank you for the information about solenoids and relays. I studied electromechanics at college for a few years when I was training to be a telephone exchange engineer, but it is scary just how little I can truly recall. Your posts have been fruitful refreshers.

Dave: I agree with you in that it is likely to be easier to refine the control of the device mechanically than with programming and I shall focus on that approach.

As mentioned, I do wish to complete my other main project before embarking on this at all, but below are some drawings of my ideas so far. It is quite small, the wheels being four inches in diameter: much more would exceed the capacity of my lathe. The base will house most of the solenoids, the Uno, circuitry and some batteries. The actuating arms pushing the wheels around will have a hinge and spring in them so that they can slide over the teeth when retracting for the next impulse. I am still trying to decide how best to display the time, but I am leaning towards a vertical, numbered ring on top of each wheel and a pointer; the time will be read vertically by reading off from the marker. I envisage a day of the week and a night time or day time display on the top. The whole thing, other than the base, will be placed within a clear tubular case.

Still, plenty of time to settle on a design that interests me enough to actually spend the time building it. I have designed two other clocks, but neither really engaged me anywhere as much as this has.

vertical clock front.jpgvertical clock base.jpg

 

Regards,

James.

 

Edited By James Alford on 21/01/2018 09:55:09

Edited By James Alford on 21/01/2018 09:55:50

Edited By James Alford on 21/01/2018 09:57:34

James Alford21/01/2018 11:15:58
321 forum posts
68 photos

An irritating update.

I updated my Uno board and libraries following the prompts that came up when plugged it in this morning.

Now, on all versions of my programme, I get an error message on compiling saying:

 

error: no matching function for call to 'DS3231::DS3231(const uint8_t&, const uint8_t&'

 

DS3231 rtc(SDA, SCL);

The emoji is not my addition. I have no idea what has happened, but now need to try and unpick what is causing it..................

James.

 

 

Edited By James Alford on 21/01/2018 11:17:25

SillyOldDuffer21/01/2018 12:03:15
3111 forum posts
620 photos
Posted by James Alford on 21/01/2018 11:15:58:

An irritating update.

I updated my Uno board and libraries following the prompts that came up when plugged it in this morning.

Now, on all versions of my programme, I get an error message on compiling saying:

error: no matching function for call to 'DS3231::DS3231(const uint8_t&, const uint8_t&'

DS3231 rtc(SDA, SCL);

The emoji is not my addition. I have no idea what has happened, but now need to try and unpick what is causing it..................

James.

Edited By James Alford on 21/01/2018 11:17:25

Have a look at the definitions of SDA and SCL. Should be something like:

const uint8_t SDA = 11;

rather than, say, the very similar:

int SDA = 11;

The compiler is complaining because one or both of the SDA and SCL type definitions don't match what the RTC library is expecting. Presumably the RTC library specification was tightened by the last update.

The other way of fixing it is with a cast. eg:

DS3231 rtc( (const uint8_t) SDA, (const uint8_t) SCL);

The first method is the more correct - everything matches. The second method tells the compiler "I know the definition of SDA isn't quite right and I know what it should be, stop moaning".

Dave

Marcus Bowman21/01/2018 12:46:11
124 forum posts

Updates are a pain. Updating to the current version caused the IDE to refuse to compile anything correctly, so it will not run on my MAC. Won't compile on XP of course, but does do the job nicely on W7.

Yes; I know my Mac OS is 'no longer supported' as is XP, but it is annoying when things like this are not at least backward compatible to the extent that they continue to work with existing sketches.

Marcus

James Alford21/01/2018 20:53:20
321 forum posts
68 photos

Dave,

Thank you for the suggestions. I have been playing with this, but to no avail so far. I have to confess that, to be honest, I do not really know what I am doing.

I have searched through the code for the SDA and SC definitions, but can find nothing there. I have added the "cast" code, but that has not worked either. The trouble is, though, that I am not sure quite what the bits const uint8_t really mean nor where I should place the "cast".

I shall carry on looking and playing............

James.

SillyOldDuffer21/01/2018 21:58:45
3111 forum posts
620 photos

Did you replace

DS3231 rtc(SDA, SCL);

with

DS3231 rtc( (const uint8_t) SDA, (const uint8_t) SCL);

What error do you get from the second form?

The explanation - if interested read, on.

In C, all variables must be declared before use. For example:

int a; // a is an integer
int b=2; // b is an integer set to 2
float f; // f is a floating point number

Somewhere near the front of your code there will integer definitions for SDA and SCL. They define the pins on the Arduino to which the RTC Modules SDA and SCL terminals are connected. Something like:

char SDA = 11; // Not necessarily 11 - depends on your wiring
or int SDA = 11;
or byte SDA = 11;
or similar

uint8_t is an Unsigned 8-bit integer. It's a close relative of char, int and byte, but not identical.

A declaration like

const uint8_t SDA = 11; means "SDA is an unsigned integer (can't be negative) that's set to 11. Also, it's a constant number that cannot be changed later."

"error: no matching function for call to 'DS3231::DS3231(const uint8_t&, const uint8_t& ) ;" means that the DS3231 RTC is expecting to be initialised with a pair of constant unsigned 8-bit integers and that's not how SDA and SCL are defined further up the code.

This is sensible. There are no circumstances where it would be legitimate to change an RTC module's connections while the program is running. Attempting it is almost certainly a bad mistake. The compiler is 'helpfully' preventing you from making that mistake - it's 'tough love'.

A 'cast' is a conversion. int a = (int) 3.141; would set a = 3. The cast is what's between the brackets.

Usually you don't get entangled in this arcane stuff: most sensible conversions are done automatically.

Dave

James Alford22/01/2018 07:18:09
321 forum posts
68 photos

Dave,

Thank you for the explanation, which makes sense and makes the problem both clearer and, perversely, more puzzling to me.

Firstly: I tried replacing DS3231 rtc(SDA, SCL); with DS3231 rtc( (const uint8_t) SDA, (const uint8_t) SCL); but received the same error message as before.

However, after reading your explanation, I read through the my code to try and find definitions of SDA, SCL or indeed anything else to do with the RTC. The only definitions or setting up that I can find are as below.

Near the top, before the void setup or void loop, but amongst the other definitions, was

DS3231 rtc(SDA, SCL); which is now replaced by DS3231 rtc( (const uint8_t) SDA, (const uint8_t) SCL);

A couple of lines down is this:

//define RTC address
#define DS3231_I2C_ADDRESS 0x68

There are no other references to the RTC or DS3231 that I can find, other than

// Initialize the rtc object
rtc.begin();

in void setup and a series of commands to read from the RTC for the display.

I thought that I had possibly managed to delete something vital without realising it, so checked a much earlier version of the sketch that was fully operational, but this was the same.

I am assuming that I need to add something to the code to specifically define the RTC. I shall have to have another go this evening after work.

Regards,

James.

 

 

 

#define DS3231_I2C_ADDRESS 0x68

Edited By James Alford on 22/01/2018 07:25:40

SillyOldDuffer22/01/2018 09:40:14
3111 forum posts
620 photos

Morning James.

First of all my apologies for misleading you yesterday. Brain-fade at my end. The RTC is an I2C device and on the Arduino, SCL is always on pin A5 and SDA on pin A4. The pins are predefined and you can't change them. You can't find the definitions because they're not in your code - I sent you on a wild-goose chase!

Something else is wrong:

  • Which RTC library are you using?
  • Are there any other error messages or warnings in the window?

Quite often as a program grows and this sort of error suddenly strikes it can be hard to see the wood for the trees. Nothing worse than looking for a subtle bug in a mass of code. If we don't crack this quickly, write a new sketch, as short as possible, that does nothing but prove the RTC works. Quite likely you won't need to write it yourself - most Arduino libraries come with an examples folder containing a few simple demo sketches.

Good news - as you had it working before, it's likely that the error will be easy to fix. Bad news, small errors can be hard to find.

If you get really stuck, we can exchange email addresses via a PM. If you send me a copy of your program, I'll have a closer look.

Dave

Marcus Bowman22/01/2018 19:00:35
124 forum posts

Testing the RTC is quite straightforward. Here's a sketch (below). As indiated in the comments, you will need to instal the RTC library from Adafruit (link inside the sketch.

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
// This example taken from Adafruit. Info at https://learn.adafruit.com/adafruit-ds3231-precision-rtc-breakout/arduino-usage

// Wire.h is part of the Arduino IDE
#include <Wire.h>

// RTClib.h comes from https://codeload.github.com/adafruit/RTClib/zip/master
#include "RTClib.h"

// Connections:
// RTC SDA to Arduino A4
// RTC SCL to Arduino A5


RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {

#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif

Serial.begin(9600);

delay(3000); // wait for console opening

if (! rtc.begin()) {
Serial.println("Couldn't find RTC";
while (1);
}

if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!";
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}

void loop () {
DateTime now = rtc.now();

Serial.print(now.year(), DEC);
Serial.print('/';
Serial.print(now.month(), DEC);
Serial.print('/';
Serial.print(now.day(), DEC);
Serial.print(" (";
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(" ";
Serial.print(now.hour(), DEC);
Serial.print(':';
Serial.print(now.minute(), DEC);
Serial.print(':';
Serial.print(now.second(), DEC);
Serial.println();

Serial.print(" since midnight 1/1/1970 = ";
Serial.print(now.unixtime());
Serial.print("s = ";
Serial.print(now.unixtime() / 86400L);
Serial.println("d";

// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));

Serial.print(" now + 7d + 30s: ";
Serial.print(future.year(), DEC);
Serial.print('/';
Serial.print(future.month(), DEC);
Serial.print('/';
Serial.print(future.day(), DEC);
Serial.print(' ';
Serial.print(future.hour(), DEC);
Serial.print(':';
Serial.print(future.minute(), DEC);
Serial.print(':';
Serial.print(future.second(), DEC);
Serial.println();

Serial.println();
delay(3000);
}

Compile, then upload. Then open a Serial Monitor window. There is a pause before the first time reading appears, then an update every 3 seconds. Works very well.

Marcus

Marcus Bowman22/01/2018 19:13:07
124 forum posts

I have just noticed the 'smileys' the site has added to the sketch. Wasn't me: honest.

If someone can tell me how to upload a sketch as an attachment, I can do that later tonight. If not; the smileys need to be edited out.

Marcus

 

Edited By Marcus Bowman on 22/01/2018 19:13:33

SillyOldDuffer22/01/2018 21:49:13
3111 forum posts
620 photos

Those smileys - don't cha luv 'em? LOL

The site converts character smiley sequences into imojis. The sequence that causes most trouble in posts containing C source is a right bracket ) followed immediately by a semi-colon;

The site won't do an imoji if you type a space character between the ) and the ;

There's a better way. Retaining indentation when publishing source code is also a problem. It can be done by fooling the site by embedding your own HTML tags in the text. The ordinary 'Post a reply' window blocks this but the edit function doesn't

The method I use is to first type and save the post without inserting the source code. Then immediately edit the post to insert html tags <code> and </code> on separate lines where you want your source to appear. Finally paste the source between the html tags and save the edit.

Bit of a fiddle but all being well any text inserted between the tags should appear in a fixed font with indentation. As the length of posts is limited keep it short.

Dave

Frances IoM22/01/2018 22:03:46
503 forum posts
21 photos
That 'feature' in edit seems worrying unless it is severely restrictive - if it accepts iframes + cross site links or javascript then all bets are off as to what can be done
James Alford23/01/2018 07:08:07
321 forum posts
68 photos

Dave:

I had noticed that the SDA and SCL pins were labelled on the board, but did not know whether they still needed to be referenced.

I have also been coming around to the idea of starting the basic RTC code again to get that working and then reintroducing the additional structure, stage by stage. I shall try this over the next few days. Thank you for the offer of looking at my coding, should I get nowhere. I appreciate this, but shall try to resolve it myself before taking up more of your time.

I had thought that I used a RTC library from Northern Widget on github, but I am no longer quite sure. What puzzles me further is that I went back to the place where I initial found the RTC code that I am using and there is no reference there to any RTC library. The trouble is that I did this bit of coding several months ago and have forgotten the detail. A good example of why I should have added more notes to the source in the code........

Marcus:

Thank you for the RTC code. I shall copy this over and try it out to see whether I can then use this to get me going again.

Watch this space for an update in a day or two's time.

Regards,

James.

Edited By James Alford on 23/01/2018 07:08:50

Edited By James Alford on 23/01/2018 07:09:19

Edited By James Alford on 23/01/2018 07:10:02

James Alford23/01/2018 21:16:20
321 forum posts
68 photos

Good evening.

I am utterly bemused. This morning, I created a new sketch using the code that Marcus posted above, editing it to remove the smiley faces. I installed the relevant library and uploaded the programme to the board to test it: all went well and it worked, printing to the serial printer as designed.

This evening, out of curiosity, I tried compiling my original code again, having made no alterations to it. Much to my surprise, it compiled, loaded and ran perfectly.

I have absolutely no idea at all why this should have happened. The only thing that I can think of is that, somehow, installing the library for Marcus's code allowed mine to work. As the library names are different, this seems unlikely, but I really can not see what else it can be. No matter what; I shall be wary about further board and library updates.

I should just like to thank you all again for the detailed help with this problem.

Regards,

James.

Doubletop23/01/2018 22:48:38
avatar
365 forum posts
4 photos
Configuration management. You do it on physical engineering projects but can be so much harder in the virtual world when you can't see the detail Suddenly all your imperial nuts become metric and things assemble properly

Pete

Edited By Doubletop on 23/01/2018 22:50:44

All Topics | Latest Posts

Please login to post a reply.

Magazine Locator

Want the latest issue of Model Engineer or Model Engineers' Workshop? Use our magazine locator links to find your nearest stockist!

Find Model Engineer & Model Engineers' Workshop

Latest Forum Posts
Support Our Partners
Eccentric Engineering
Ausee.com.au
Expo Tools July 14
Eccentric July 5 2018
emcomachinetools
Allendale Electronics
Sarik
ChesterUK
Warco
Shapiro
TRANSWAVE Converters
SPG Tools October Seventeen
Subscription Offer

Latest "For Sale" Ads
Latest "Wanted" Ads
Get In Touch!

Do you want to contact the Model Engineer and Model Engineers' Workshop team?

You can contact us by phone, mail or email about the magazines including becoming a contributor, submitting reader's letters or making queries about articles. You can also get in touch about this website, advertising or other general issues.

Click THIS LINK for full contact details.

For subscription issues please see THIS LINK.

Digital Back Issues

Social Media online

'Like' us on Facebook
Follow us on Facebook

Follow us on Twitter
 Twitter Logo

Pin us on Pinterest

Visit the Model Engineer
Exhibition website

Model Engineer Exhibition