Making a nested loop repeat before going back to main loop

I'm going through the Tuts so I bother you guys less. I'm enjoying the learning very much. I've always wanted to learn a code so I think this is a great way to start.

I was one the very first one (blink) with no issues (if I had issues on that one I'm guessing I should just break my arduino in half now). So I made 2 blinks at different times.

int led = 13;

void setup() {
  pinMode(led, OUTPUT);
}

void loop() {
  digitalWrite(led, HIGH);
  delay(1000);
  digitalWrite (led, LOW);
  delay(1000);
  {  
  digitalWrite(led, HIGH);
  delay(200);
  digitalWrite(led, LOW);
  delay(2000);
}
}

Now what I'd like to do is make the inner loop (the delay 200 blink) repeat say.... 5 times before coming back out to the outside loop (is there a better term for them besides inner and outside loops?)

Thank you VERY much once again.

use a for loop

for(int i = 0; i < 5; i++)
{
  digitalWrite(led, HIGH);
  delay(200);
  digitalWrite(led, LOW);
  delay(2000);
}

its on the reference now that you know what your looking for

Thank you VERY much once again.

yea, no problem, as long as your trying we dont mind at all

1 Like

Osgeld:

for(int i = 0; i < 5; i++)

{
 digitalWrite(led, HIGH);
 delay(200);
 digitalWrite(led, LOW);
 delay(2000);
}

So the "for" followed by "int" lets you set a peripheral that must be reached before the loop can end? Or atleast in this case? I see that being used in a large amount of ways.
Also what does the "i" that follows "for(int" do in this case. I tried googleing but when your using "i" in any search its not an easy task finding what you want.

i is the loop control variable in this case. It goes from 0 to 4.

Loops can be more general than that. Try looking up "for loop".

for(int i = 0; i < 5; i++)

The 'for' says to repeat the statements inside the { } brackets until the expression returns false.

'int i' says define a new integer variable to be used as a simple counter and set it's initial value to 0.
i < 5 is the test for the for expression that is calculated for each time through the enclosed statements are executed, so perform the statements five times (0 to 4 counts of i). i++ means to increment the i variable each time the statements are executed.\

Here is the reference for for: http://arduino.cc/en/Reference/For

That make sense?

Lefty

1 Like

and for the record "i" could be anything you choose to call it, its just the name of a local variable to the loop(and you can use it inside the loop as well)

I just use i as the variable name as shorthand for iteration, which of course means

Iteration means the act of repeating a process usually with the aim of approaching a desired goal or target or result.

unless I have a specific meaning for that variable

maybe this will help you see

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    for(int counter = 0; counter < 10; counter++)
    {
        Serial.println(counter);
        delay(1000);
    }
}

which should count from 0-9 then reset and loop again (since its in the main loop)

O wow... Yeah thanks for the key word. That was a HUGE chunk in learning this. Atleast for me. Wow.... I wish I had learned this years ago. I'm going to need some more arduinos LOL.

retrolefty:
That make sense?
Lefty

Perfect. very VERY clear. Your good at dumbing it down hahaha.

So I'm trying to not ask to much of you guys on this one and really want to learn how to code but I just wasted 4 hours of my life. I want this thing to control a lightning storm on my reef tanks leds. So that being said I needed random delays. So I googled around and found how to use an analogRead to make random numbers. Well it was really hard to control so it was about 30 mins making the code correct and then 3.5 hours tweeking it. I came up with this.

void loop() {
  wildcard = (analogRead(5)%30)*2;
  darksky = (analogRead(0)%75)*200;
  lightsky = (analogRead(5)%100)*wildcard; 
  delay(darksky);
  digitalWrite(led, HIGH);   // turn the LED on
  delay(lightsky);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED
  delay(1000);               // wait for a second
}

And man o man was I proud of myself. I was going to come on and ask a question about getting a wider range of numbers so I could have 10mS all the was to 5ish seconds but plowed on ahead anyways.

So then I went on to making the led flicker to look more like lightning and the first thing I see after 10 seconds on google is delay(random(200,1000))

:0 OMG GTFO ROFLCOPPTER BOOM HEADSHOT

I know I will never stop going through this. I mean you all still do every once in awhile right? But this is my first so I feel entitled to the right to make you all read about it.

heres my new one after 10 mins of testing.

void loop() {
  burst = random(10,100);
  big = random(3000,4000);
  delay(1000);
  digitalWrite(led, HIGH);   // turn the LED on
  delay(random(burst, big)); 
  digitalWrite(led, LOW);    // turn the LED off
}

I've been up since 10:00 am yesterday, started learning this stuff at around 10:00 pm and now it 5:30 am. I'm to tired to make them flicker. Maybe the arduino forum fairy will visit my post in my sleep and write the flicker for me ahahaha.

#include "SIM900.h"
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include "sms.h"
#define GSM_ON 9
#define GSM_RESET 8
SMSGSM sms;
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
SoftwareSerial sim900(2,3);

int i;
int led = 10;
int valLed;
int ldr = A0;
int valorSensor;

boolean started=false;
char smsbuffer[160];
char n[20];
char c;

void setup(){
pinMode(led, OUTPUT);
pinMode(ldr, INPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
digitalWrite(led, LOW);
}

void SIM900power(){ //on / off the SIM
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9,HIGH);
delay(5000);
}

void SIM900reset(){ //is reset to SIM900
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, HIGH);
delay(5000);
}

void connect(){ //connects to the GSM network
if(gsm.begin(9600)){
Serial.println("\nstatus=READY");
started=true;
}
else Serial.println("\nstatus=IDLE");
if(started){
delsms();
}
}

void enviarSMS1(){
lcd.begin(16, 2);
lcd.print("dry soil please");
lcd.setCursor(0, 1);
lcd.print(" pump send : ON");
delay(5000);
sms.SendSMS("+", "sensor register . To light LED send ON");
Serial.println("\nSMS sent OK");
delay(15000);
}
void enviarSMS2(){
lcd.begin(16, 2);
lcd.print(" pump on to off ");
lcd.setCursor(0, 1);
lcd.print("send : OFF ");
sms.SendSMS("+", "led on . To delete send OFF");
Serial.println("\nSMS sent OK");
delay(15000);
}
void enviarSMS3(){
lcd.begin(16, 2);
lcd.print("the pump is OFF ");
lcd.setCursor(0, 1);
lcd.print("");
sms.SendSMS("+", "lED off");
Serial.println("\nSMS sent OK");
delay(15000);
}
void enviarSMS4(){
lcd.begin(16, 2);
lcd.print("SIM900 off");
lcd.setCursor(0, 1);
lcd.print("");
sms.SendSMS("+", "SIM900 off");
Serial.println("\nSMS sent OK");
delay(15000);
}
void enviarSMS5(){
lcd.begin(16, 2);
lcd.print("");
lcd.setCursor(0, 1);
lcd.print("");
sms.SendSMS("+", "sensor without registration . To clear LED send OFF");
Serial.println("\nSMS sent OK");
delay(15000);
}

void receberSMS(){
int pos=0;
if(started){
pos=sms.IsSMSPresent(SMS_ALL);
if(pos){
Serial.println("IsSMSPresent at pos ");
Serial.println(pos);
sms.GetSMS(pos,n,smsbuffer,100);
Serial.println(n);
Serial.println(smsbuffer);
if(!strcmp(smsbuffer,"ON")){
Serial.println("ON");
lcd.begin(16, 2);
lcd.print("pump is working ");
digitalWrite(led, HIGH);
delay(20000);
digitalWrite(led, LOW);
delay(50);
enviarSMS2();
}
if(!strcmp(smsbuffer,"OFF")){
Serial.println("OFF");
digitalWrite(led, LOW);
enviarSMS3();

}
if(!strcmp(smsbuffer,"SIMOFF")){
Serial.println("SIMOFF");
enviarSMS4();
SIM900power();
}

delay(1000);
delsms();
}
}
}

void delsms(){ //delete sms on the card
Serial.println("delsms");
for (int i=0; i<10; i++){ //do it max 10 times
int pos=sms.IsSMSPresent(SMS_ALL);
if (pos!=0){
Serial.print("\nFind SMS at the pos ");
Serial.println(pos);
if (sms.DeleteSMS(pos)==1){
Serial.print("\nDeleted SMS at the pos ");
Serial.println(pos);
}
else
{
Serial.print("\nCant del SMS at the pos ");
Serial.println(pos);
}
}
}
}

void loop(){
for(int valorSensor = 0; valorSensor < 1; valorSensor++)
{
valorSensor = analogRead(ldr);
valLed=digitalRead(led);
lcd.begin(16, 2);
lcd.print(" AUTOMATIC ");
lcd.setCursor(0, 1);
lcd.print(" GARDENING ");
delay(3000);
lcd.clear();
lcd.begin(16, 2);
lcd.print(" WAITTING ...");
delay(3000);
Serial.print("State sensor: ");
Serial.println(valorSensor);
delay(1000);

if(valorSensor>300 && valLed==LOW){ //tanah kering
while (1){
SIM900reset();
connect();
enviarSMS1();
delay(6000);
receberSMS();
break;
}
}

if(valorSensor<=300 && valLed==HIGH){ //tanah basah
while (1){
SIM900reset();
connect();
enviarSMS5();
delay(6000);
receberSMS();
break;
}
}
}
}

this code it send message "the soil dry" to phone and after i dont send something to gsm it wiil continue send message"the soil dry " :confused:

help me to make the code send message for one time only .....
for example : the soil is dry and it send message to phone and stop ... after i send something to gsm it continue the code ....

i want the code stay function but only the message send one time

please help me

afiqzty:
#include "SIM900.h"
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include "sms.h"
#define GSM_ON 9
#define GSM_RESET 8
SMSGSM sms;
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
SoftwareSerial sim900(2,3);

int i;
int led = 10;
int valLed;
int ldr = A0;
int valorSensor;

boolean started=false;
char smsbuffer[160];
char n[20];
char c;

void setup(){
pinMode(led, OUTPUT);
pinMode(ldr, INPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
digitalWrite(led, LOW);
}

void SIM900power(){ //on / off the SIM
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9,HIGH);
delay(5000);
}

void SIM900reset(){ //is reset to SIM900
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, HIGH);
delay(5000);
}

void connect(){ //connects to the GSM network
if(gsm.begin(9600)){
Serial.println("\nstatus=READY");
started=true;
}
else Serial.println("\nstatus=IDLE");
if(started){
delsms();
}
}

void enviarSMS1(){
lcd.begin(16, 2);
lcd.print("dry soil please");
lcd.setCursor(0, 1);
lcd.print(" pump send : ON");
delay(5000);
sms.SendSMS("+", "sensor register . To light LED send ON");
Serial.println("\nSMS sent OK");
delay(15000);
}
void enviarSMS2(){
lcd.begin(16, 2);
lcd.print(" pump on to off ");
lcd.setCursor(0, 1);
lcd.print("send : OFF ");
sms.SendSMS("+", "led on . To delete send OFF");
Serial.println("\nSMS sent OK");
delay(15000);
}
void enviarSMS3(){
lcd.begin(16, 2);
lcd.print("the pump is OFF ");
lcd.setCursor(0, 1);
lcd.print("");
sms.SendSMS("+", "lED off");
Serial.println("\nSMS sent OK");
delay(15000);
}
void enviarSMS4(){
lcd.begin(16, 2);
lcd.print("SIM900 off");
lcd.setCursor(0, 1);
lcd.print("");
sms.SendSMS("+", "SIM900 off");
Serial.println("\nSMS sent OK");
delay(15000);
}
void enviarSMS5(){
lcd.begin(16, 2);
lcd.print("");
lcd.setCursor(0, 1);
lcd.print("");
sms.SendSMS("+", "sensor without registration . To clear LED send OFF");
Serial.println("\nSMS sent OK");
delay(15000);
}

void receberSMS(){
int pos=0;
if(started){
pos=sms.IsSMSPresent(SMS_ALL);
if(pos){
Serial.println("IsSMSPresent at pos ");
Serial.println(pos);
sms.GetSMS(pos,n,smsbuffer,100);
Serial.println(n);
Serial.println(smsbuffer);
if(!strcmp(smsbuffer,"ON")){
Serial.println("ON");
lcd.begin(16, 2);
lcd.print("pump is working ");
digitalWrite(led, HIGH);
delay(20000);
digitalWrite(led, LOW);
delay(50);
enviarSMS2();
}
if(!strcmp(smsbuffer,"OFF")){
Serial.println("OFF");
digitalWrite(led, LOW);
enviarSMS3();

}
if(!strcmp(smsbuffer,"SIMOFF")){
Serial.println("SIMOFF");
enviarSMS4();
SIM900power();
}

delay(1000);
delsms();
}
}
}

void delsms(){ //delete sms on the card
Serial.println("delsms");
for (int i=0; i<10; i++){ //do it max 10 times
int pos=sms.IsSMSPresent(SMS_ALL);
if (pos!=0){
Serial.print("\nFind SMS at the pos ");
Serial.println(pos);
if (sms.DeleteSMS(pos)==1){
Serial.print("\nDeleted SMS at the pos ");
Serial.println(pos);
}
else
{
Serial.print("\nCant del SMS at the pos ");
Serial.println(pos);
}
}
}
}

void loop(){
for(int valorSensor = 0; valorSensor < 1; valorSensor++)
{
valorSensor = analogRead(ldr);
valLed=digitalRead(led);
lcd.begin(16, 2);
lcd.print(" AUTOMATIC ");
lcd.setCursor(0, 1);
lcd.print(" GARDENING ");
delay(3000);
lcd.clear();
lcd.begin(16, 2);
lcd.print(" WAITTING ...");
delay(3000);
Serial.print("State sensor: ");
Serial.println(valorSensor);
delay(1000);

if(valorSensor>300 && valLed==LOW){ //tanah kering
while (1){
SIM900reset();
connect();
enviarSMS1();
delay(6000);
receberSMS();
break;
}
}

if(valorSensor<=300 && valLed==HIGH){ //tanah basah
while (1){
SIM900reset();
connect();
enviarSMS5();
delay(6000);
receberSMS();
break;
}
}
}
}

this code it send message "sensor register . To light LED send ON" to phone and after i dont send something to gsm it wiil continue send message"sensor register . To light LED send ON " :confused:

help me to make the code send message for one time only .....
for example : the soil is dry and it send message to phone and stop ... after i send something to gsm it continue the code ....

i want the code stay function but only the message send one time

please help me

Quote from: afiqzty on May 29, 2016, 11:38 pm

You are still a doofus.

CODE tags, NOT Quote tags.

To make a function/command only go once(unless otherwise instructed, I do this. That way you can set line = false; elsewhere, and the function is ready to print again.

boolean line = false

void loop(){

if (something && !line){
Serial.println("some message");
line = true;
}