Stop Random Speed Loop

Hi!

I need help with my Arduino Program, please!

I just started learning to use Arduino and I am looking to turn on an LED Strip Light and a DC Motor with a clip by using capacitive touch. While the DC Motor is on, it should go at random speed every 5 seconds until I touch the clip again to turn everything off.

I managed to activate everything after touching the clip, but I can’t find a way to turn everything off once I touch it again.

I have to let you know that I am new to Arduino, so I must apologize if my work is confusing (I combined examples I founded on the internet, hehe).

Here is my Arduino development for you to evaluate:

// 1) LED STRIP LIGHTS
int relayPin = 13;

// 2) CAPACITIVE TOUCH
int touchPin = 2;
int touchedCutoff = 50;

// 3) DC MOTOR
int randNumber;
int motorPin = 3;
int wait = 5; 

int state = LOW;

void setup() {
  pinMode(relayPin, OUTPUT);
  pinMode(motorPin, OUTPUT);
  pinMode(touchPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(touchPin), myISR, FALLING);
  randomSeed(millis());
}

void loop() {
  if (readCapacitivePin(touchPin) > touchedCutoff){
    digitalWrite(relayPin, state);
    while (state == HIGH){
      randomSeed(millis());
      randNumber = random(70, 200);
      analogWrite(motorPin, randNumber);
      delay(wait*1000);
      }
    }
}

void myISR(){
  state = !state;
}

// 2) CAPACITIVE TOUCH                                     // readCapacitivePin
                                                           // Input: Arduino pin number. Output: A number, from 0 to 17 expressing how much capacitance is on the pin.
                                                           // When you touch the pin, or whatever you have attached to it, the number will get higher.
uint8_t readCapacitivePin(int pinToMeasure){               // In order for this to work now, the pin should have a 1+Megaohm resistor pulling it up to +5v.
  volatile uint8_t* port;                                  // This is how you declare a variable which will hold the PORT, PIN, and DDR registers on an AVR.
  volatile uint8_t* ddr;
  volatile uint8_t* pin;
  byte bitmask;                                            // Here we translate the input pin number from Arduino pin number to the AVR PORT, PIN, DDR,-
  if ((pinToMeasure >= 0) && (pinToMeasure <= 7)){         // and which bit of those registers we care about.
    port = &PORTD;
    ddr = &DDRD;
    bitmask = 1 << pinToMeasure;
    pin = &PIND;
  }
  if ((pinToMeasure > 7) && (pinToMeasure <= 13)){
    port = &PORTB;
    ddr = &DDRB;
    bitmask = 1 << (pinToMeasure - 8);
    pin = &PINB;
  }
  if ((pinToMeasure > 13) && (pinToMeasure <= 19)){
    port = &PORTC;
    ddr = &DDRC;
    bitmask = 1 << (pinToMeasure - 13);
    pin = &PINC;
  }
  *port &= ~(bitmask);                                     // Discharge the pin first by setting it low and output
  *ddr  |= bitmask;
  delay(1);
  *ddr &= ~(bitmask);                                      // Make the pin an input WITHOUT the internal pull-up on
  int cycles = 16000;                                      // Now see how long the pin to get pulled up
  for(int i = 0; i < cycles; i++){
    if (*pin & bitmask){
      cycles = i;
      break;
    }
  }
  *port &= ~(bitmask);                                     // Discharge the pin again by setting it low and output.
  *ddr  |= bitmask;                                        // It's important to leave the pins low if you want to be able to touch more than 1 sensor at a time - 
                                                           // if the sensor is left pulled high, when you touch two sensors, your body will transfer the charge between sensors.
  return cycles;
}

That while loop will never exit because you never ask it to check the touch sensor again. Read the "Demonstration code for several things at the same time" thread https://forum.arduino.cc/index.php?topic=223286.0

While you're at it also read the "Read this before posting a programming question" thread as well, particularly about posting code in a readable format.

@vivianehdez

Please follow the advice on posting code given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

I am so sorry! Thank you for letting me know. I'll definitely will go through it and do it the right way. I apologize for the inconvenience.

chopsuwe:
That while loop will never exit because you never ask it to check the touch sensor again. Read the "Demonstration code for several things at the same time" thread https://forum.arduino.cc/index.php?topic=223286.0

While you're at it also read the "Read this before posting a programming question" thread as well, particularly about posting code in a readable format.

Thank you for the tip! I will study it and I'll let you know how it went!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.