Go Down

Topic: cant find sample sketch to help (Read 1 time) previous topic - next topic

40YearOldNoob

as you can tell by the screen name, you know who/what I am. I am looking for a sketch similar to an idea I have, with no luck. Guess it cuz i cant think of the wording to describe what it is i want to do. I have played around with relays and led controls which aren't that different. As well as copied and pasted the "Hello World" script into my Uno r3 16x2 LCD. Meh, what can i say fun? cute? a day spent? did i learn? Yeah... :smiley-neutral:

so with that being said,
this is what i have,

http://www.microcenter.com/product/435326/DIY_Developer_Kit

and the

http://www.microcenter.com/product/437823/8_channel_Signal_Relay_Module_Board


my goal (with your help, patience and understanding) is to....
have one button cycle pairs of relays, have them on stand by, waiting for them to be triggered by a second button when pressed.

this is what i would like to happen
hardware configuration via arduino.. (to minimize additional wiring and hardware)

pressing button number 1 selects 1 of these these groups of relays (in succession) but is momentarily triggered by button number 2


1-turn on arduino
  not one pair of relays is waiting to be or can be triggered by button number 2. until....
2-press button number 1, one time selects relays 1 and 2, that are waiting to be triggered by  button number two
3-press button number 1 again, selects relays 2 and 3, that are waiting to be triggered by  button number two.
4-press button number 1 again, selects relays 4 and 5, that are waiting to be triggered by  button number two.
5-press button number 1 again, selects relays 5 and 6, that are waiting to be triggered by  button number two.
6-press button number 1 again, selects relays 7 and 8, that are waiting to be triggered by  button number two.
7-press button number 1 again to return to relay relays 2 and 3, that are waiting to be triggered by  button number two. in which the cycle continues never ending. until the arduino is turned off.

I understand that this can be done without the arduino. I have done it. its time consuming to wire and reproduce for my friends. In addition, i would like to add other bells and whistles. Until then this is the foundation of the project.



As my screen name states.... so please no ego, do your best to stay on topic, and most of all please help me learn.

thanks in advance.
the 40 Year Old Noob.
lol


robtillaart

The most important thing you should learn is the use of array's. I wrote a sketch that does some sort what you needed.

Code: (might include typos etc) [Select]

int relay[8] = { 4,5,6,7,  8,9,10,11 };  // pin numbers of the relays.
int button1 = 2;
int button2 = 3;

int relayIndex = 0;
int active = -1;

void setup()
{
  Serial.begin(9600);
  Serial.print("Start: ");
  Serial.println(__FILE__);  // to remember which sketch is loaded.

  for (int i=0; i<8; i++) pinMode(relay[i], OUTPUT);    // an array of 8 elements uses index 0..7
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(button1) == LOW)
  {
    relayIndex = relayIndex + 1;
    if (relayIndex > 7) relayIndex  = 0;
    Serial.println("relayIndex changed to: ");
    Serial.println(relayIndex);
  }
  if (digitalRead(button2) == LOW)
  {
    Serial.println("switch off relay: ");
    Serial.println(active);
    digitalWrite(active, LOW);

    Serial.println("switch on relay: ");
    Serial.println(active);   activeRelay = relayIndex;
    digitalWrite(active, HIGH);
  }
}


The print statements show what happens, sort of,

give it a try
 

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

40YearOldNoob

thanks robtillaart, i'll let you know what happens. busy so sometime next week.

40YearOldNoob

thanks for the sketch

when i first ran it, it threw me for a void loop() {const int punIntended = INPUT}. I understand your intentions were for me to get familiar with "arrays". And it did! Since I am still learning, I have decided to go with a different type of relay making my own relay board. Which will compensate for my lack of knowledge in code.
So here is the sketch with each line commented. Once again thanks for your help!



/*modified from sketch provided by robtillaart via arduino forum.
*/
 
const int relay[5] = {6, 7, 8, 9, 10};        // PWM pin numbers of the relays.
const int button1 = A0;                       // pin number of button 1.
const int button2 = A1;                       // pin number of button 2.
int relayIndex = 6;                           // incremental increase PWM starting point.
int active = -1;                              // variable used to insert -1 into sketch.

void setup()
{
 Serial.begin(9600);                         //speed of serial bus.
 Serial.print("Start: ");                    //start serial bus.
 Serial.println(__FILE__);                   // display sketch name at the start of serial bus monitor.

 for (int i=0; i<6; i++) pinMode(relay, OUTPUT);// an array of 5 elements uses index 0..5 which is set to PWM's 6,7,8,9,10, to be set as an output.
 pinMode(button1, INPUT);                    // pin A0 sees 0v dc untill button is press sending 5 v to pin A0.
 pinMode(button2, INPUT);                    // pin A1 sees 0v dc untill button is press sending 5 v to pin A1.
}

void loop()
{

 if (digitalRead(button1) == HIGH)            // if pin 2 reads pressed seeing 5v dc at A0.
 {
   relayIndex = relayIndex + 1;               // add 1 to the index of relays everytime button 1 is pressed.
   delay(350);                                // puts a deley of 250ms so that it doesn't increment so fast when the button is pressed.
   if (relayIndex >10) relayIndex  = 6;       // if the button is pressed more than 5 times return to PWM pin 6 of the array.
   Serial.println("LED changed to: ");        //sends incremental relay selection to sreial monitor.
   Serial.println(relayIndex);                // prints incremental relay selection to sreial monitor.
 }

 
 if (digitalRead(button2) == HIGH)            //if button2 is pressed
 {
   Serial.println("LED ON OOO:  ");           //prints "LED ON OOO: " to serial monitor.
   Serial.println(relayIndex);                //prints "led index number " to serial monitor.
   digitalWrite(relayIndex, HIGH);            // gives 5v dc to selected relay index number.
 }
 else {
   // turn LED off:
   Serial.println("LED OFF ---: ");           //prints "LED OFF ---: " to serial monitor.
   Serial.println(relayIndex);                //prints "led index number " to serial monitor.
   digitalWrite(relayIndex, LOW);             // gives 0v dc to selected relay index number.
 }  
 }

Go Up