Do Any Members Have Skill or Experience With Arduino; Coding for Microcontrollers

post the code you have here and i’ll format it for you. we can work this out. i hope the person is alright too.

2 Likes

So, distilling this to the actual questions at hand (the background is great but I want to be sure I understand where you need help):

  1. You have an arduino nano setup with IR break-beam sensor. I’m curious what this electrical configuration is, but this is working, correct?

  2. You also have a 12V solenoid driver setup that is working? Presumeably a small transistor, hopefully a flyback diode. It may also be helpful to post that so we can understand. You link a solenoid circuit, but you are now using a 5V solenoid. Are you using the same parts as the link (transistor, diode, resistor)? What voltage are you driving the solenoid circuit with? A picture would be helpful, usually can sort it out from that. If you just swapped in a 5V solenoid, depending on where you get that power, you may be asking for longer-term problems. Do you have a link to the solenoid?

  3. I’d be happy to look at your Arduino code, there are many people here (me included) that could easily diagnose it and guide you in the right direction. IF/ELSE is pretty straightforward to help with, although, like anything else, if we don’t know where you are at exactly, it’s hard to provide effective help. Post the code you have.

Fundamentally, the electronic forums are unforgiving “seeming” because the topic is so fundamentally technical. The barrier to entry is a huge set of very specific, technical terms and lingo to describe things very specifically. And often, way too often, beginners (while very welcome) ask difficult questions to answer without context. If you don’t post your schematic (or some circuit drawing) and say your thing isnt’ working, there isn’t much to guide you with - just like here, you can’t say “what’s wrong with my plants” and not provide any background info like nutes/environment/pH/etc. But I’d be happy to help ya however I can. I’d like to start with that circuit part so you have hardware that WORKS before you start testing code on it.

2 Likes

Hi guys. I’ve been in the middle of large data backup, which I’m doing in parts, and also avoiding/putting off trimming. Haha. Not to mention other things. But I’m going to get the files and information I need to show what I have/had going and to answer your questions tonight, and will post no later than tomorrow.

I will get the code (the code rat trap chronicles shared with me) from whatever folder/drive it’s on and post it. Thank you.

This is true, I absolutely agree.

Absolutely, and that’s a good analogy.

I will get everything I need to answer (accurately, instead of of memory alone) these questions shortly.
Thanks very much.

@FieldEffect @sfzombie13

Ok, first I’ll post the stuff relating to the guy/youtube channel “Rat Trap Chronicles” (Who’s channel was just vaporized days ago). I had copied this text from the description of (probably) the first video where he implemented an arduino into the trap [NOTE: I tried to at least italicize when quoting him, and use italics for all but the code copy/paste. Can we not change font color in this forum?]:

"This big rat escaped unharmed from a previous encounter with a modified Victor Trap. That trap had a small increase in spring tension and added nails. The rat easily escaped and was walking and eating fine the very next day.

See https://www.youtube.com/watch?v=eUlcj… So I knew this rat wasn’t going to bite the trigger on another trap. So added a wedge to preload the spring even more. Adding 50% more clamping force at the closed position, double the number of nails and added a saw blade to the other side of the striking bar. To solve the avoidance of the trigger I added an Arduino Pro Mini with an IR break sensor that will activate a solenoid to pull the trigger. After I removed the power indicating LED, it draws only 150 microamps. It worked, but not as well as I expected. After reviewing the video, thought it should have been triggered many times before it finally did work. The firmware needs some fine tuning to increase it’s performance."

There were also timestamps with comments, for example:

33:38 I thought the trap should have been triggered at this time.

Next, this is Rat Trap Chronicles’ (RTC) reply to my request about sharing code (which I noted came from his 33rd arduino rat catch):

"I used a Pro mini clone. An R3 should work fine, easier to work with. I chose the Pro mini because after I modded it removing the power on LED and replacing the voltage regulator it uses very little power, so that I can run it off of a 2AH 12v Milwaukee tool battery for over 6 months. Here’s the code. It is much more complicated than it needs to be because of the added logic to reduce power consumption. I put the processor to sleep most of the time, waking up, power on the IR emitter only when needed. I found out power cycling the IR sensor on and off made them fail in a few days or less. I didn’t test any future and just left the sensor on all the time, using more power. With more testing and rewiring the power consumption could be cut by more than 50%. Also I discovered that the IR lighting used by the webcam interfered with the IR sensor so I had to add a shield so that the webcam’s light had less interference. I still have a problem when the webcam turns the IR light on and off and back on. So I just leave the IR light on all of the time.

Sorry for the shitty formatting a cut and paste didn’t preserve the layout."

#define DEBUGP

/*-------------------------------------------------------------------------------------

Code without turing on LED and sensors = 0.140ma

Reading sensors = 13.5ma

Time to read sensor min 8us with my finger over the receiver, 36us with fluresent light overhead.

Time to excute loop without prints about 104us

Worst case assuming the leds were on 100% during the main loop, then the average overall power

consumpsion is (104us * 13.5ma + 120000us * 0.140) / ( 120000us + 104us) = 0.152ma

Power to light LED is 14.6ma, so the terminal loop LED flashes consumes

(14.6 * 60 + 16000 * 0.140) / (16060) = 0.124ma

All test done with Vcc at 5v. Using raw input it use will be a little more from the regulator.

The IR sensors needs settling time to accurately tell the state of the IR beam. If the number is

too low then even when the beam has a clear path it will signal the beam is broken. If the number

is to large time and power is wasted. Bench testing seems that the minimum is 40us. But it may

be different under other conditions of ambient light.

New data with IR sensors powered all the time from Vcc. 464ua used during sleep and 13.24ma when

reading sensors. So if

Time to excute loop without prints about 104us

Worst case assuming the leds were on 100% during the main loop, then the average overall power

consumpsion is (104us * 13.24ma + 120000us * 0.464) / ( 120000us + 104us) = 0.475ma

consumpsion is (104us * 13.24ma + 64000us * 0.464) / ( 64000us + 104us) = 0.485ma

Settling time. With the sensor hood testing shows that 0 fails, but 20 usec works. Double it to 40

*/

#define LED_ON PORTB |= 0b100000

#define LED_OFF PORTB &= !0b100000

#define COIL_ON PORTC |= 0b100

#define COIL_OFF PORTC &= !0b100

#define SETTLING_TIME 40 // wait up to usec time after powering IR sensors before reading

#include “LowPower.h”

/*

Pro Mini

I/O port assignments.

PB5 - LED indicator

PC0 - IR LED power pin 14

PC1 - IR Sensor input 15

PC2 - Coil power enable pin 16

PD0 - RXD serial debugging

PD1 - TXD serial debugging

PCB Wiring

Pin A0 switched power to IR LED

Pin A1 to IR Sensor input

Pin A2 to coils enable

Pin RAW on right side at the top is +12 unregulated power.

GND

-------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------

  • Returns non-zero if IR sensor is active.

------------------------------------------------------------------------------------*/

unsigned char Sensor_Read(unsigned long settling_time_usec)

{

unsigned char s;

PORTC |= 0b01; // power on IR LED bit 0

delayMicroseconds(settling_time_usec);

s = ((PINC >> 1) & 1) ^ 1; // read sensors invert to convert to active high

PORTC &= 0b11111110; // turn power off

return s;

}

void loop(void)

{

if (Sensor_Read(SETTLING_TIME))

{

  LED_ON;

  COIL_ON;    // trigger solenoid on

  LowPower.powerDown(SLEEP_250MS, ADC_OFF, BOD_OFF); 

  COIL_OFF;  

  LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF); 

  for ( ; ; )

  {

#ifdef DEBUGP

  Serial.println("Termimal sleep loop.");

  Serial.flush();

#endif

     LED_ON;

     LowPower.powerDown(SLEEP_60MS, ADC_OFF, BOD_OFF); 

     LED_OFF;

     LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 

     LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 

  }

}

LowPower.powerDown(SLEEP_60MS, ADC_OFF, BOD_OFF);

}

void LED_Blink(int count)

{

while (count–)

{

  LED_ON;

  LowPower.powerDown(SLEEP_250MS, ADC_OFF, BOD_OFF); 

  LED_OFF;

  LowPower.powerDown(SLEEP_250MS, ADC_OFF, BOD_OFF); 

}

}

void Sensor_Test(void)

{

int tn, i;

unsigned long st;

#if 1

LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);

LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);

#endif

tn = 1;

for (st = 0; st <= 100 ; st += 20)

{

#ifdef DEBUGP

  Serial.print("Delay ");

  Serial.println(st);

  Serial.flush();

#endif

  LED_Blink(tn++);

  LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); 

  for (i = 0; i < 5; i++)

  {

     if (Sensor_Read(st))

        LED_Blink(1);

     else

        LowPower.powerDown(SLEEP_500MS, ADC_OFF, BOD_OFF); 

  }

  LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF); 

}

}

void setup()

{

int i;

// POWER REDUCTION REGISTER

#ifdef DEBUGP

Serial.begin(2000000);

Serial.println(“Rat Trap Startup”);

Serial.flush();

#else

PRR |= 1 << PRUSART0;

#endif

PRR |= 1 << PRTWI | 1 << PRSPI | 1 << PRADC; // maybe can add timers PRTIM0 PRTIM1

DDRB &= 0b11100000; // 0 to 4 set to inputs, 5 LED, 6 and 7 crystal.

PORTB |= 0b00011111; // set pulls to high

DDRC &= 0b11000000; // set all pins to input but 6-reset and 7-n/a

DDRC |= 0b00000101; // 0 is used to power IR LED and sensor, 3 is coil enable

PORTC |= 0b00111010; // bit 2 IR sensor pull up

DDRD &= 0b11; // 0 and 1 are for serial, set 2 to 7 as inputs

PORTD |= 0b11111100; // set pullup resistors

// Blink LED 5 times to indicate good startup.

for (i = 0; i < 5; i++)

{

  LED_ON;

  LowPower.powerDown(SLEEP_60MS, ADC_OFF, BOD_OFF); 

  LED_OFF;

  LowPower.powerDown(SLEEP_250MS, ADC_OFF, BOD_OFF); 

}

Sensor_Test();

}

-----END OF HIS COMMENT-----

So he didn’t use an arduino nano; he used a clone of an arduino pro mini. And the code looks like what would be used in the arduino IDE (void setup, void loop, etc.). The other video where a man used a different coding language was this one, I think: https://www.youtube.com/watch?v=lYImd3WCjZM This is ‘besides the point’ though.

Anyways, I found the code shared by RTC to be over my head, especially at the time. I don’t think I’d started trying to put anything together at that point.

In the next post, in a little bit, I’ll share what I have so far. It’s basically what I followed from a BC-Robotics write up, in addition to some other sources. I’ll list all the parts I used, and add links where necessary.

Thanks for your time.

I don’t know why some of the “code” that I copy pasted, from RTC, that he shared with me, is displayed like it is above^. Maybe the format, and the " { " type characters.

Alright. This is the path that I took. (I didn’t use any code from Rat Trap Chronicles guy). I already had an Elegoo Arduino UNO R3 Kit. And I think I’d already ordered break beam sensors prior.

Tutorial I ‘followed’ from:
BC Robotics - Controlling a Solenoid Valve with Arduino. Though I’m not controlling a solenoid “valve”.

Parts I used:
1x Arduino UNO R3 (Elegoo Brand) (LINK)

1x Adafruit Mini Push-Pull Solenoid, 5V (LINK)

1x Adafruit Break-Beam Sensors - 3mm LEDs (LINK)

1x Solderless Breadboard, and Jumper wires
(I had some from the UNO kit, and another breadboard and jumper wire kit. I think I’m using the breadboard from the other kit. (I’ve read that breadboards can often be the source/cause of “issues”, especially with ‘prototyping’ and perhaps especially with noobs…).

1x TIP102-VP Darlington Transistor (LINK). Note: I didn’t pay attention, and got TIP102 instead of the TIP120 that is “recommended”. This other guide (THIS GUIDE) I referenced said that TIP102 would work as well, so I proceeded with the TIP102.

1x 1N4007 Diode Rectifier. From The UNO kit. (LINK - Datasheet). I knew that some guides said “1N4004” OR “1N4001” would both work, but I honestly don’t remember noticing the kit came with “1N4007”. I might have misread the “1” as a “7”.

1x 1K Ohm Resistor. It’s either from the UNO kit, or another kit of resistors I got.

(NOTE: This is the 12v solenoid I wanted to use as far as being 12v LINK. They have a note: "Revision History

  • As of Jan 17, 2018 we’re selling this solenoid with a 12VDC actuation voltage instead of 24V"

I found that with my circuit (which now seems at least suspect, regarding transistor and diode) the 12v solenoid either didn’t fire, or was laggy and weak in its actuation.)


I do have a folder for this ‘project’, where I have some datasheets and the like. I mostly used them for diagrams just to make sure I was using the correct lead (polarity, or correct function in the case of the transistor).

I found there to be a slight difference in the functioning of the circuit (eg: “strength” the solenoid fired with) depending on if I powered the UNO via the (5v) usb connection to laptop, 12v/2A wall adapter via the dc barrel connector on the UNO, or 12v/2A wall adapter via the VIN and GND “Power” header connections on the UNO.
I believe, and from looking at the UNO now, that I “settled” on using the 12v wall adapter connected via DC in barrel jack, and jumped the 5v power from the UNO to power the IR break beam, and jumped the 12v VIN power to the part of the circuit that’d power the solenoid.
I did attempt to get an idea of what was present at a couple points in the circuit using my DMM, but I didn’t get far with that and didn’t keep what I scribbled down.

I still have the circuit setup as it was, and I can and will take a pic or two to upload. With the 5v push pull solenoid, the bc robotics code and circuit works. The IR sensors are connected to a piece of wood six inches apart, in line, and when powered up, I break the beam and the solenoid fires.

1 Like

Nitt,

This will take me a while to digest - give me a day or two to go through it. Thanks for posting though, we’ll get 'ya sorted out

1 Like

I often struggle with the line between too much, and not enough details or information.
Thanks for understanding. No problem, take your time.

In the meantime, I’ll try and see if I can get myself back up to speed, and to where I was at. At least I got my buds bucked off and into buckets now, so final trimming isn’t as urgent.

Thank you.

I’ve got some hobby experience in this area. My grow tent lights are currently controlled by a raspberry pi zero w switching a solid state relay. I set the schedule from a web interface using node red.

I’ve used a bunch of other micros. Arduino is great because you can get the toolchain up and running in minutes. That alone makes it worth it for small projects.

I’ve used the MBED platform with some ST Micro ARM Coretex M4 chips. Those are some powerful little things, and the MBED system is easy. Compiler is in the cloud, so you have zero setup. It drops you a binary file, and you just drag and drop it into your microcontroller like a thumb drive. ST mbed boards are cheap. They have an M7 Coretex that is a beast. I want it, but don’t really have anything to use it for.

The one I like the most is the PSOC line. They have a cheap dev board for the PSOC5 that is awesome. Their software is pretty easy to setup and use. They have programmable logic cells that are awesome for some things like interfacing ccds. Plus every pin is configurable, so you just hook shit up and then configure it in software. They can do usb as well. I really like their products. Even the PSOC4 is sweet, just significantly less powerful.

Oh yeah, also if you want to do wifi projects, then the ESP 32 or 8266 is cool. Wifi can be pretty important to a lot of projects. I’d probably grab an ESP32 for most things these days. You can install an add on and program it with the arduino software.

2 Likes

looks like javascript from the comments. the format threw me off until i read that it was autoformatting. looks funky to me but i love a challenge.

1 Like

Most of the stuff you mention beyond this point is over my head, and I’ve not heard of these things. But have you seen ledgardeners raspi environmental controller build series. I haven’t checked it in a while, but it was pretty great.

I’ve heard of the ESP32 (and 8266) recommended for some projects, mainly for this reason; the wifi. I actually have some in an amazon wish list, with all the other ‘stuff’ I thought I’d consider and might need for this, and possibly other, projects.

Whats your background? EE? Or did you get this all from just hobby experience and self taught/learning?

Thanks.

1 Like

Do you mean the code right after he says: “Sorry for the shitty formatting a cut and paste didn’t preserve the layout.”? Javascrip hey? Hmm. I recognize some “statements” in there (void setup and loop, if else, serial.print, etc.) but I bet some of these languages share and/or have similar terms. And he did say that he’s using an arduino pro mini clone.

Sounds good. Thank you!

This guy’s trap worked flawlessly. It ran off a 12v, 2ah, milwaukee tool battery. And if I could get the guys code to work for me, we’d be so happy. I’d buy any components I didn’t have just to make sure I had the same hardware setup as he used. Which wouldn’t be much, maybe a used milwaukee 12v battery (or a knock off, or rig up some 18650 batteries, which is almost certainly what’s in that 12v milwaukee, and I have some lying around from an old 18v tool battery pack - they might be too old now. I like 18650s form factor, amount of amp hours and relative ease of acquiring them). Unfortunately, I don’t think he listed any other components (transistors, resistors, diodes, etc.) that he used. The solenoid plunger was connected to some part of the trigger/pan of the trap with a short length of thin cable (ie: a string, basically) and would pull the trigger, releasing the “permanent sleep” bar of the snap trap.

I have two of his videos, which I downloaded. I found them, and can share a link (either to view, or download), or a few still images here in the thread…? I’ll use my degree in MS paint to edit out the sleeping rodent.

i’ve got a whole drawer half full of 18650s from laptop batteries i took apart. i picked up some old microvwaves from the side of the road to make a battery welder to put them together. video instructions here and here. most languages share some common terms but you can tell what one you’re working with by how they put the comments in. pretty sure every language is different with the comments, but there may be a couple that share the format for them. i’ll have a look later this week or today if i have time. if i copy it all to an ide it may format it for the kind of code i call it automatically, although i think javascript is one you can use without formatting. python wouldn’t run like it is at all. of course you have to change the variables and values of things like ip addresses and other things that can change.

1 Like

Still image of RTC’s arduino trap (the other IR sensor is directly across, out of view).

Just chiming in to say that “//“ comments to the end of the line in several programming languages. C++, java, and JavaScript are three I know for sure. If you’re just looking at a block of code out of context there’s not really a foolproof way to know what exact language it is. I don’t know for sure but I’m guessing that code above is C or C++.

1 Like

Good news Nitt!

This is pretty straightforward. I got the code reformatted in the arduino IDE, where it belongs. I have some concerns about some of the low power modes working properly with a different Arduino unit, do you have issues compiling it and loading it? Or does it compile and not do anything?

This would be easier to debug if maybe Friday morning I send you something to try WITHOUT all the power saving stuff. I think getting it running, then making it low-power, is the best path. What do you think of that?

I attached the .ino file renamed as .pdf. Change the extension to “.ino” and you can load 'er up in the Arduino IDE.

rattrap - FE format.pdf (5.4 KB)

1 Like

well, there aren’t many languages that share comment structure. if you look here you’ll see that javascript, c, and c++ share them while many others have their own style. i have a small batch file i wrote to tell me what kind of code it is by analyzing the comments, but didn’t use it this time since i had been reading a bunch of javascript last week and recognized the comments or i would have seen that it could have been c or c++ as well. thanx for pointing that out though. and now i don’t have to bother with it this weekend since it’s already been done.

1 Like

That is good news, and thank you so much!

I never tried to do anything with the code from RatTrapChronicles. I looked through it, and his comments about it. But I didn’t feel confident enough to even try anything with it at the time.

Sure. I’m fine with that, or whatever you think is the best way to go about it. I’ll try whatever you think is the best path with this.

Ok. When I logged in a few hours ago and saw your comment, I read it twice, and then I downloaded the file. I had some issues, but I think I got those issues resolved.

First I moved it from my downloads folder to my desktop. I changed the extension from .pdf to .ino. I tried to open it in the IDE, but I got this error:


I clicked “OK” and it basically made a folder for the file with the same name and placed the file in the folder. Trying to open it again, the same way, gave me a new error:

I then redownloaded the file and just left it in the downloads folder. Trying it again from there, I got the same two errors.

So I tried opening some sketches from another folder (like the sketch that’s on the arduino currently, and some other things) in the IDE, and they opened without issue. Then I noticed that the file naming “convention” for all the sketches in my folder, etc., were different; they have no spaces, or hyphens, and in place of a space they have an “_” (underscore). So, I renamed changed your file to not have spaces (rattrap_FE_format.ino) and then tried to open that, and it worked. I feel like I accomplished something, hahaha.

I watched a quick video, and plan to watch a couple more because I need a basic refresher, to say the least. And I’m gonna read through the sketch now.
I haven’t tried to upload your file to the UNO yet.
I’m thinking I’ll order a pro mini, and maybe an esp32 or esp8266 from amazon tonight, unless someone says otherwise. I figure that might make things easier.
…I also wonder about other components he would’ve originally used. How can I know? Resistors, transistors, diodes, octocouplers?

Thank you so much!

1 Like

Thank you for chiming in, man. I appreciate it.

Thank you as well, zombie. I checked the wikipedia page for the list of programming languages; there’s only about 4 million. Haha.

1 Like

Sorry about the naming confusion! I just made a copy and edited it with the description and new suffix without even thinking about it. Good job getting it figured out :+1:

I’d use a MOSFET instead of the darlington transistor to switch the solenoid. Specifically a “logic level N-channel MOSFET.” Something like this N-channel power MOSFET [30V / 60A] : ID 355 : Adafruit Industries, Unique & fun DIY electronics and kits

Make sure you get something in the 30-60V at least 10+A range so it’s flexible and a tad more robust if you have issues. This isn’t mandatory, it’s just a better, more modern way to do what your Darlington transistor does. It’ll likely do better driving your solenoid. They’re pretty generic for something like this, it doesn’t really matter too much what particular one you find other than logic-level gate and a higher voltage rating than 30V or so.

Sorry my posting from the phone is such a mess, it just doesn’t work as well as sitting at the computer for whatever reason

I don’t know where you are, guessing Canada. I don’t know where you buy stuff like this. I use Digikey and Mouser in the US.

I’ve mostly used the Mega or the standard original Arduino, the Nano took me a while to get running, but I wanted to use something cheap for my last Arduino project.

In the mean time, try to get your Arduino, with nothing connected, to compile/load/run the blink and hello world examples. We want to be sure you have everything setup to compile and run those basic programs first. There are lots of tutorials that are more efficient and probably better written than I can, and for your particular Arduino model

Post your issues if you have any.

1 Like