someone check attached sketch and let me know why I am getting these messages. do I need another void loop and if so how do I do that.
thanks. Telescopeman
sketch_dec06a.ino (4.47 KB)
someone check attached sketch and let me know why I am getting these messages. do I need another void loop and if so how do I do that.
thanks. Telescopeman
sketch_dec06a.ino (4.47 KB)
buttonOneState = digitalRead(buttonOnePin);
All of the code from here on is outside of a function. No wonder you get error messages
Did you end the loop() function too early ?
yes,I think I did. I fixed it all and now it compiles.
Hi,
Is this the circuit you are using?
Also the LEDs are the wrong way around.
Tom... ![]()
Hi,
I have redrawn your circuit, spreading components out.
I am assuming you are using a supply voltage higher than 7v so feed the supply to the Vin or Vraw pin.
I have taken the 5V reg out, as the Mini will provide the 5V for the I2C pullup resistors.
I have added a current limit resistor to each LED and oriented them in the correct direction.
I was using the original circuit which was designed by the Meade inst. co. but I substituted an Arduino for the original PIC 16C54, and I am using a 9VDC battery for power. My bigger problem lies with the sketch that I am using. I shall include the sketch. When I start the system the first press turns on number one led and the motors start up and continue each additional press increases the speed and lights the next led up to the fourth a nd then back to first like I want, but the motors don't stop. In case 1 all stops. I removed the analogWrite statements. I will Put the motor statements at the end of the loop, question is how do I call the mode1state for the second set of switch statements.
hope someone can help
telescopeman
sketch_dec06a.ino (4.49 KB)
Please post your code following the forum guide in the sticky post.
Hi,
This;
pinMode(buttonOnePin, INPUT); //setup pins
pinMode(buttonTwoPin, INPUT);
pinMode(buttonThreePin, INPUT);
pinMode(buttonFourPin, INPUT);
pinMode(mode1Pin, INPUT);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
Should be ;
pinMode(buttonOnePin, INPUT_PULLUP); //setup pins
pinMode(buttonTwoPin, INPUT_PULLUP);
pinMode(buttonThreePin, INPUT_PULLUP);
pinMode(buttonFourPin, INPUT_PULLUP);
pinMode(mode1Pin, INPUT_PULLUP);
Tom... ![]()
I tried to make sense of the post from robin2 about multiple things at the same time. What I would like to do is run the detection routine covered in my last couple of posts in the loop and use void() for the four switches that determine the directions for the mount motors , (N,E,W,S,). Should I list the State conditions in the void loop (). Codes can be downloaded. They are way to long to type out and it would take 3-4 hours to do it. Telescopeman
If you need it for download again let me know.
It’s best you show us what you have at this moment.
In the IDE, use CTRL T to format your code.
Attach your sketch between code tags
[code]Paste your sketch here[/code]
I will send the code as a download link as I cannot type that fast, and it will be full of mistakes.
Only way I can do it.
sketch_dec06a.ino (4.22 KB)
Okay, here is your code cleaned up a bit.
It compiles, no effort was made to see what happens.
Please add useful comments to the code then re-post the update here.
/*
full test
*/
const byte buttonOnePin = 2; //digital input pins
const byte buttonTwoPin = 6; //
const byte buttonThreePin = 4; //
const byte buttonFourPin = 5; //
const byte mode1Pin = 3; //
const byte ledPinOne = 7; // digital output pins
const byte ledPinTwo = 8; //
const byte ledPinThree = 14; //
const byte ledPinFour = 15; //
const byte rmotorPin = 9; //analog output pins
const byte dmotorPin = 11; //
const byte rdirPin = 10; //digital output pins
const byte ddirPin = 12; //
const byte sdaPin = 18; //I2C pins
const byte sclPin = 19; //
byte buttonOneState; //variables
byte lastButtonOneState;
byte buttonTwoState;
byte lastButtonTwoState;
byte buttonThreeState;
byte lastButtonThreeState;
byte buttonFourState;
byte lastButtonFourState;
byte mode1State;
byte val;
int buttonState;
int mode1;
void setup()
{
pinMode(buttonOnePin, INPUT_PULLUP); //setup pins
pinMode(buttonTwoPin, INPUT_PULLUP);
pinMode(buttonThreePin, INPUT_PULLUP);
pinMode(buttonFourPin, INPUT_PULLUP);
pinMode(mode1Pin, INPUT_PULLUP);
pinMode(ledPinOne, OUTPUT);
pinMode(ledPinTwo, OUTPUT);
pinMode(ledPinThree, OUTPUT);
pinMode(ledPinFour, OUTPUT);
pinMode(rmotorPin, OUTPUT);
pinMode(dmotorPin, OUTPUT);
pinMode(rdirPin, OUTPUT);
pinMode(ddirPin, OUTPUT);
}
void loop()
{
val = digitalRead(mode1Pin);
if (val != buttonState && val == LOW)
{
mode1++;
}
buttonState = val;
if (mode1State != mode1)
{
//if(mode1 !=0) {
}
switch (mode1)
{
case 2:
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, LOW);
digitalWrite(ledPinFour, LOW);
break;
case 3:
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, HIGH);
digitalWrite(ledPinFour, LOW);
break;
case 4:
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
digitalWrite(ledPinFour,HIGH);
break;
default:
mode1 = 1;
digitalWrite(ledPinOne, HIGH);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
digitalWrite(ledPinFour, LOW);
break;
}
readButtonPins();
} //END of loop()
void readButtonPins()
{
buttonOneState = digitalRead(buttonOnePin);
buttonTwoState = digitalRead(buttonTwoPin);
buttonThreeState = digitalRead(buttonThreePin);
buttonFourState = digitalRead(buttonFourPin);
//compare the button state to the previous state.
if (buttonOneState != lastButtonOneState)
{
if (buttonOneState == LOW)
{
analogWrite(rmotorPin, mode1);
digitalWrite(rdirPin, LOW);
}
else
{
analogWrite(rmotorPin, 0);
digitalWrite(rdirPin, LOW);
}
}
if (buttonTwoState != lastButtonTwoState)
{
if (buttonTwoState == LOW)
{
analogWrite(dmotorPin, mode1);
digitalWrite(ddirPin, LOW);
}
else
{
analogWrite(dmotorPin, 0);
digitalWrite(ddirPin, LOW);
}
}
if (buttonThreeState != lastButtonThreeState)
{
if (buttonThreeState == LOW)
{
analogWrite(rmotorPin, mode1);
digitalWrite(rdirPin, HIGH);
}
else
{
analogWrite(rmotorPin, 0);
digitalWrite(rdirPin, LOW);
}
}
if (buttonFourState != lastButtonFourState)
{
if (buttonFourState == LOW)
{
analogWrite(dmotorPin, mode1);
digitalWrite(ddirPin, HIGH);
}
else
{
analogWrite(dmotorPin, 0);
digitalWrite(ddirPin, LOW);
}
}
switch (mode1)
{
case 2:
analogWrite(rmotorPin, 150);
analogWrite(dmotorPin, 100);
break;
case 3:
analogWrite(rmotorPin, 200);
analogWrite(dmotorPin, 125);
break;
case 4:
analogWrite(rmotorPin, 250);
analogWrite(dmotorPin, 150);
break;
default :
mode1 = 1;
analogWrite(rmotorPin, 100);
analogWrite(dmotorPin, 60);
break;
}
delay(50);
lastButtonOneState = buttonOneState ;
lastButtonTwoState = buttonTwoState;
lastButtonThreeState = buttonThreeState;
lastButtonFourState = buttonFourState;
} //END of readButtonPins()
Let's get some better names for these:
buttonOnePin
buttonTwoPin
buttonThreePin
buttonFourPin
mode1Pin
ledPinOne
ledPinTwo
ledPinThree
ledPinFour
buttonOneState
lastButtonOneState
buttonTwoState
lastButtonTwoState
buttonThreeState
lastButtonThreeState
buttonFourState
lastButtonFourState
mode1State
val
buttonState
mode1
OK, I will give it a try tomorrow on my system. I will let you know how it works.
thanks:telescopeman
When it comes to switches, they do not need to be read any faster than 50ms.
I place non blocking, timing code, in your sketch to scan your switches at this interval.
There still is more work to be done as I mentioned in post #32.
You can easily use the, Find/Replace All, feature to make your variable name changes.
/*
full test
*/
const byte buttonOnePin = 2; //digital input pins
const byte mode1Pin = 3; //
const byte buttonThreePin = 4; //
const byte buttonFourPin = 5; //
const byte buttonTwoPin = 6; //
const byte ledPinOne = 7; // digital output pins
const byte ledPinTwo = 8; //
const byte ledPinThree = 14; //
const byte ledPinFour = 15; //
const byte rmotorPin = 9; //analog output pins
const byte dmotorPin = 11; //
const byte rdirPin = 10; //digital output pins
const byte ddirPin = 12; //
const byte sdaPin = 18; //I2C pins
const byte sclPin = 19; //
byte buttonOneState; //variables
byte lastButtonOneState;
byte buttonTwoState;
byte lastButtonTwoState;
byte buttonThreeState;
byte lastButtonThreeState;
byte buttonFourState;
byte lastButtonFourState;
byte mode1State;
byte val;
int buttonState;
int mode1;
unsigned long currentMillis;
unsigned long lastMillis;
//********************************************************************************************
void setup()
{
pinMode(buttonOnePin, INPUT_PULLUP); //setup pins
pinMode(buttonTwoPin, INPUT_PULLUP);
pinMode(buttonThreePin, INPUT_PULLUP);
pinMode(buttonFourPin, INPUT_PULLUP);
pinMode(mode1Pin, INPUT_PULLUP);
pinMode(ledPinOne, OUTPUT);
pinMode(ledPinTwo, OUTPUT);
pinMode(ledPinThree, OUTPUT);
pinMode(ledPinFour, OUTPUT);
pinMode(rmotorPin, OUTPUT);
pinMode(dmotorPin, OUTPUT);
pinMode(rdirPin, OUTPUT);
pinMode(ddirPin, OUTPUT);
}
//********************************************************************************************
void loop()
{
//capture the current time
currentMillis = millis();
//Put non blocking code here
//is it time to read the switches?
if (currentMillis - lastMillis >= 50) //every 50ms
{
//update the time
lastMillis = currentMillis;
readMode1Pin();
readButtonPins();
}
} //END of loop()
//********************************************************************************************
void readMode1Pin()
{
val = digitalRead(mode1Pin);
//
if (val != buttonState)
{
//update to the new state
buttonState = val;
//was the switch just pressed?
if (val == LOW)
{
mode1++;
}
}
//
if (mode1State != mode1)
{
//if(mode1 !=0) {
}
//
switch (mode1)
{
//
case 2:
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, LOW);
digitalWrite(ledPinFour, LOW);
break;
//
case 3:
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, HIGH);
digitalWrite(ledPinFour, LOW);
break;
//
case 4:
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
digitalWrite(ledPinFour, HIGH);
break;
//
default:
mode1 = 1;
digitalWrite(ledPinOne, HIGH);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
digitalWrite(ledPinFour, LOW);
break;
} //END of switch (mode1)
} //END of readMode1Pin()
//********************************************************************************************
void readButtonPins()
{
//
buttonOneState = digitalRead(buttonOnePin);
buttonTwoState = digitalRead(buttonTwoPin);
buttonThreeState = digitalRead(buttonThreePin);
buttonFourState = digitalRead(buttonFourPin);
//
if (buttonOneState != lastButtonOneState)
{
//update to the new state
lastButtonOneState = buttonOneState;
//
if (buttonOneState == LOW)
{
analogWrite(rmotorPin, mode1);
digitalWrite(rdirPin, LOW);
}
else
{
analogWrite(rmotorPin, 0);
digitalWrite(rdirPin, LOW);
}
}
//
if (buttonTwoState != lastButtonTwoState)
{
//update to the new state
lastButtonTwoState = buttonTwoState;
//
if (buttonTwoState == LOW)
{
analogWrite(dmotorPin, mode1);
digitalWrite(ddirPin, LOW);
}
else
{
analogWrite(dmotorPin, 0);
digitalWrite(ddirPin, LOW);
}
}
//
if (buttonThreeState != lastButtonThreeState)
{
//update to the new state
lastButtonThreeState = buttonThreeState;
//
if (buttonThreeState == LOW)
{
analogWrite(rmotorPin, mode1);
digitalWrite(rdirPin, HIGH);
}
else
{
analogWrite(rmotorPin, 0);
digitalWrite(rdirPin, LOW);
}
}
//
if (buttonFourState != lastButtonFourState)
{
//update to the new state
lastButtonFourState = buttonFourState;
if (buttonFourState == LOW)
{
analogWrite(dmotorPin, mode1);
digitalWrite(ddirPin, HIGH);
}
else
{
analogWrite(dmotorPin, 0);
digitalWrite(ddirPin, LOW);
}
}
//
switch (mode1)
{
//
case 2:
analogWrite(rmotorPin, 150);
analogWrite(dmotorPin, 100);
break;
//
case 3:
analogWrite(rmotorPin, 200);
analogWrite(dmotorPin, 125);
break;
//
case 4:
analogWrite(rmotorPin, 250);
analogWrite(dmotorPin, 150);
break;
//
default :
mode1 = 1;
analogWrite(rmotorPin, 100);
analogWrite(dmotorPin, 60);
break;
} //END of switch (mode1)
} //END of readButtonPins()
//********************************************************************************************
I gave the new version of the code a try but alas the motors will not stop turning, I can reverse the direction with the directional switches, but when I release the button it goes back to the original direction. They also respond to the speed switch. The problem seams to be tied to the speed switch,(mode1). any other ideas to separate the motors from the (mode1).
Telescopeman
As mentioned, your code was just cleaned up a bit, not run to confirm functionality.
We need to check every part of the sketch one section at a time.
The first thing needed is to get some variable names changed, we need to understand and follow what is happening.
Adding comments to your code is a must when you expect others to offer help.
We will help, but we need to be able to understand what the code is attempting to do.
Waiting for your changes. ![]()
Some of the variable names I still prefer t use but I have a new post in download form.
sketch_dec08a.ino (4.74 KB)
Reply #12, housekeeping point one.
What does val stand for?