Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Adding a second button(input)? on: March 03, 2013, 03:18:36 pm
Could you show me an example of what the code would look like for a state machine?
2  Using Arduino / Programming Questions / Adding a second button(input)? on: March 03, 2013, 02:04:14 pm
I didn't get much luck the last time i asked for help maybe i wasn't clear enuff last time i don't know so i will try to explain it in a diffident way and i need it explained to me as simple as possible because i'm very new to all of this and I DON'T KNOW EVERYTHING : P haha. So this is my code it control's an RGB led with one push button that go's throw 71 case functions(cut down to 5 to fit on the page)
Code:
const int redledPin = 2;
const int greenledPin = 1;
const int blueledPin = 0;
const int buttonPin = 3;
int RGB=1;

int prevUp = HIGH;
int prevDn = HIGH;

void setup ()
{
  pinMode (redledPin, OUTPUT);
  pinMode (greenledPin, OUTPUT);
  pinMode (blueledPin, OUTPUT);
  pinMode (buttonPin, INPUT);
  }

void loop ()
{
int currUp = digitalRead(buttonPin);

   if(currUp != prevUp)
   {
      delay(25);

      currUp = digitalRead(buttonPin);

      if(currUp != prevUp)
      {
         if(currUp == LOW)
            RGB++;
      }
   }
  
   switch (RGB){
    case 1:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(200);
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
    
    case 2:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    break;    
    
    case 3:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    break;
    
    case 4:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    delay(600);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(300);
    break;
        
   }
    prevUp = currUp;
}
it works fine but its got to many functions to go throw i would like to add another button to make things a bit more user friendly but i have know idea where to start. What i would like the buttons(button 1 = B1 & button 2 = B2) to do pressing B1 will go throw the different colours available so every time you press it go's to the next red, blue, green ect.. and soon as youv found the colour you want pressing B2 will select the flash pattern and different flashing effects ect.. could someone show me how i would be able to Incorporate this into my code?
3  Using Arduino / Programming Questions / Re: A switch case within a switch case. on: March 03, 2013, 01:14:43 pm
Could someone show me an example of what i could do to fix my code i'm really new to all of this and i'm struggling : /
4  Using Arduino / Programming Questions / Re: A switch case within a switch case. on: March 02, 2013, 01:47:34 pm
I'm shore i didn't mean to but like i said i'm a little bit out of my depth here so not really shore anymore haha.
5  Using Arduino / Programming Questions / Re: A switch case within a switch case. on: March 02, 2013, 01:31:59 pm
Ok will try that dude o and the pins are set to 0,1,2 etc.. because i'm using an ATtiny85.
6  Using Arduino / Programming Questions / Re: A switch case within a switch case. on: March 02, 2013, 01:13:50 pm
There we go : )
Code:
const int redledPin = 2;
const int greenledPin = 1;
const int blueledPin = 0;
const int buttonPin = 3;
const int buttonPin2 = 4;
int RGB=1;
int B2=2;

int prevUp = HIGH;
int prevDn = HIGH;

void setup ()
{
  pinMode (redledPin, OUTPUT);
  pinMode (greenledPin, OUTPUT);
  pinMode (blueledPin, OUTPUT);
  pinMode (buttonPin, INPUT);
}

void loop ()
{
  int currUp = digitalRead(buttonPin2);

  if(currUp != prevUp)
  {
    delay(5);

    currUp = digitalRead(buttonPin2);

    if(currUp != prevUp)
    {
      if(currUp == LOW)
        B2++;
    }
  }

  switch (B2)
  case 1:
  {
    {
      int currUp = digitalRead(buttonPin);

      if(currUp != prevUp)
      {
        delay(5);

        currUp = digitalRead(buttonPin);

        if(currUp != prevUp)
        {
          if(currUp == LOW)
            RGB++;
        }
      }

      switch (RGB){
      case 1:
        analogWrite(redledPin, 0);
        analogWrite(blueledPin, 250);
        analogWrite(greenledPin, 0);
        delay(200);
        analogWrite(redledPin, 0);
        analogWrite(blueledPin, 250);
        analogWrite(greenledPin, 0);
        delay(100);
        break;

      case 2:
        analogWrite(redledPin, 0);
        analogWrite(blueledPin, 250);
        analogWrite(greenledPin, 0);
        break;

      }
      prevUp = currUp;
    }
    break;

  case 2:
    {
      int currUp = digitalRead(buttonPin);

      if(currUp != prevUp)
      {
        delay(5);

        currUp = digitalRead(buttonPin);

        if(currUp != prevUp)
        {
          if(currUp == LOW)
            RGB++;
        }
      }

      switch (RGB){
      case 1:
        analogWrite(redledPin, 0);
        analogWrite(blueledPin, 0);
        analogWrite(greenledPin, 250);
        delay(200);
        analogWrite(redledPin, 0);
        analogWrite(blueledPin, 0);
        analogWrite(greenledPin, 0);
        delay(100);
        break;

      case 2:
        analogWrite(redledPin, 0);
        analogWrite(blueledPin, 0);
        analogWrite(greenledPin, 250);

      }
      prevUp = currUp;
    }
    break;
  }
}
7  Using Arduino / Programming Questions / Re: A switch case within a switch case. on: March 02, 2013, 01:09:05 pm
I made a code for just one button that worked grate but it had to many things to cycle throw so thought adding the exstar button would be easy i just well take a look its not pretty and last time i posted it up i got mugged off about it P.s don't ask what i thought i was doing i have know idea haha.
Code:
const int redledPin = 2;
const int greenledPin = 1;
const int blueledPin = 0;
const int buttonPin = 3;
const int buttonPin2 = 4;
int RGB=1;
int B2=2;

int prevUp = HIGH;
int prevDn = HIGH;

void setup ()
{
  pinMode (redledPin, OUTPUT);
  pinMode (greenledPin, OUTPUT);
  pinMode (blueledPin, OUTPUT);
  pinMode (buttonPin, INPUT);
  }

void loop ()
{
int currUp = digitalRead(buttonPin2);

   if(currUp != prevUp)
   {
      delay(5);

      currUp = digitalRead(buttonPin2);

      if(currUp != prevUp)
      {
         if(currUp == LOW)
            B2++;
      }
   }
  
   switch (B2)
     case 1:{
  {
int currUp = digitalRead(buttonPin);

   if(currUp != prevUp)
   {
      delay(5);

      currUp = digitalRead(buttonPin);

      if(currUp != prevUp)
      {
         if(currUp == LOW)
            RGB++;
      }
   }
  
   switch (RGB){
    case 1:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
    
    case 2:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    break;
  
   }
    prevUp = currUp;
}
break;

  case 2:{
int currUp = digitalRead(buttonPin);

   if(currUp != prevUp)
   {
      delay(5);

      currUp = digitalRead(buttonPin);

      if(currUp != prevUp)
      {
         if(currUp == LOW)
            RGB++;
      }
   }
  
   switch (RGB){
    case 1:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
    
    case 2:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    
   }
    prevUp = currUp;
  }break;
   }
 }
8  Using Arduino / Programming Questions / A switch case within a switch case. on: March 02, 2013, 12:51:05 pm
I'v gone a little of track and just confused myself could someone show me an example of what a switch case within a switch case would look like. What i'm doing is simple or well should be i'm stuck on the code for what i'm doing the code is running an RGB led and the controls are two push buttons so every you press the first button it go's to the next colour and every time you press the second button it go's to a different flashing mode. Anyone?
9  Using Arduino / Programming Questions / Re: Can't get this switch / case sketch to work. Any ideas? on: February 28, 2013, 11:07:41 am
Ok so i scraped the first code redid a lot of it and came up with this layout still not working but i think its in beter shape than the first
Code:
const int redledPin = 2;
const int greenledPin = 1;
const int blueledPin = 0;
const int buttonPin = 3;
const int buttonPin2 = 4;
int RGB=1;
int RGB2=2;

int prevUp = HIGH;
int prevDn = HIGH;

void setup ()
{
  pinMode (redledPin, OUTPUT);
  pinMode (greenledPin, OUTPUT);
  pinMode (blueledPin, OUTPUT);
  pinMode (buttonPin, INPUT);
  pinMode (buttonPin2, INPUT);
  }

void loop ()
{
int currUp = digitalRead(buttonPin && buttonPin2);

   if(currUp != prevUp)
   {
      delay(25);

      currUp = digitalRead(buttonPin && buttonPin2);


      if(currUp != prevUp)
      {
         if(currUp == LOW)
            RGB++ && RGB2;

      }
   }
   
  switch (RGB){
    case 1:
   switch (RGB2){
    case 1:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    break;
     
    case 2:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    break;   
   
    case 3:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    break;
   
    case 4:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    break;
   
    case 5:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    break;
  }
    prevUp = currUp;
   
    case 2:
    switch (RGB2){
    case 1:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    break;
     
    case 2:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    break;   
   
    case 3:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    break;
   
    case 4:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    break;
   
    case 5:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    break;
  }
    prevUp = currUp;
  }
}
10  Using Arduino / Programming Questions / Re: Can't get this switch / case sketch to work. Any ideas? on: February 27, 2013, 07:25:06 pm
Still no joy : / thanks for the help anyway guys : ) is there an easier way to achieve a switch case within a switch case only using two inputs?
11  Using Arduino / Programming Questions / Can't get this switch / case sketch to work. Any ideas? on: February 27, 2013, 04:42:55 pm
The program was suppose to run an RGB led with a lot of different functions off of one button but it was to many to cycle throw so i thought i could add a second button to brake down the functions making it easier to use so one button cycle the colours whilst the other dealt with the colours functions but iv made a but of a mess any help would be greatly appreciated. A small sample of the code
Code:
const int redledPin = 2;
const int greenledPin = 1;
const int blueledPin = 0;
const int buttonPin = 3;
const int buttonPin2 = 4;
int RGB=1;
int B2=2;

int prevUp = HIGH;
int prevDn = HIGH;

void setup ()
{
  pinMode (redledPin, OUTPUT);
  pinMode (greenledPin, OUTPUT);
  pinMode (blueledPin, OUTPUT);
  pinMode (buttonPin, INPUT);
  }

void loop ()
{
int currUp = digitalRead(buttonPin2);

   if(currUp != prevUp)
   {
      delay(5);

      currUp = digitalRead(buttonPin2);

      if(currUp != prevUp)
      {
         if(currUp == LOW)
            B2++;
      }
   }
 
   switch (B2)
     case 1:{
  {
int currUp = digitalRead(buttonPin);

   if(currUp != prevUp)
   {
      delay(5);

      currUp = digitalRead(buttonPin);

      if(currUp != prevUp)
      {
         if(currUp == LOW)
            RGB++;
      }
   }
 
   switch (RGB){
    case 1:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 2:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    break;
   
   }
    prevUp = currUp;
}
break;

  case 2:{
int currUp = digitalRead(buttonPin);

   if(currUp != prevUp)
   {
      delay(5);

      currUp = digitalRead(buttonPin);

      if(currUp != prevUp)
      {
         if(currUp == LOW)
            RGB++;
      }
   }
 
   switch (RGB){
    case 1:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 2:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
   
   }
    prevUp = currUp;
  }break;
   }
 }
12  Using Arduino / Programming Questions / Re: Need help coding a debounce on: February 18, 2013, 08:44:59 pm
Don't play yourself down dude i know a savant when i see one : D
13  Using Arduino / Programming Questions / Re: Need help coding a debounce on: February 18, 2013, 05:31:24 pm
Half right?  smiley-confuse
14  Using Arduino / Programming Questions / Re: Need help coding a debounce on: February 18, 2013, 05:20:40 pm
Much better you sir are a savant : )
15  Using Arduino / Programming Questions / Need help coding a debounce on: February 18, 2013, 04:14:22 pm
Anyone help me debounce this code had a few go's be no joy : /
Code:
const int redledPin = 2;
const int greenledPin = 1;
const int blueledPin = 0;
const int buttonPin = 3;
int RGB=1;

int prevUp = HIGH;
int prevDn = HIGH;

void setup ()
{
  pinMode (redledPin, OUTPUT);
  pinMode (greenledPin, OUTPUT);
  pinMode (blueledPin, OUTPUT);
  pinMode (buttonPin1, INPUT);
  }

void loop ()
{
{
   int currUp = digitalRead(buttonPin);
   if(currUp != prevUp)
   {
      if(currUp == LOW)
         RGB++;
   }
   prevUp = currUp;

}
 
   switch (RGB){
    case 1:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    break;
     
    case 2:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    break;   
   
    case 3:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    break;
   
    case 4:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    delay(600);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(300);
    break;
   
    case 5:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 6:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    delay(10);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 7:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    break;
   
    case 8:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(600);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(300);
    break;
   
    case 9:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 10:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(10);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 11:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    break;
   
    case 12:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(600);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(300);
    break;
   
    case 13:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 14:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(10);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 15:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    break;
   
    case 16:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(600);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(300);
    break;
   
    case 17:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 18:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 250);
    delay(10);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 19:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    break;
   
    case 20:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    delay(600);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(300);
    break;
   
    case 21:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 22:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 250);
    delay(10);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 23:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    break;
   
    case 24:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(600);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(300);
    break;
   
    case 25:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 26:
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(10);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 27:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    break;
   
    case 28:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(600);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(300);
    break;
   
    case 29:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(200);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
    case 30:
    analogWrite(redledPin, 250);
    analogWrite(blueledPin, 250);
    analogWrite(greenledPin, 0);
    delay(10);
    analogWrite(redledPin, 0);
    analogWrite(blueledPin, 0);
    analogWrite(greenledPin, 0);
    delay(100);
    break;
   
   
   }
}
Pages: [1] 2