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

Yeah, there’s couple ways you can use the esp. One option is to use the arduino software to program it. But people have also written “firmware” that you load onto it and it sets up a like web server and gives you some minimal scripting capability. I have used a project called ESPeasy: ESPEasy - Let's Control It

I’m all self taught for microcontrollers, but I have had some basic electronics courses. There’s a lot of great information on some of the platforms I’ve mentioned. If you can do arduino, you can do esp or mbed with a little googling.

1 Like

+1 on this, I’ve used Arduino a bit for controlling relays and found it pretty straightforward, I used it for a controller for a camera based photogrammetry…

This

To control this

For making 3d models like this

I’ve also worked a lot on mocap systems in my time, including inertial bases systems, I always had drama with electromagnetic interference/ noise being an issue, even when using Kalman filtering etc. There is also the issue of error accumulation if you end up using dead reckoning for root translation, that said most of the local orientation data is usable and we ended up using optical tracking for root translation and that produced usable mocap data, especially for game dev.

1 Like

I looked through some of your other details. Your Uno will work fine. Make sure you select that as your target in the Arduino environment, get your blink and hello world examples going. Most of the tutorials were written around the Uno, so follow those.

Your solenoid isn’t as big as I would have probably used, makes me worry less about the TIP102. Not concerned, but MOSFETs are better for this stuff. We can use either here without much effect at this level. Kinda similar to LEDs - we use LEDs for grow lights now, the darlington is kinda like an OLD MH and ballast system. Either will work, you run what 'ya brung.

Nice break beam sensor. That’ll work fine as well. If you order from Adafruit get those FETs I linked from there.

The majority of the components here are totally non-critical, just have to stay within certain limits. The best way to learn about those limits is to dive in, and I’ll give you suggestions however I can.

We’ll be able to get this going :sunglasses:

2 Likes

Ok, thanks. Sounds good. I did a little bit more last night. I’m watching a newer ‘newb’ arduino intro video now, as a refresher. I’ll give details on what I did last night, and then what I get into tonight.
Got some garden stuff done today. Shovelling snow is a pain in the ass, or back, rather. Gotta refuel in a bit.

I’ll update asap.

I tought I’d also noted that I do have two of these: Adafruit LINK, but something about what I’d setup (code or hardware) had it firing weakly, slowly (lagged), or not at all. I’ll share pics of that circuit, and code later too.
EDIT: I also have found some “better” (not quality) solenoids, mostly 12v, but with longer throw. Though the longer throw is likely not needed for a basic “snap trap” style trigger/trigger pan.

Thanks again.

1 Like

Yup, canada. I’ve used both Digikey.ca and Mouser.ca or .com before. I like digikey.ca. I like amazon.ca because I have prime. And I use amazon.com sometimes.

I’m not sure what the “original” is. I got the UNO R3 (kit) because I’m new, and whatever research I did suggested the UNO was a good choice for newbs. A smaller board might be better. I don’t mind getting something else.

OK. Last night I got the IDE installed on my macbook pro, this mbp is essentially empty and I haven’t been using it at all. I put the new IDE 2.0.x on it, I duno why. Anyway, it’s bit different (not that I’m that fluent with the original IDE), and I had to configure some things. Got that figured out.

For the last however many months, I’ve had the UNO and breadboard connected and “working” with a basic break beam demo, and wiring.

Wiring like this: LINK, with maybe a change or two. I might have more jumper wires used (for me to understand, at the time), and I powered it with a 12v/2a adapter via the DC in (barrel jack). Then I used the Vin pin for 12v for the solenoid, and the 5v pin for the break beam sensors.

The code I used is from here: Arduino | IR Breakbeam Sensors | Adafruit Learning System, with maybe a minor change or two (I just played with the “delay” for how long the solenoid fires when beam is broke, for eg.).
Here’s the actual code that’s been on, and is on the UNO now:

/*
MODIFIED IR Breakbeam sensor demo. V1.3. Attempt to have trap turn off after tripped
*/

#define LEDPIN 13
// Using pin 13 to control solenoid.

#define SENSORPIN 4
// Using pin 4 as the sensor pin for IR signal.

int sensorState; //State of IR sensor.
int triggerCount = 0; //Number of times trap (solenoid or beam?) has tripped.

void setup() {
// initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup

Serial.begin(9600);
}

void loop() {
// read the value of sensorState:
sensorState = digitalRead(SENSORPIN);
// read the value of the triggerCount?
int triggerCount = digitalRead(LEDPIN);

// check if the sensor beam is broken, if so, sensorState is LOW:
if (sensorState == LOW) {
// turn LED on:
digitalWrite(LEDPIN, HIGH);
delay(300);
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
}
// if the triggerCount is > 0, “disarm trap”:
if (triggerCount > 0) {
digitalWrite(SENSORPIN, LOW);
}
else {
digitalWrite(SENSORPIN, HIGH);
}
}


Obvious issue example, if the beam were to stay broken (like if it caught something) the solenoid would stay in “on”/actuated). I tried to play with some other statements/functions to learn how to deal with this, but failed.

Anyways,
Last night I connected the 12v/2a adapter to the setup I described above, and tested it, it works. I knew it did, I haven’t changed it since then.
Then, on the mbp, I uploaded “blink” and ran that (note: UNO was connected to mbp, and the break beam circuit while I tried blink, IIRC). It worked fine. I’ve done “blink” and I think “hello world”, maybe one or two others, while following a series on youtube long ago.
So, I reuploaded the “modified” breakbeam sensor demo sketch, and it still works.

Just now, I opened your sketch in the IDE 1.x.x on my windows laptop (no arduino connected right now). I tried to verify it and got this error:

"Arduino: 1.8.19 (Windows 10), Board: “Arduino Uno”

rattrap_FE_format:30:10: error: #include expects “FILENAME” or

** #include “LowPower.h”**

** ^**

C:\Users\admin\Desktop\RatTrapChronicles Code - Formatted By FE\rattrap - FE format\rattrap - FE format.ino:30:10: error: #include expects “FILENAME” or

** #include “LowPower.h”**

** ^**

exit status 1

#include expects “FILENAME” or

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences."

1 Like

I appreciate your time, and expertise @FieldEffect (and all of you!), so feel free to tell me what info is unnecessary to post, etc., or anything like that.
A few pics of what the circuit I have wired up for that demo sketch, unless not necessary.

Thanks.

Nitt,

No worries! I love helping people, but the opportunities with this sort of thing are limited.

  1. Don’t expect rattrap code I sent to compile yet. It was intended for a different Arduino and we probably need to change some things. Like I said, let’s just get it running first.

  2. First thing in the morning I’ll edit the code you have been running to shut the solenoid off. We’ll check that works properly and move towards lowering the power consumption :+1:

1 Like

Just be aware during the week (Monday thru Thursday) I’m a busy guy with work and family. Tomorrow I’ll be able to spend some time contributing more :sunglasses:

1 Like

That seems to me like a whole lot of code for what is essentially switching logic, l will give it a proper look though in case I’m missing something.

+1 on the addafruit demo above, it looks like a simple place to start, you can just alter the output to do what you’re wanting instead of turning on an LED.
Another tip is to write it up as pseudo code with if then else statements and calls to function blocks, that way at least you will get the flow logic clear in your head.

Something like this might be of use also

2 Likes

I fully understand, of course! Honestly, I’m incredibly happy that someone is able, and willing to help with this at all! And “help” is a pretty big understatement. Hahaha.
No rush at all.

Ok, cool.

Sounds good. Whenever you get around to it is perfectly fine.
I wonder though, that is, I assume that the code that RatTrapChronicles wrote is going to be much better and more sophisticated for the actual task.
The code I’d played with was only meant to print “unbroken” and/or “broken” when the ir beam is in either of those states, and to light up the UNO’s pin 13 led when “broken”. All I did was use that pin as an output to trigger the wiring/hardware setup from the bc robotics “solenoid valve” tutorial. I think I got that right, hah.

Thanks.

2 Likes

Morning Nitt,

First, let’s walk through why your code isn’t running the way you expect it to. Then, I’ll propose a simpler alternative that should do what you want. Read through this entire post before you change anything, just try to follow along and understand these points, then the answer is easy.

Part A (for this discussion ignore the code I added around my note B comment):

  1. So you define those variables sensorState and triggerCount as globals at the top of the program. You define them as ints, which is OK but technically bool would be a better fit as they are single-bit variables (their value is either 1 or 0).
  2. At the top of the while loop, we set the value of sensorState to be the pin state of the sensor pin. We expect that to be “LOW” or 0 when we have a rat in the trap, and a “HIGH” or 1 when there is no interruption in the beam.
  3. I don’t like a few things about your setting of the triggerCount variable. First of all, the variable is already defined, so re-defining it every iteration of this loop is unneccesary/bad form. At a minimum, you’d want to remove that “int” starting the line. I understand what you were trying to accomplish here, triggerCount will go to 1 if you have the solenoid triggered.
  4. Your state logic based on sensorState is good! That’s why this works, part of the way anyway.
  5. The state logic based on triggerCount isn’t so good. Let’s look at what you have there. If we’ve got an obstacle, and we assume we just set the LEDPIN high (because beam is broken), we’d want to now deactivate the trap. But what happens instead? Well, triggerCount is based on the old state of LEDPIN (because it got set just before we activate the LEDPIN in that if (sensorState) conditional). So, it’s going to read 0 this go around. Look carefully at the PIN you are writing to. SENSORPIN.
  6. Having your state setup before your solenoid deactivation routine doesn’t work the way you expect because the solenoid will still continuously try to set LEDPIN high, even IF you were properly deasserting it with the triggerCount condition below that. This will run almost instantaneously, you won’t even notice that pin would ever go low without an oscilloscope.

OK, so if you want to make this work, I’d do a few things.

  • Let’s make triggerCount a variable based on logic, not a pin state. I’d delete this int triggerCount = digitalRead(LEDPIN);
  • How about we set triggerCount = 1 when we turn on LEDPIN after a beam break? I’d add triggerCount=1; just after your 300ms delay
  • Let’s take another look at that output condition to deactivate after you trip. First, fix the pin issue, replacing SENSORPIN with LEDPIN. Now, we still need to deal with the issue that you are effectively just retriggering with a 300ms hold time the LEDPIN with the if(sensorState) condition. So, I’d try to make that happen ONLY if triggerCount was 0 still.

On to Part B (let’s only pay attention to the code fragment purple-boxed around NOTE B.

  1. OK, so I’ll make a condition where I want to toggle the LEDPIN high only if the beam is broken AND the triggerCount is 0. Great, this is how you write that.
  2. While we’re at it, let’s just execute the entire solenoid pulse. We’ll set it high, wait a while (you may want to make this longer than 500ms), then turn it off. In order to prevent this from ever happening again, now let’s set triggerCount to something other than 0. If this is the ONLY place we touch that variable, it won’t ever be 0 again (unless you reset the microcontroller). I think this is what you want.
  3. That’s it. Get rid of all your other code there. The only thing in your void loop should be the stuff in the purple box. That should do what you want. All the stuff above the void loop stays the same for now.

Here’s my total code that should work:

#define LEDPIN 13
// Using pin 13 to control solenoid.

#define SENSORPIN 4
// Using pin 4 as the sensor pin for IR signal.

int sensorState; //State of IR sensor. FE NOTE: Proper form to use bool type
int triggerCount = 0; //Number of times trap (solenoid or beam?) has tripped. FE NOTE: Proper form to use bool type

void setup() {
// initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, LOW); //FE Addition drive the output low by default
// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup
Serial.begin(9600); //FE NOTE: This isn't needed unless you are sending serial data back to your terminal. It's not a problem either.
}

void loop() {
  // read the value of sensorState:
  sensorState = digitalRead(SENSORPIN);

  if (sensorState==LOW && triggerCount == 0){ //FE Addition if triggerCount is zero, and beam is broken we enter this routine
digitalWrite(LEDPIN, HIGH);
delay(500);
digitalWrite(LEDPIN, LOW);
triggerCount = 1;        //FE Addition, triggerCount set to non-zero, meaning we can never enter this subroutine again (until you reset it to zero)
  }
}
2 Likes

I’ll also say that the rattrap code is more complicated just because he’s using a different sensor, and he’s shutting down the Arduino most of the time to conserve battery power. Funtionally, it’s very similar to what I suggested above. I don’t think it would actually do anything better other than conserve power. Me, being lazy, would probably just power this thing with a wall adapter and not worry about it. You already use one, if you can use that where you intend to place this trap, use it and we can tweak the code much more easily. It’s easy to add funtionality at this point, and if you want to save power, we take those steps after it functions just the way you want it to.

2 Likes

A couple things I remember about the circuit and code I had/have:
-Upon plugging in the 12v adapter, some times, but not all the time, the solenoid will very rapidly fire on and off several times. Maybe 3 times on-off, within abot a half second.

Is this as simple as changing “int sensorState;” to “boolean sensorState;”, and changing “int triggerCount = 0” to “boolean triggerCount = 0”?

The “while loop”? I don’t see it.
And are you only explaning what’s expected here, or is there an action you want me to take with that part?

Ok. I think I understand this; it’s… redundant at best? And plain incorrect/could or does cause an issue at worst?

Literally just remove the “int” but leave the “triggerCount = digitalRead(LEDPIN);” part? Just checking.

I think that’s right; that’s probably what I was trying for.

Do you mea this part:
" // check if the sensor beam is broken, if so, sensorState is LOW:
if (sensorState == LOW) {
// turn LED on:
digitalWrite(LEDPIN, HIGH);
delay(300);
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
} "?

I don’t quite understand this part, the part in bold particularly, but I’m trying to. I’ll keep trying.

Ah. Are you saying that the digitalWrite in this part should have been to LEDPIN, not SENSORPIN? If so, that might have been a more “basic” mistake, as in I wasn’t paying attention or ‘typo’. (I think in one of my previous ‘attempts’ I had been calling the LEDPIN “SOLENOID PIN” instead, which I think makes more sense). (OK, I see your note further down confirming this is indeed the mistake, and fix).

I think I understand the gist of this. It’s essentially an “order of operations” error? (Aside from the “not properly deasserting” issue). The “solenoid deactivation” code should come before the “state setup”?

(I just thought of this question, a day later; is there a way to display numbers on each line in the IDE? Know what I mean?)

Ok, so I’ve been reading this over and over, and trying to understand every single thing before moving ahead and trying to either modify my “demo” code OR copy paste your “total code that should work” (both so I can learn, and to not feel like I’m cheating or being lazy). I think I’m getting most of it, maybe 60-75% of it.
But I’m just gonna post the above^ reply now, and try out the code you’ve posted here, or I’ll never get anything done. I’ll update later.

Thank you!

1 Like

He’s using a different break beam? Hmm. I thougth he’d told me he was using adafruit’s. Ok.

The battery power ability is going to be important for me in the end.

Ok.
But in the end, the trap(s) are ideally going to be placed outside, some inside - sure. Ideally, I’d like to end up with some functioning code, and a set of known parts/components that I can buy, and put together the arduino/microcontroller, connect the components, and apply or “connect” this to any snap trap, to make multiple of them with ease and repeatability.
I also plan to give one or more to my neighbors. We are connected (buildings), so when rodents get in one home, they get in all homes. It’s a nightmare, I wrecked my already messed up back cutting up the deck to get underneath it and use galvanized hardware cloth to “fix” an 8 foot by 3 foot area and block the main entry they were using. But they still get in, elsewhere (neighbors, etc.). There’s one that goes around the kitchen etc. now. Stressful. Hah.

Thanks again.

I’m gonna try that code you’ve given me above now.

(EDIT: I might as well quickly mention that I have a 'reapeating trap", GoodNature A24, that maybe in the future I’d like to fit it with the same “set of components”, upgrading it’s absolutely ‘shite’ “leaf trigger” mechanism with an ir break beam. This A24 is one of the biggest wastes of money we made. Just garbage.)

The sensor probably is the same, I must have been mistaken while formatting that earlier code.

Seems the fundamental difference then is the shutdown during off periods. We can sort that out once we get it running, you’ll need to wire up the sensor a little different than you have it now (we will want to run the transmitter LED only when you are checking if the beam is broken).

Let me know how that code runs.

1 Like

Using windows laptop, I verified the code, then uploaded it. It seemed to operate how we wanted. ( I could see the pin 13 led light up for .5 sec).

I went over to macbook area to connect it to 12v adapter (because that’s where I had the 12v adapter, which due to how I wired it, is needed to power the solenoid). It operated how we wanted. Then I connected it to the macbook pro, moved your sketch over with usb thumbstick, opened IDE, and verified again. Then when I go to upload, I get error based on ‘port’ selection. The only option that shows up is incoming_bluetooth or something. So I spent some time trying to find info on that error, installed some ‘driver’, restarted, didn’t work.

I moved back over to laptop pc, now I’m getting the same issue regarding port on my laptop pc! So now it doesn’t work anywhere. I’m too irritated to continue with this right now. I’m gonna step out for a walk, and do some trimming for a couple hours. So frustrating.
Windows pc says IDE says:
Arduino: 1.8.19 (Windows 10), Board: “Arduino Uno”

Sketch uses 2100 bytes (6%) of program storage space. Maximum is 32256 bytes.

Global variables use 186 bytes (9%) of dynamic memory, leaving 1862 bytes for local variables. Maximum is 2048 bytes.

An error occurred while uploading the sketch

avrdude: ser_open(): can’t open device “\.\COM7”: The system cannot find the file specified.

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

And “port” is grayed out under “tools” in the IDE.

Sounds like mostly good news, at least the code works when you succeed in loading it.

I don’t know anything about Mac, so I can’t help you there. It sounds like your USB got enumerated under a different port.

In windows, I typically address this by closing the Arduino IDE, unplugging/replugging my Arduino and opening device manager. If you don’t know how to do that last part, type device manager into the windows search by the start menu, and open it.

You want to look at Ports (COM and LPT). You should find the connected Arduinos here, note their COM port before opening the Arduino IDE.

Now open your IDE, and make sure you have the correct port. Should work after that.

Let me know how you get along. I’m looking at how to run the uno low-power.

1 Like

Also, I hadn’t noticed your detailed reply to my main post. I’ll go over that in detail to help you along. Kiddo has been wild this afternoon :rofl:

1 Like

Ok, other half taking care of the little one for a while. Hopefully this helps, sorry this can be frustrating. I hate figuring out why things won’t connect or do what I want, only to find out it’s something stupid on the computer side.

This stuff isn’t easy to figure out. When I started, I had to build my own programmer with a parallel port interface. There wasn’t internet tutorials or online application resouces like there are now. I know frustration…deeply. I basically had to read my basic compiler manual and start trying things. And failing essentially all the time. Hindsight, it’s a wonder I made it past that. As soon as you get something running, all the frustration almost gets forgotten. Once you trap your first rat with this, you’ll arrive there as well, at least I hope :slightly_smiling_face: :upside_down_face:

This is because voltages are coming on/shutting off in rapid succession. We can avoid this by including a power-up delay at the end of the void setup(){} routine, a delay(100) should be adequate.

Yes. “bool” is the official way to declare a boolean for Arduino. https://cdn.arduino.cc/reference/en/language/variables/data-types/bool/

Explaining. I think of that void loop(){} as a while loop, which it is, but isn’t clear to anyone who hasn’t done some programming. I should have described it as the void loop(){}, my bad.

Correct. I’d just delete the int declaration, then your global definition towards the top of the program supercedes it. Redundant at best is probably the situation here, because it worked somewhat before.

I think you followed the rest of it. Don’t agonize over understanding everything yet, it comes with some experience and playing around. I like to pretend I’m the “computer,” and literally interpret every line for each condition and see which way the code tells me to go. For example, here’s my test code with line numbers displayed (to do this see How to show line numbers in Arduino IDE?. ):

Alright, so 1 through 5 just setup my background stuff. The setup loop runs before anything else. So, if I’m the Arduino, you just applied power to me, I start at line 7.

7: OK, here we go setting me up
8: Comment/ignore
9: I’ll setup this LEDPIN as an output
10: Make sure this LEDPIN is low, so I don’t accidentally trip the trap
11: Comment/ignore
12: SENSORPIN wants to be setup as input, OK
13: enable the pullup, OK
14: comment/ignore
15: going to twiddle thumbs for half a second (literally 8 million times at 16MHz)
16: done, go to main run loop()!

18: Ok, here we are at the main loop
20: What’s the state of SENSORPIN? Shove that value (1 or 0) into sensorState
22: Is sensorState 0? If not, go to Line 28. Yes? Great, is triggerCount 0? If Not, go to line 28. Yes, OK, let’s go to line 23.
23: Turn on LEDPIN (trap rat pls)
24: twiddle thumbs for half a second (8 million times again)
25: Turn off LEDPIN (release trigger pin pls)
26: Take this 1 and shove it into triggerCount
27: This just defines where we exit this if() conditional code
28-29: Loop back up to 20

1 Like

@FieldEffect Hi. I’ll go over your replies thoroughly once I get home from doing errands this afternoon.
Thank you.