Here is my code:
#include <SoftwareSerial.h>
#include <Servo.h> // servo library
Servo servo1; // servo control object
Servo servo2;
Servo servo3;
SoftwareSerial bluetooth(5, 6); // RX, TX
int servo1Pin = 11;
int servo2Pin = 10;
int servo3Pin = 9;
int motor1pin = 8;
int flag = 0; //Sets integer for HIGH or LOW LED case
String bdata = "";
char c = ' ';
void setup()
{
bluetooth.begin(9600);
pinMode(motor1pin, OUTPUT);
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
servo3.attach(servo3Pin);
}
void loop()
{
int position;
while (bluetooth.available()) {
c = bluetooth.read();
bdata += c;
//~~~~~~Servo 1~~~~~~~~
if (bdata == "open1")
{
//Opens Door
for (position = 0; position < 90; position += 1)
{
servo1.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
if (bdata == "close1")
{
//Closes Door
for (position = 90; position >= 0; position -= 1)
{
servo1.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
//~~~~~~Servo 2~~~~~~~~
if (bdata == "open2")
{
//Opens Door
for (position = 0; position < 90; position += 1)
{
servo2.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
if (bdata == "close2")
{
//Closes Door
for (position = 90; position >= 0; position -= 1)
{
servo2.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
//~~~~~~Servo 3~~~~~~~~
if (bdata == "open3")
{
//Opens Door
for (position = 0; position < 90; position += 1)
{
servo3.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
if (bdata == "close3")
{
//Closes Door
for (position = 90; position >= 0; position -= 1)
{
servo3.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
//~~~~~~Motor 1~~~~~~
if (bdata == "on")
{
flag = 1; //Turns LEDs on using a high digital signal
}
if (bdata == "off")
{
flag = 0; //Turns LEDs off using a low digital signal
}
}
digitalWrite(motor1pin, flag);
delay(1000);
bdata = "";
}
It is uploaded to an Arduino Uno where I have the 3 servos and DC Motor connected via breadboard. It is meant to power a 3d printed mechanical arm I have made. when read you can see it is also controlled via Bluetooth using a HC-05 module and phone. I am having trouble with it as the commands for servo 1 are working fine so is 3, but when command for servo 2 is sent from phone servo 3 moves instead and servo 3 command also moves servo 3. I have already tried looking at the variable and the commands on my phone by nothing is sticking out to me. so here I am.
Can anyone help?
Related
I was creating a PIR Motion sensor and GSM Module SMS 800A based Security System with Arduino UNO
But I am facing a problem from 2 to 3 days that whenever I send a SMS from my phone to my GSM module then the Motion sensor automatically detects a motion and makes the system active.
Please tell me why is this happening?
This is my code :->
#include <SoftwareSerial.h>
#include <Sim800L.h>
#include <MemoryFree.h>
Sim800L GSM(10, 11);
SoftwareSerial sim800(10, 11); //2 is TX and 3 is RX
int day, month, year, minute, second, hour;
int onDay, onMonth, onYear, onMinute, onSecond, onHour;
int offDay, offMonth, offYear, offMinute, offSecond, offHour;
unsigned long prevMillis;
unsigned long currentMillis;
const long interval = 30000;
boolean timeStatus = false;
int RTCPerm;
int led = 13;
int pin = 5;
int x = 1;
int m = 0;
char y;
char t;
int value = 0;
int pirState = LOW;
char n[3];
int j = 0;
int i = 0;
char data[200] = {};
int condition = 0;
char DTMF[200] = {};
int z;
int delayOnMinute, delayOffMinute, delayOnHour, delayOffHour;
// connections
//
// GSM ARDUINO UNO
//
// GND -----> GND
// TX -----> 2
// RX -----> 3
// Power Supply -----> 12V 1A or 5V 2A
//
// Motion Sensor Arduino UNO
//
// VCC -----> 5V
// GND -----> GND
// OUT -----> 4
void setup() {
pinMode(led, OUTPUT);
pinMode(12, OUTPUT);
pinMode(pin, INPUT);
Serial.begin(9600);
sim800.begin(9600);
delay(6000);
sim800.println(F("AT+CMGF=1"));
sim800.println(F("AT+CNMI=2, 2, 0, 0, 0"));
sim800.println(F("AT+DDET=1"));
GSM.begin(9600); // for RTC data input
delay(5000);
onMinute = 30;
onHour = 17;
offMinute = 00;
offHour = 15;
}
void loop() {
while (RTCPerm == 0) {
RTCStatus();
prevMillis = millis();
while (currentMillis - prevMillis != interval) {
// save the last time you blinked the LED
currentMillis = millis();
SMSRecieve();
if (timeStatus == true) {
value = digitalRead(pin);
if (value == HIGH) {
digitalWrite(led, HIGH);
if (pirState == LOW) {
goto Motion;
}
}
}
}
}
Serial.println("Hello");
value = digitalRead(pin);
if (value == HIGH) {
digitalWrite(led, HIGH);
if (pirState == LOW) {
Motion:
Serial.println(F("Motion Detected!"));
sim800.begin(9600);
So in the loop function you can see the SMS Receive function that checks whether there is any incoming SMS or not. So whenever I send a SMS to my GSM then my Motion sensor gets active and goes to label Motion which activates other functioning like starting the alarm etc.
This is the code of SMSRecieve() Function :->
void SMSRecieve() {
Samarth:
/* if (condition == 2) {
memset(data, 0, sizeof(data));
y = 0;
condition = 1;
}*/
if (sim800.available())
{ // Printing the collected data
y = sim800.read();
Serial.write(y);
if (y == '#') {
goto skip;
}
data[i] = y;
i++;
skip:
if (strstr(data, "off") || strstr(data, "OFF") || strstr(data, "Off")) {
// Do Something
} else if (strstr(data, "Lighton") || strstr(data, "lighton") || strstr(data, "LightOn") || strstr(data, "LIGHTON")) {
// Do Something
}else if (strstr(data, "Lightoff") || strstr(data, "lightoff") || strstr(data, "LightOff") || strstr(data, "LIGHTOFF")) {
// Do Something
}
}
}
Please help me where I am doing wrong ?
After testing the situation again and again I got the solution and the reason why that was happening.
REASON:- PIR Motion Sensors also detect the RF and other unseen frequencies in the air. So whenever I sent a SMS to my GSM, it was detected by my PIR Motion Sensor.
SOLUTION:- The GSM Module should be kept with at least 2 feet of distance from the PIR Motion Sensor
I'm writing an Arduino program to control a minigun for a friend's cosplay. I need to write a program that when the button is pressed the motor ramps up from 0 to a given value (for now lets say "analogWrite(output_pin,200);") then loop at that RPM until the button is released, at which point it needs to ramp down back to zero.
When I try to put ramping into a loop it doesn't finish the code. I
I need something like this in c++ code for the Arduino button. (I've tried similar things using the "delay" function to no avail)
motorspeed = 0
if buttonpress == True:
buttonheld = True
for i in range (0,10):
delay(1)
motorspeed =+ 20
if buttonpress == False:
motorspeed = 0
if buttonheld == True:
motorspeed = 200
if buttonpress == False:
for i in range(0,10):
delay(1)
motorspeed =- 20
else:
#shut off motor
#Play error sound
Here is the current code that only runs the motor at one speed when the button is held down.
const int button1 =4;
int BUTTONstate1 = 0;
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(9, OUTPUT);
pinMode(button1, INPUT);
}
void loop()
{
//set button to read
BUTTONstate1 = digitalRead(button1);
//if button is pressed
if (BUTTONstate1 == HIGH)
{
//run current through motor
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
//set speed
//Rampup
analogWrite(9,200);
}
else
{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
}
}
Here is a virtual environment for the circuit
https://www.tinkercad.com/things/cLOBc9JJuTz-copy-of-dayloncircuitrefinecode/editel?sharecode=b6cqTLlNqUCCN09-mQ_zykp5sMnXx6KLt_KNqlXJmcs
I would recommend using interrupts, it's pretty well documented in Arduino reference:
Arduino reference - Interrupts
Most helpful is "Parameters" and "Example code" section. I think you should prepare two methods and attach interrupt to your input (button) pin on RISING and FALLING trigger.
Make sure that the button pin can be used for interrupt (on Arduino UNO / NANO only pin 2 and 3).
I think it should look similar to this (I haven't tested that):
#define buttonPin 2; // pin usable for interrupt
#define outputPin 9;
#define powerPinA 3;
#define powerPinB 4;
bool running = false;
int currentSpeed = 0;
void buttonUp()
{
digitalWrite(powerPinA, LOW);
digitalWrite(powerPinB, LOW);
running = false;
}
void buttonDown()
{
digitalWrite(powerPinA, LOW);
digitalWrite(powerPinB, HIGH);
running = true;
}
void setup()
{
pinMode(outputPin, OUTPUT);
pinMode(powerPinA, OUTPUT);
pinMode(powerPinB, OUTPUT);
pinMode(buttonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), buttonUp, RAISING);
attachInterrupt(digitalPinToInterrupt(interruptPin), buttonDown, FALLING);
}
void loop()
{
delay(1);
if (running && currentSpeed < 200)
currentSpeed += 20;
else if (!running && currentSpeed > 0)
currentSpeed -= 20;
analogWrite(outputPin, currentSpeed);
}
Here's an example that doesn't rely on any interrupts. Just reading the button and remembering the state in a variable and remembering the time of the last step in a variable to check against millis() (Compiled but Untested)
int buttonPin = 5;
int lastButtonState = HIGH;
unsigned long lastRampStep;
unsigned int stepTime = 10; // 10ms steps. Set to whatever you want.
int analogPin = 3;
int analogLevel = 0;
int maxLevel = 200;
int analogStepSize = 10;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // will read LOW when pressed
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
// The button just changed!
if (buttonState == LOW) {
// was just now pressed
analogLevel = 0; // start ramp up
lastRampStep = millis(); // note the time that we took this step
} else {
// Button just released.
// not sure what you want to happen here
}
}
else if (buttonState == LOW) {
// While button is held ramp until we reach the max level
if(analogLevel < maxLevel){
if(millis() - lastRampStep >= stepTime){
analogLevel += analogStepSize;
lastRampStep = millis();
}
}
} else {
// While button is released (HIGH) ramp back down:
if(analogLevel > 0){
if(millis() - lastRampStep >= stepTime){
analogLevel -= analogStepSize;
lastRampStep = millis();
}
}
}
analogWrite(analogPin, analogLevel);
lastButtonState = buttonState;
}
You could use interrupts to handle pressed/release, set a value, and handle the ramp up/down in the loop.
Keep in mind that you should also debounce the button.
Below is some pseudocode:
const byte maxLevel = 200; // some example values (max 255)
const int rampDelay = 5;
volatile byte targetLevel = 0;
byte currentLevel = 0;
// ...
ISR for Button { // Could be in one ore two ISR's
if pressed
targetLevel = maxLevel;
// possible other calls
if released
targetLevel = 0;
// possible other calls
}
void loop()
{
if (currentLevel < targetLevel) {
SetLevel(++currentLevel);
}
else if (currentLevel > targetLevel) {
SetLevel(--currentLevel);
}
// if currentLevel == targetLevel nothing changes
// if your device is battery powered you could even put the mcu
// to sleep when both levels are zero (and wake up from the ISR)
// no need for constantly polling the button when using ISR
delay(rampDelay);
}
This will also start ramping down immediately if the button is released before the motor is at full speed.
I'm currently hitting a brick wall while trying to edit some code. I'm trying to input a pause/resume piece into the code, so that when the electrode is touched - if playing it will be paused and then consequently be able to be resumed by touching it again.
With my current edit of the code, the track isn't resuming once paused.
I'm learning as I go along so apologies, if it is an glaringly obvious fix. Any help will be greatly appreciated!
Link to library using is here:
https://github.com/mpflaga/Sparkfun-MP3-Player-Shield-Arduino-Library
See the code I'm working with below!
// compiler error handling
#include "Compiler_Errors.h"
// touch includes
#include <MPR121.h>
#include <Wire.h>
#define MPR121_ADDR 0x5C
#define MPR121_INT 4
// mp3 includes
#include <SPI.h>
#include <SdFat.h>
#include <FreeStack.h>
#include <SFEMP3Shield.h>
// mp3 variables
SFEMP3Shield MP3player;
byte result;
int lastPlayed = 0;
// mp3 behaviour defines
#define REPLAY_MODE FALSE // By default, touching an electrode repeatedly will
// play the track again from the start each time.
//
// If you set this to FALSE, repeatedly touching an
// electrode will stop the track if it is already
// playing, or play it from the start if it is not.
// touch behaviour definitions
#define firstPin 0
#define lastPin 11
// sd card instantiation
SdFat sd;
void setup(){
Serial.begin(57600);
pinMode(LED_BUILTIN, OUTPUT);
//while (!Serial) ; {} //uncomment when using the serial monitor
Serial.println("Bare Conductive Touch MP3 player");
if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
if(!MPR121.begin(MPR121_ADDR)) Serial.println("error setting up MPR121");
MPR121.setInterruptPin(MPR121_INT);
MPR121.setTouchThreshold(5);
MPR121.setReleaseThreshold(5);
result = MP3player.begin();
MP3player.setVolume(10,10);
if(result != 0) {
Serial.print("Error code: ");
Serial.print(result);
Serial.println(" when trying to start MP3 player");
}
}
void loop(){
readTouchInputs();
}
void readTouchInputs(){
if (MPR121.touchStatusChanged()) {
MPR121.updateTouchData();
// only make an action if we have one or fewer pins touched
// ignore multiple touches
if (MPR121.getNumTouches() <= 1) {
for (int i = 0; i < 12; i++) { // Check which electrodes were pressed
if (MPR121.isNewTouch(i)) {
//pin i was just touched
Serial.print("pin ");
Serial.print(i);
Serial.println(" was just touched");
digitalWrite(LED_BUILTIN, HIGH);
if (i <= lastPin && i >= firstPin) {
if (MP3player.isPlaying()) {
if (lastPlayed == i && !REPLAY_MODE) {
// if we're already playing the requested track, stop it
// (but only if we're not in REPLAY_MODE)
MP3player.pauseMusic();
Serial.print("paused_playback");
Serial.println(MP3player.getState());
}
else {
// if the track is already paused, resume the track
if (MP3player.getState() == paused_playback) {
if (lastPlayed == i) {
MP3player.resumeMusic();
Serial.print("resuming");
Serial.println(i-firstPin);
} else {
// if we're already playing a different track (or we're in
// REPLAY_MODE), stop and play the newly requested one
MP3player.stopTrack();
MP3player.playTrack(i-firstPin);
Serial.print("playing track ");
Serial.println(i-firstPin);
// don't forget to update lastPlayed - without it we don't
// have a history
lastPlayed = i;
}
}
}
} else {
// if we're playing nothing, play the requested track
// and update lastplayed
MP3player.playTrack(i-firstPin);
Serial.print("playing track ");
Serial.println(i-firstPin);
lastPlayed = i;
}
}
} else {
if (MPR121.isNewRelease(i)) {
Serial.print("pin ");
Serial.print(i);
Serial.println(" is no longer being touched");
digitalWrite(LED_BUILTIN, LOW);
}
}
}
}
}
}
Thanks Again!
edited code with last played included
// compiler error handling
#include "Compiler_Errors.h"
// touch includes
#include <MPR121.h>
#include <Wire.h>
#define MPR121_ADDR 0x5C
#define MPR121_INT 4
// mp3 includes
#include <SPI.h>
#include <SdFat.h>
#include <FreeStack.h>
#include <SFEMP3Shield.h>
// mp3 variables
SFEMP3Shield MP3player;
byte result;
int lastPlayed = 0;
// mp3 behaviour defines
#define REPLAY_MODE FALSE // By default, touching an electrode repeatedly will
// play the track again from the start each time.
//
// If you set this to FALSE, repeatedly touching an
// electrode will stop the track if it is already
// playing, or play it from the start if it is not.
// touch behaviour definitions
#define firstPin 0
#define lastPin 11
enum Action {
DO_NOTHING,
PLAY,
PAUSE,
RESUME,
STOP_THEN_PLAY
};
Action nextAction(state_m state, int i) {
if (state == paused_playback && lastPlayed == i) {
return RESUME;
}
if (state != playback) {
return PLAY;
}
if (state == playback && lastPlayed == i){
return PAUSE;
}
if (state == playback) {
return STOP_THEN_PLAY;
}
return DO_NOTHING;
}
// sd card instantiation
SdFat sd;
void setup(){
Serial.begin(57600);
pinMode(LED_BUILTIN, OUTPUT);
//while (!Serial) ; {} //uncomment when using the serial monitor
Serial.println("Bare Conductive Touch MP3 player");
if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
if(!MPR121.begin(MPR121_ADDR)) Serial.println("error setting up MPR121");
MPR121.setInterruptPin(MPR121_INT);
MPR121.setTouchThreshold(5);
MPR121.setReleaseThreshold(5);
result = MP3player.begin();
MP3player.setVolume(10,10);
if(result != 0) {
Serial.print("Error code: ");
Serial.print(result);
Serial.println(" when trying to start MP3 player");
}
}
void loop(){
readTouchInputs();
}
void readTouchInputs(){
if(MPR121.touchStatusChanged()){
MPR121.updateTouchData();
// only make an action if we have one or fewer pins touched
// ignore multiple touches
if(MPR121.getNumTouches()<=1){
for (int i = 0; i < 12; i++) { // Check which electrodes were pressed
if (MPR121.isNewTouch(i)) {
state = MP3player.getState();
Action action = nextAction(state, i); // find what to do next
switch (action) {
case PLAY:
Serial.println("play");
MP3player.playTrack(i-firstPin);
lastPlayed = i;
state = playback;
break;
case PAUSE:
Serial.println("pause");
MP3player.pauseMusic();
state = paused_playback;
lastPlayed = i;
break;
case RESUME:
Serial.println("resume");
MP3player.resumeMusic();
state = playback;
break;
case STOP_THEN_PLAY:
Serial.println("stop then play");
MP3player.stopTrack();
MP3player.playTrack(i-firstPin);
state = playback;
lastPlayed = i;
break;
default:
break;
}
}
}
}
}
}
Deeply nested if statements are very difficult to follow. I understand that you want to trigger one of four actions (play, pause, resume and stop/play) based on the state of MP3 player, last played track and electrode ID. I suggest you define a function like this to determine an action.
enum Action {
DO_NOTHING,
PLAY,
PAUSE,
RESUME,
STOP_THEN_PLAY
};
Action nextAction(state_m state, int i) {
if (state == paused_playback && lastPlayed == i) {
return RESUME;
}
if (state != playback) {
return PLAY;
}
if (state == playback && lastPlayed == i){
return PAUSE;
}
if (state == playback) {
return STOP_THEN_PLAY;
}
return DO_NOTHING;
}
Pass the state of MP3 player and the electrode's id to the function. You can then trigger an appropriate action based on the value of action.
for (int i = 0; i < 12; i++) { // Check which electrodes were pressed
if (MPR121.isNewTouch(i)) {
state_m state = MP3player.getState();
Action action = nextAction(state, i); // find what to do next
switch (action) {
case PLAY:
Serial.println("play");
MP3player.playTrack(i-firstPin);
lastPlayed = i;
state = playback;
break;
case PAUSE:
Serial.println("pause");
MP3player.pauseMusic();
state = paused_playback;
lastPlayed = i;
break;
case RESUME:
Serial.println("resume");
MP3player.resumeMusic();
state = playback;
break;
case STOP_THEN_PLAY:
Serial.println("stop then play");
MP3player.stopTrack();
MP3player.playTrack(i-firstPin);
state = playback;
lastPlayed = i;
break;
default:
break;
}
}
}
This question already has answers here:
What are forward declarations in C++?
(8 answers)
Closed 5 years ago.
Im working with Arduino IDE, which doesn't allow nested functions, or calling a function before it had been declared(it doesnt jump to find it)..
In my code, I need either one or the other..
Also, making it all one big function does not work because when i check back to an earlier sensor, i have to write what to do if yes and if no over and over forever.
Here, im trying to sense light, if theres light, go to the PIR sensor, if not, run light sensor again. now if the PIR senses motion twice in 3.5 sec, check color, if not, check light.
Now the color senses color and clasifies it(r g or b). now it checks for PIR again and if there's movement, it check color again and checks pir again. now if the pir doesnt sense movement, it sends to the lines to serial..
I declared some functions before I used them(print lines) but some of them not because i get stuck in a loop.
When i check light, if its positive, i run the PIR,so i would need to declare the pir function before ligth. but in the pir, if its negative(no movement), i run light sensor, implying that i should declare light sensor before pir.
Im stuck in that loop...
int lightPin = A0;
int valLight = 0;
int ledTrans = 2;
int pirTrans = 3;
int pirPin = 4;
int pirState = LOW;
int CalTime = 30;
int colorTrans = 5;
int s2 = 6;
int s3 = 7;
int OUTpin= 8;
boolean RD= false;
boolean GD = false ;
boolean BD = false;
int rfPin = 9;
void setup() {
pinMode(A0,INPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,INPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,INPUT);
pinMode(9,OUTPUT);
Serial.begin(9600);
}
void printlines();
void pirSensor();
void colorSensor();
void pirSensor2();
void lightSensor() {
//light sensor
valLight = analogRead(A0);
delay(1000);
if (valLight >= 300) {
ledTrans = HIGH;
pirTrans = HIGH;
pirSensor();
} else {
delay(8000);
}
}
void pirSensor() {
// Calibration
for ( byte i = 0; i < 30; i++) {
delay(1000);
}
// Check PIR state
int pirState = digitalRead(2);
delay(1500);
pirState = digitalRead(2);
if (pirState == HIGH) {
colorTrans = HIGH;
colorSensor();
} else if (pirState == LOW) {
pirState = LOW;
lightSensor();
}
}
void colorSensor(){
//Check Color reads pulse for RGB
// void checkred
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
unsigned int RW = 255 - (pulseIn(OUTpin, LOW)/ 400 - 1); // turns into 0-255
delay(6000);
// void checkgreen
digitalWrite(s2,LOW);
digitalWrite(s3,HIGH);
unsigned int GW = 255 - (pulseIn(OUTpin, LOW)/ 400 - 1);
delay(6000);
// void checkblue
digitalWrite(s2, HIGH);
digitalWrite( s3, HIGH);
unsigned int BW = 255 - (pulseIn(OUTpin, LOW) / 400 - 1);
delay(6000);
// seeing which color I got(r g or b)
if (RW > BW && RW > GW){
RD = true;
delay(7000);
} else if (GW > RW && GW > BW){
GD = true;
delay(7000);
} else if (BW > RW && BW > GW){
BD = true;
delay(4000);
}
}
void pirSensor2(){
pirState = digitalRead(2);
delay(1500);
pirState = digitalRead(2);
if(pirState = HIGH){
colorSensor();
}else{
printlines();
}
}
void printlines(){
if(RD){
Serial.print("RED DETECTED");
} else if(GD){
Serial.print("GREEN DETECTED");
}else if(BD){
Serial.print("BLUE DETECTED");
}else if(RD && GD){
Serial.print("RED & GREEN DETECTED");
}else if(RD && BD){
Serial.print("RED & BLUE DETECTED");
}else if(BD && GD){
Serial.print("GREEN & BLUE DETECTED");
}else if(RD && GD && BD){
Serial.print("RED, GREEN, BLUE DETECTED");
}
delay(7000);
}
You can declare a function before you define it:
void setup();
Then you can call it:
setup();
...and somewhere later in the file you can write its definition:
void setup() {
// implementation here
}
My question is very simple but I'm stuck on it for a while now.
I need to make a script that when pushed on a button loop a() is put on pause. And when you press it again it should go from where it ended. But I can't figure out a way to do it.
I hope someone can help me.
This is my code:
int Aan = 1;
int Uit = 0;
int analogPin = A3;
int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int LED4 = 10;
int val;
bool r = false;
void setup() {
pinMode(analogPin, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
digitalWrite(LED1, Aan);
digitalWrite(LED2, Aan);
digitalWrite(LED3, Aan);
digitalWrite(LED4, Aan);
}
void loop() {
val = digitalRead(analogPin);
if (val == LOW)
{
if (r == true)
{
r = false;
}
if (r == false)
{
r = true;
}
}
if (r == true)
{
a();
}
}
void a() {
for (int i = 10; i <= 13; i++)
{
pinMode(i, OUTPUT);
digitalWrite(i, Uit);
delay(100);
digitalWrite(i, Aan);
}
for (int i = 13; i >= 10; i--)
{
pinMode(i, OUTPUT);
digitalWrite(i, Uit);
delay(100);
digitalWrite(i, Aan);
}
}
Just to explain whats happening.. Void a() makes 4 different leds light up and go out. The pattern thats used is Knight Rider (If you don't know the tv show just google the car of him)
I will assume you want the "Knight Rider" pattern to constantly run.
I've made a couple of changes. First, I added a function to run the led sequence one way. Second, I added a while loop that will always run once, and will continue to run while the button is pushed.
bool paused = false;
int buttonState = HIGH;
void loop() {
a();
}
// This only works if leds ports are consecutive
void runSequence(int ledStart, int ledEnd)
{
int direction = ledStart < ledEnd ? 1 : -1;
for (int i = ledStart; i != ledEnd + direction; i += direction) {
digitalWrite(i, Uit);
do {
delay(100);
} while (LOW == digitalRead(analogPin)); // Check button state
digitalWrite(i, Aan);
}
}
void a() {
runSequence(LED4, LED1);
runSequence(LED1, LED4);
}
EDIT Changes based on comment
bool paused = false;
int buttonState = HIGH;
int currentLED = LED1;
int currentDirection = -1;
void loop() {
checkButton();
if (!paused) {
// Flash the led
digitalWrite(currentLED, Uit);
delay(100);
digitalWrite(currentLED, Aan);
// Change direction?
if (LED1 == currentLED || LED4 == currentLED) {
currentDirection *= -1;
}
// Setup for next iteration
currentLED += currentDirection;
}
}
void checkButton() {
int state = digitalRead(analogPin);
// Check if button state has changed
if (state != buttonState) {
buttonState = state;
// Change paused state when button is released
if (state == HIGH) {
paused = !paused;
}
}
}
Inside your a() function put another while loop which becomes false when the button is Up.
Like:
while(digitalRead(analogPin) == LOW)
{
}
As long as the button is pressed down the loop will continue. When you release the button the program will exit the loop and continue execution of your code.