12th doctor's sonic screwdriver

Hello wonderful people.

I need you help in some code. I am very new to arduino and im looking to do this

However add an additional red static light and some sort of chase or pulsing red setting.

So i want it to work exactly the same way but add three quick up presses making the static red light with sound activate and then downwards three time to make a cashing (like the double press down blue version) red with sound activate.

If any of you clever people could give me code help or some pointers that would be great

kind Regards

tom

A clever poster with questions about his code would post that code so help might be given.

Paul

TomBaines:
If any of you clever people could give me code help or some pointers that would be great

I guess William Hartnell could give the best description of the code behind his screwdriver.

Right ok.

Blue code 'chase'

byte ledPin [] = {1, 2, 3, 4};
int ledDelay(65);
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int bluePin = 5;

void setup()
{
for(int x=0; x<4; x++)
{
pinMode(ledPin[x], OUTPUT);
pinMode(bluePin, OUTPUT);
}
changeTime = millis();
}

void loop()
{
if ((millis() - changeTime) > ledDelay)
{
changeLED();
changeTime = millis();
}
}
void changeLED()
{
for (int x=0; x<4 ; x++)
{
digitalWrite(ledPin[x], LOW);
digitalWrite(bluePin, HIGH);
}
digitalWrite(ledPin[currentLED], HIGH) ;
currentLED += direction;
if (currentLED == 4)
{
direction =-1;
}

if(currentLED ==0)
{
direction = 1;
}
}

id like to add the sound to this but to play on a double press on a switch and stop as i let go

Please use [ code ] tags. The forum software has eaten some of your code right after "ledPin".

Look, this is all i have. I really am a beginner and i am looking for some help. That is the code i am using and it works to get my leds to 'chase' at the correct speed. I need to know how to add sound and how to add a double press switch that will switch off when i let go. Similar to the video above. If i haven't provided the correct information its because i do not know it. I f you need more please, without being patronising, let me know what else you need

We need [ code ] tags. Your code has been mangled by the forum software. We can't tell you what's wrong as some of that is not mistakes you made. It also makes it easier for users of some display devices such as tablets to have the code in a defined code window.

i'll be honest i dont know how to do that. I have copied and pasted what i used of arduino straight to here. Tell me what to do. I dont know what else to provide.

Please read the "How to use this forum" post at the top of this forum. That will tell you how to use code tags.

Hi Morgan S,

Thank you for your help here is my code tag

Blue code 'chase'

byte ledPin [] = {1, 2, 3, 4};
int ledDelay(65);
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int bluePin = 5;

void setup()
{
  for(int x=0; x<4; x++)
  {
    pinMode(ledPin
, OUTPUT);

    pinMode(bluePin, OUTPUT);
  }
  changeTime = millis();
}
 
void loop()
{
   if ((millis()  - changeTime) > ledDelay)
  {
    changeLED();
    changeTime = millis();
  }
}
void changeLED()
{
  for (int x=0; x<4 ; x++)
  {
    digitalWrite(ledPin
, LOW);

     digitalWrite(bluePin, HIGH);
  }
  digitalWrite(ledPin[currentLED], HIGH) ;
  currentLED += direction;
  if (currentLED == 4)
  {
    direction =-1;
  }
  
  if(currentLED ==0)
  {
    direction = 1;
  }
}

Yes, this is legal C code but please don't do it. For statements which fit on one line, you should use only one line.

    digitalWrite(ledPin

, LOW);

Why do you do exactly the same thing 4 times in a row?

  for (int x = 0; x < 4 ; x++)

{
    digitalWrite(ledPin, LOW);
    digitalWrite(bluePin, HIGH);
  }

To allow the thing to do different things at different times, you need a state machine. You will end up with a surprisingly large number of states: button-down-first, button-up-first, button-down-second, button-up-second...

Start with drawing the state diagram. Every single change on the button will be a new state. Then changeLED() can do different things depending on which state it's in.