You did it already. Here for example:
buttonPushCounter = sensorCountQuarter;
Now, when you see a pulse, decrement that variable. At the bottom of loop, if it's zero, stop the motors.
You did it already. Here for example:
buttonPushCounter = sensorCountQuarter;
Now, when you see a pulse, decrement that variable. At the bottom of loop, if it's zero, stop the motors.
Ok..... I tried this, but the motor still won't come on. I changed something in code and motor would come on but would not turn off.....
const int buttonUpQuarter = 8;
const int buttonDownQuarter = 10;
const int buttonUpInch = 11;
const int buttonDownInch = 12;
const int R_IS = 6;
const int R_EN = 2;
const int R_PWM = 3;
const int L_IS = 7;
const int L_EN = 4;
const int L_PWM = 5;
const int irSensor = 9;
long lastDebounceTime = 0;
long debounceDelay = 200;
int buttonState = 0;
int buttonPushCounter = 0;
int lastButtonState = 0;
int reset = 0;
int sensorCountQuarter = 8;
int sensorCount1inch = 32;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buttonUpQuarter, INPUT_PULLUP);
pinMode(irSensor, INPUT_PULLUP);
pinMode(buttonDownQuarter, INPUT_PULLUP);
pinMode(buttonUpInch, INPUT_PULLUP);
pinMode(buttonDownInch, INPUT_PULLUP);
pinMode(R_IS, OUTPUT);
pinMode(R_EN, OUTPUT);
pinMode(R_PWM, OUTPUT);
pinMode(L_IS, OUTPUT);
pinMode(L_EN, OUTPUT);
pinMode(L_PWM, OUTPUT);
digitalWrite(R_IS, LOW);
digitalWrite(L_IS, LOW);
digitalWrite(R_EN, HIGH);
digitalWrite(L_EN, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(irSensor);
if (buttonState != lastButtonState){
if (buttonState == HIGH){
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}else {
Serial.println("off");
}
delay(50);
}
lastButtonState = buttonState;
delay(50);
//This makes button press of quarter go up quarter of an inch after 8 signals from IR sensor it shuts off and resets button counter to 0
if(digitalRead(buttonUpQuarter)==LOW){
int i;
for(i = 0; i <= 255; i= i+10) //clockwise rotation
analogWrite(R_PWM, i);
analogWrite(L_PWM, 0);
buttonPushCounter = sensorCountQuarter;}
//This makes button press up one inch go up an inch
if(digitalRead(buttonUpInch)==LOW){
int i;
for(i = 0; i <= 255; i= i+10) //clockwise rotation
analogWrite(R_PWM, i);
analogWrite(L_PWM, 0);
buttonPushCounter = sensorCount1inch;}
//This makes button press down one quarter go down one quarter inch
if(digitalRead(buttonDownQuarter)==LOW){
int i;
for(i = 0; i <= 255; i= i+10) //counterclockwise rotation
analogWrite(R_PWM, 0);
analogWrite(L_PWM, i);
buttonPushCounter = sensorCountQuarter;
delay(50);}
//This makes button press down one inch go down one inch
if(digitalRead(buttonDownInch)==LOW){
int i;
for(i = 0; i <= 255; i= i+10) //counterclockwise rotation
analogWrite(R_PWM, 0);
analogWrite(L_PWM, i);
buttonPushCounter = sensorCount1inch;}
if(buttonPushCounter = sensorCountQuarter){
analogWrite(R_PWM, 0);
analogWrite(L_PWM, 0);}
if(buttonPushCounter = sensorCount1inch){
analogWrite(R_PWM, 0);
analogWrite(L_PWM, 0);}
}
You fell into the = and == trap again.
If (buttonPUshCounter = sensorCountQuarter)
Should be == ? And the one for sensorCount1inch?
= is value assignment.
== is boolean comparison.
A simple way to clean up your code a bit is to use the autoformat function of the IDE (ctrl-T shortcut key), give closing brackets their own line, and remove excessive whitespace.
Do try to get rid of those delay() calls. Not likely to be the cause of your problems now, but still not good for responsive code.
dave-in-nj:
DRO glass scale eould be easier
More accurate and might cost less.
Outdoors? This whole topic is turning into a Monty Python skit.
JCA34F:
Outdoors? This whole topic is turning into a Monty Python skit.
Not sure if it matters, but I am going to try again and attach the winch (that is geared way down) that I am planning to connect this to. I am planning to connect a 3d printed part to the main shaft (shaft of motor) the IR sensor will contact this 3D printed part once each revolution (I may increase if I need a higher resolution) The winch is geared down I;m guessing somewhere around 400:1. I am hoping this high number will allow fairly accurate precision.
This is on a sawmill. This will only be determining the width of cut of each board. They are rough cut boards. I don't need them to be accurate to the nearest .001. I have always used this winch by hand with a toggle switch, and I watch a ruler as I let it up/down to determine distance traveled. I'm guessing I probably get it accurate to 1/32nd (.03) of an inch maybe.
I am hoping this will be around that accurate or hopefully better than that. It may not be, and I may have to experiment with the sensor type to find the one that best suits my needs. I am just very appreciative of all of the members that are taking their time and helping walk me through this project. I am learning a lot (very slow sometimes) and hopefully will be able to use these basic coding skills I am learning for this and future projects! Thanks again for all of your help!
Thanks for the photos, that's helpful.
The IR sensor will work but is prone to dirt in that environment (fully exposed).
The magnet with hall effect sensor is a better solution indeed, not so easily affected by the dirt.
Now looking at this photo there's yet another possible sensor: the inductive sensor, which can detect teeth passing by. You can use that to basically turn a gear wheel into an encoder. That gives you an even higher resolution.
I'm quite sure that by counting single rotations of the shaft you will get to the best possible positioning with this system. The difference in height due to things like play in the gears and stretch in the wires is likely way more than a single rotation.
I will be able to experiment with sensors once I get the code working correctly, right?
I just can't wrap my head around this **** coding! I know what I want to do, but I don't know how to write it in code..... Would I be better or worse off if I used the pulseIn function for my sensor?
I do have another problem now though.... I think I let the smoke out of my motor controller. I connected it to the motor that I am intending to use with just a simple on off code. It seemed to work fine, but I reconnected it and I am assuming I let a wire touch or get loose and I saw the dreaded little puff of smoke..... Now nothing... Arduino still seems to be working fine, but motor will not turn on through the motor driver.
So, now I don't know that I have a way of testing my code until I can get another motor driver board in.
I have a L293d chip that is a motor driver. Obviously not powerful enough for my winch motor, but could I use it for trying to get the coding correctly with the little dc motor that comes with starter kits?
Thanks!
You may want to experiment with sensors is a standalone program - easier to experiment that way than to worry about whether your other code is causing problems.
If you have LEDs, you can set up something that indicates what the motor would be doing if you had a working driver.
I wouldn't bother with pulseIn for reading your sensor, just poll it with digitalRead.
Ok. I can do some testing that way.
I am pasting my latest code. I think it may be doing what I want, but I will have to test. I have read and reread your posts wildbill trying to figure out how to do what you say. My intention is to have the buttonPushCounter count backwards. When a button is pressed it will turn motor on in the correct direction and set the buttonPushCounter to the number of pulses that I want. It will loop back around and count backwards until it reaches 0. If (buttonPushCounter ==0) { Turn MotorOff.
I do have a couple of questions.
Ex. Push button sets buttonPushCounter to 8 nothing will happen until it loops back around. when it goes through the loop the buttonPushCounter does not need to reset to 0 it needs to count backwards until it reaches 0 based on the sensor pulses.
Thanks!
const int buttonUpQuarter = 8;
const int buttonDownQuarter = 10;
const int buttonUpInch = 11;
const int buttonDownInch = 12;
const int R_IS = 6;
const int R_EN = 2;
const int R_PWM = 3;
const int L_IS = 7;
const int L_EN = 4;
const int L_PWM = 5;
const int irSensor = 9;
long lastDebounceTime = 0;
long debounceDelay = 200;
int buttonState = 0;
int buttonPushCounter = 0;
int lastButtonState = 0;
int reset = 0;
int sensorCountQuarter = 8;
int sensorCount1inch = 32;
int motorOff = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buttonUpQuarter, INPUT_PULLUP);
pinMode(irSensor, INPUT_PULLUP);
pinMode(buttonDownQuarter, INPUT_PULLUP);
pinMode(buttonUpInch, INPUT_PULLUP);
pinMode(buttonDownInch, INPUT_PULLUP);
pinMode(R_IS, OUTPUT);
pinMode(R_EN, OUTPUT);
pinMode(R_PWM, OUTPUT);
pinMode(L_IS, OUTPUT);
pinMode(L_EN, OUTPUT);
pinMode(L_PWM, OUTPUT);
digitalWrite(R_IS, LOW);
digitalWrite(L_IS, LOW);
digitalWrite(R_EN, HIGH);
digitalWrite(L_EN, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(irSensor);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter--;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
Serial.println("off");
}
delay(50);
}
lastButtonState = buttonState;
delay(50);
//This makes button press of quarter go up quarter of an inch after 8 signals from IR sensor it shuts off and resets button counter to 0
if (digitalRead(buttonUpQuarter) == LOW) {
int i;
for (i = 0; i <= 255; i = i + 10) //clockwise rotation
analogWrite(R_PWM, i);
analogWrite(L_PWM, 0);
buttonPushCounter = sensorCountQuarter;
}
//This makes button press up one inch go up an inch
if (digitalRead(buttonUpInch) == LOW) {
int i;
for (i = 0; i <= 255; i = i + 10) //clockwise rotation
analogWrite(R_PWM, i);
analogWrite(L_PWM, 0);
buttonPushCounter = sensorCount1inch;
}
//This makes button press down one quarter go down one quarter inch
if (digitalRead(buttonDownQuarter) == LOW) {
int i;
for (i = 0; i <= 255; i = i + 10) //counterclockwise rotation
analogWrite(R_PWM, 0);
analogWrite(L_PWM, i);
buttonPushCounter = sensorCountQuarter;
delay(50);
}
//This makes button press down one inch go down one inch
if (digitalRead(buttonDownInch) == LOW) {
int i;
for (i = 0; i <= 255; i = i + 10) //counterclockwise rotation
analogWrite(R_PWM, 0);
analogWrite(L_PWM, i);
buttonPushCounter = sensorCount1inch;
}
if (buttonPushCounter == motorOff) {
analogWrite(R_PWM, 0);
analogWrite(L_PWM, 0);
}
}
neketege88:
- IF I buttonPushCounter-- does this mean the buttonPushCounter will count backwards from whatever its value is at the time?
It means decrease buttonPushCounter by one, so it will count down with each button press.
- In my code: when I programed the button counter/ state change I put in there things like lastbuttonstate = buttonstate; Do any of these things automatically reset the buttonPushCounter to 0.
No. buttonPushCounter only changes when you explicitly assign a value to it or do the decrement you referred to in question one above.
I've had good results using wheelchair motors for lift large weights
these type motors have internal brakes for holding loads
Ok, I think I've made progress.... I have four buttons 2 up (different distances) and 2 down. Now when I press the button all four set the appropriate number for the pulse count (I did rename from buttonPressCounter to pulseNum). The pulseNum counts down as it should and when it reaches 0, the motor turns off... Just like I want.
Both of my up buttons are working, but neither of the down buttons are working.
The down buttons set the appropriate pulseNum, but it doesnt turn the motor on. I have under its function
int i;
for(i = 0; i <= 255; i= i+10) //counterclockwise rotation
analogWrite(R_PWM, 0);
analogWrite(L_PWM, i);
PulseNum = sensorCount1inch;}
Shouldn't the following turn the motor on counterclockwise?
analogWrite(R_PWM,0);
analogWrite(L_PWM, i);
Here is the complete code:
const int buttonUpQuarter = 8;
const int buttonDownQuarter = 10;
const int buttonUpInch = 11;
const int buttonDownInch = 12;
const int R_IS = 6;
const int R_EN = 2;
const int R_PWM = 3;
const int L_IS = 7;
const int L_EN = 4;
const int L_PWM = 5;
const int irSensor = 9;
long lastDebounceTime = 0;
long debounceDelay = 200;
int buttonState = 0;
int PulseNum = 0;
int lastButtonState = 0;
int reset = 0;
int sensorCountQuarter = 8;
int sensorCount1inch = 32;
int motorOff = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buttonUpQuarter, INPUT_PULLUP);
pinMode(irSensor, INPUT_PULLUP);
pinMode(buttonDownQuarter, INPUT_PULLUP);
pinMode(buttonUpInch, INPUT_PULLUP);
pinMode(buttonDownInch, INPUT_PULLUP);
pinMode(R_IS, OUTPUT);
pinMode(R_EN, OUTPUT);
pinMode(R_PWM, OUTPUT);
pinMode(L_IS, OUTPUT);
pinMode(L_EN, OUTPUT);
pinMode(L_PWM, OUTPUT);
digitalWrite(R_IS, LOW);
digitalWrite(L_IS, LOW);
digitalWrite(R_EN, HIGH);
digitalWrite(L_EN, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(irSensor);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
PulseNum--;
Serial.println("on");
Serial.print("number of IR pulses: ");
Serial.println(PulseNum);
} else {
Serial.println("off");
}
delay(50);
}
lastButtonState = buttonState;
delay(50);
//This makes button press of quarter go up quarter of an inch after 8 signals from IR sensor it shuts off and resets button counter to 0
if (digitalRead(buttonUpQuarter) == LOW) {
int i;
for (i = 0; i <= 255; i = i + 10) //clockwise rotation
analogWrite(R_PWM, i);
analogWrite(L_PWM, 0);
PulseNum = sensorCountQuarter; }
//This makes button press up one inch go up an inch
if(digitalRead(buttonUpInch)==LOW){
int i;
for(i = 0; i <= 255; i= i+10) //clockwise rotation
analogWrite(R_PWM, i);
analogWrite(L_PWM, 0);
PulseNum = sensorCount1inch;}
//This makes button press down one quarter go down one quarter inch
if(digitalRead(buttonDownQuarter)==LOW){
int i;
for(i = 0; i <= 255; i= i+10) //counterclockwise rotation
analogWrite(R_PWM, 0);
analogWrite(L_PWM, i);
PulseNum = sensorCountQuarter;}
//This makes button press down one inch go down one inch
if(digitalRead(buttonDownInch)==LOW){
int i;
for(i = 0; i <= 255; i= i+10) //counterclockwise rotation
analogWrite(R_PWM, 0);
analogWrite(L_PWM, i);
PulseNum = sensorCount1inch;}
if (PulseNum == motorOff) {
analogWrite(R_PWM, 0);
analogWrite(L_PWM, 0);
}
}
neketege88:
Shouldn't the following turn the motor on counterclockwise?
analogWrite(R_PWM,0);
analogWrite(L_PWM, i);
No. This probably will though:
analogWrite(L_PWM, i);
analogWrite(R_PWM,0);
The problem is your for loop. It only loops around the one statement that follows it. If you wanted it to do both of them, you should have done this:
for(i = 0; i <= 255; i= i+10) //counterclockwise rotation
{
analogWrite(R_PWM, 0);
analogWrite(L_PWM, i);
}
Wow... So much to learn....Very impressive how easily you find the stupid mistakes I am making!
Both of those will make it work...... reversing the command line so the = i is at the top of the two statements or adding the { to include both of the statements.
Which is more right? Does it matter
There are several control statements that can work on a single statement or a block e.g. for, while, if. Until you really know what you're doing, it's safer to always use the curly braces to show your intent, even if what's inside them is a single line. Generally, the code that you see here by people posting answers uses this style, even if that's not what they would have done in their own code.
In this particular case, I am not sure that the loop is doing you any good. It appears to be an attempt to ramp up the power gently, but it happens so fast that I suspect that it would be just the same if you just asked for maximum power immediately. Try it and see.
neketege88:
Very impressive how easily you find the mistakes I am making!
Not really - my sister said something similar when I helped her with a college coding assignment. I pointed out that since I was employed as a software developer at the time, it would have been deeply embarrassing if I could not.
The pictures are what I have in mind.... the code seems to be working with leds and I did connect it to the small motor driver chip I mentioned earlier and the small dc motor I have been testing with and it is working.
I really don’t know what R_Is, L_Is, R_En, and L_En that go to the motor driver are or if they are even necessary for my application??
What is the idea of that dangerously unbalanced disc?
I don't remember you telling us what motor driver you're going to use so it's hard to comment on pin assignments.