Hi
in my future project i want to be able to move something with the help of a motor and belt
i will have 4 prefix positions that the object can stop and same number of push buttons
The tricky part in all this is that i want the program be able to store push buttons calls until the action is finished
as an example if the object is in position ''1'' and i press button ''2'' that will send it to ''2'' position and at the same time press button ''4'' the program can be able to send it to position No 2 and after some seconds send it to position No4
Question :
Have to use the EEPROM to store the calls or maybe in an external memory
larryd:
Why not store the sequence in an array.
Step trough the array elements in sequence and do the appropriate operations.
I was thinking along those lines too, perhaps with a "Go" button*. Then you can hit the buttons in whatever sequence, they get arrayed in that sequence, and when you're done sequencing you hit "Go". Maybe have a "Clear" button so you get a do-over if you mess up the sequence. An LCD would be very useful here so you can see the sequence before you commit.
a non-shirt, switch type button, for those who insist buttons only belong on shirts.
/*
Zacharias Antony
control panel with arduino Nano 328 or pro mini 328 5v 8mhz
- There are 3 prefix-positions and buttons are placed so coresponding in each position
- Arduino master reads arduino slave for car position
- Byte 'F' of ic2 = 0,1,2 car positions
- Byte 'T' of ic2 where 'M' = stop sensor
*/
#include <Wire.h>
byte callbutton1 = 4;//push button in the position
byte callbutton2 = 3;//push button in the position
byte callbutton3 = 7;//push button in the position
byte relayRight = 12;//the relay that drives right the car
byte relayLeft = 10; //the relay that drives left the car
byte slowDownVVF = 5; // Pin to drive vvf voltage via transistor (slow speed)
byte electromagnet = 6; //the relay that drives up the electromagnet of doorslocks
// variables will change:
int callcar1 = 0; // variable for reading the pushbutton status of the positions
int callcar2 = 0; // variable for reading the pushbutton status of the positions
int callcar3 = 0; // variable for reading the pushbutton status of the positions
/////////////////////////////////////////////////////////////////// SETUP ////////////////////////////////////////////////////////////////
// the setup routine runs once when you press reset:
void setup()
{
// Start the I2C Bus as Master
Wire.begin();
// initialize serial communication:
Serial.begin(9600);
pinMode(callbutton1, INPUT_PULLUP);
pinMode(callbutton2, INPUT_PULLUP);
pinMode(callbutton3, INPUT_PULLUP);
// initialize the digital pins outputs.
pinMode(relayRight, OUTPUT);
pinMode(relayLeft, OUTPUT);
pinMode(slowDownVVF, OUTPUT);
pinMode(electromagnet, OUTPUT);
}
/////////////////////////////////////////////////////////////// LOOP //////////////////////////////////////////////////////////////////////////
// the loop routine runs over and over again forever:
void loop()
{
Wire.requestFrom(2, 2); // request 1 bytes from slave device #2
// Wire.requestFrom(5, 1); // request 1 bytes from slave device #2
while (Wire.available()) { // slave may send less than requested
byte F = Wire.read();
Serial.println(F);
byte T = Wire.read();
if (T == 'M') {
Serial.println("Stop floor active");
}
}
// read the state of the pushbuttons value:
callcar1 = digitalRead(callbutton1);
callcar2 = digitalRead(callbutton2);
callcar3 = digitalRead(callbutton3);
//start by checking the 3 call buttons.
//we read the input and if we have signal we wait for the release before starting.
//after the release a pause of a second is added before beggining the motion of the car
if (callcar1 == LOW)// if the position 1 call button is pressed
{
delay(100);//software debounce of the button
while (callcar1 == LOW)
{
callcar1 = digitalRead(callbutton1);//check the value of the button again
delay(10);//keep running smoothly
}//wait for the relese of the button to continue
//delay(1000);
position1function();//go to the position 1 fuction of the car
}
else if (callcar2 == LOW) // if the position 2 call button is pressed
{
delay(100); //software debounce of the button
while (callcar2 == LOW)
{
callcar2 = digitalRead(callbutton2);//check the value of the button again
delay(10);//keep running smoothly
}//wait for the relese of the button to continue
//delay(1000);
position2function();//go to the position 2 fuction of the car
}
else if (callcar3 == LOW) // if the position 1 call button is pressed
{
delay(100); //software debounce of the button
while (callcar3 == LOW)
{
callcar3 = digitalRead(callbutton3);//check the value of the button again
delay(10);//keep running smoothly
}//wait for the relese of the button to continue
//delay(1000);
position3function();//go to the position 3 fuction of the car
}
digitalWrite(relayRight, LOW); //keep the relay for right motion inactive
digitalWrite(relayLeft, LOW); //keep the relay for left motion inactive
digitalWrite(slowDownVVF, LOW); //keep the VVF running with high speed quite
digitalWrite(electromagnet, LOW); //keep the relay for electromagnet inactive
delay(10);//a few delay just to keep running smoothly
}
at first it was based in ''while'' for store call but just for one call only..
but as with ''while'' had problems i decide to leave this approach.
i want to push the one button and if an other button is pushed also.. saved in memory or with an other method and be able to make the new movement after finish the first movement
sorry but had error message for exceed 9000 characters limitation so had to brake the code to 2 parts / posts
Part B
/////////////////////////////////////////////////////////////// CALL 1 //////////////////////////////////////////////////////////////////////////
void position1function()
{
Wire.requestFrom(2, 2); // request 2 bytes from slave device #2
while (Wire.available()) { // slave may send less than requested
byte F = Wire.read();
byte T = Wire.read();
Serial.println(F); // print message to monitor
if (F != 0) // check the proximity sensor of the first position to see if the car is there
{ //if the car isn't there call it
// while ( F != 0) //check that the proximity of the first position is low
//the motor begins and stops when the appropriate position proximity give the signal
// {
Serial.println("call for 1 position");
digitalWrite(electromagnet, HIGH);//start the electromagnet
digitalWrite(relayLeft, HIGH);//drive the car left
if (F == 0 ) {
digitalWrite(slowDownVVF, HIGH);
Serial.println("slow speed");
}
if (F == 0 && T == 'M') {
digitalWrite(relayLeft, LOW);
digitalWrite(electromagnet, LOW);//stop the electromagnet
Serial.println("Stoped - on position");
}
delay(10);//a few delay to keep running the routine smoothly
// }
}
}
}
/////////////////////////////////////////////////////////////// CALL 2 //////////////////////////////////////////////////////////////////////////
void position2function()
{
Wire.requestFrom(2, 2); // request 2 bytes from slave device #2
while (Wire.available()) { // slave may send less than requested
byte F = Wire.read();
byte T = Wire.read();
//Serial.println(F); // print message to monitor
if (F > 2) // check the proximity sensor of the third position to see if the car is there
{ //if the car is there call it
// while ( F != 2) //the motor begins and stops when the appropriate position proximity give the signal
// {
// byte F = Wire.read();
// byte T = Wire.read();
Serial.println("call for 2 position");
digitalWrite(electromagnet, HIGH);//start the electromagnet
digitalWrite(relayLeft, HIGH);//drive the car left
Serial.println("Down Move");
if (F == 2 ) {
digitalWrite(slowDownVVF, HIGH);
Serial.println("slow speed");
}
if (F == 2 && T == 'M') {
digitalWrite(relayLeft, LOW);
digitalWrite(electromagnet, LOW);//stop the electromagnet
Serial.println("Stoped - on position");
delay(10);//a few delay to keep running the routine smoothly
// }
}
}
else if (F <= 0) // check the proximity sensor of the first position to see if the car is there
{ //if the car is there call it
// while ( F != 2) //the motor begins and stops when the appropriate position proximity give the signal
// {
// byte F = Wire.read();
// byte T = Wire.read();
Serial.println("call for 2 position");
digitalWrite(electromagnet, HIGH);//start the electromagnet
digitalWrite(relayRight, HIGH);//drive the car left
Serial.println("Up Move");
if (F == 2 ) {
digitalWrite(slowDownVVF, HIGH);
Serial.println("slow speed");
}
if (F == 2 && T == 'M') {
digitalWrite(relayRight, LOW);
digitalWrite(electromagnet, LOW);//stop the electromagnet
Serial.println("Stoped - on position");
delay(10);//a few delay to keep running the routine smoothly
}
// }
}
}
}
/////////////////////////////////////////////////////////////// CALL 3 //////////////////////////////////////////////////////////////////////////
void position3function()
{
Wire.requestFrom(2, 2); // request 4 bytes from slave device #5
while (Wire.available()) { // slave may send less than requested
byte F = Wire.read();
byte T = Wire.read();
Serial.println(F); // print message to monitor
if (F < 3) // check the proximity sensor of the first and second position to see if the car is there
{ //if the car is there call it
//while ( F != 3) //the motor begins and stops when the appropriate position proximity give the signal
// {
// while (Wire.available()) {
// byte F = Wire.read();
// byte T = Wire.read();
Serial.println("call for 3 position");
digitalWrite(electromagnet, HIGH);//start the electromagnet
digitalWrite(relayRight, HIGH);//drive the car left
if (F == 3 ) {
digitalWrite(slowDownVVF, HIGH);
Serial.println("slow speed");
}
if (F == 3 && T == 'M') {
digitalWrite(relayRight, LOW);
digitalWrite(electromagnet, LOW);//stop the electromagnet
Serial.println("Stoped - on position");
delay(10);//a few delay to keep running the routine smoothly
// }
}
}
}
}
/////////////////////////////////////////////////////////////// END //////////////////////////////////////////////////////////////////////////
Create an array of size 'x'. Initialize all the array elements with some number which will never naturally occur, say '99'.
Initialize an array index variable to point to the first array element.
When a button is pressed store its number (or any special number you like) in the array at the position dictated by the index variable then increment the index variable. When another button is pressed, store and increment again.
Some other code will be looking at the first array position. If it sees a '99' it does nothing. When it sees a valid number it does the process 'y' associated with that number. When process 'y' is finished the first array element is destroyed by moving all higher-indexed elements one step to the next lower element:
a[0] <-- a[1], a[1] <-- a[2], etc. Decrement the index variable.
Put an invalid value in the now-vacated last array position.
Try this: You are a courier. You have an assignment to retrieve a package from someone who lives on Elm street. Great! There are fifty houses on Elm street, to which should you go? Well, your dispatcher will instruct you "Pick up the package from address 37 on Elm street." Now you know exactly where to go. You will not go to Arch street, or Sandy Boulevard, you will go to only Elm street. So your destination might look like this:
pickup = ElmStreet[50]; // get the contents of the address
/*
When you have completed this the dispatcher may hand you a package and say deliver this to the person at 8 Elm Street.
*/
ElmStreet[8] = delivery; // put a value at the specified address
The power of this is in the ability to modify the subscript, the part in the square brackets [ x ], especially within a loop.
larryd:
Of course, we have to point out there maybe problems on Elm street!
Since Freddy's only been to 1 thru 9 we'll institute bounds checking to avoid those addresses. If he keeps encroaching we'll declare Elm St. totally corrupted and reconstruct the data on Primrose Lane. ;D