Coding Question - Please Send Help

Hello internet,

This is my first ever post so tell me if I’m doing anything wrong. I’m a student programming a little piece and I genuinely have no clue what I am doing.

Evidently, I’m highly inexperienced with this, but I’ve written four individual codes but now I need to stick them all together into one, but that would mean eliminating ‘delays’ and a whole other rewrite… right?

Any tips, any help, ANYTHING, would be appreciated.

/ FLICKERING LED CODE
int ledPin1 = 10;
int ledPin2 = 9;
int ledPin3 = 11;

void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}

void loop() {
analogWrite(ledPin1, random(15)+100);
analogWrite(ledPin2, random(50)+200);
analogWrite(ledPin3, random(100)+300);
delay(random(100));
}


/ COLOUR CHANGING LED

const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int DISPLAY_TIME = 2;  // In milliseconds
 void setup()
{pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
 void loop()
{mainColors();
showSpectrum();
}
 void mainColors()
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
 delay(1000);
 // Red (turn just the red LED on):
 digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
 delay(1000);
 // Green (turn just the green LED on):
 digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
 delay(1000);
 // Blue (turn just the blue LED on):
 digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
 delay(1000);
 // Yellow (turn red and green on):
 digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
 delay(1000);
 // Cyan (turn green and blue on):
 digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
 delay(1000);
 // Purple (turn red and blue on):
 digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
// White (turn all the LEDs on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
}
void showSpectrum()
{
int x;  // define an integer variable called "x"
  
for (x = 0; x < 768; x++)
 
{
showRGB(x);  // Call RGBspectrum() with our new x
delay(DISPLAY_TIME);   // Delay for 10 ms (1/100th of a second)
}
}

void showRGB(int color)
{
int redIntensity;
int greenIntensity;
int blueIntensity;

if (color <= 255)          // zone 1
{
redIntensity = 255 - color;    // red goes from on to off
greenIntensity = color;        // green goes from off to on
blueIntensity = 0;             // blue is always off
}
else if (color <= 511)     // zone 2
{
redIntensity = 0;                     // red is always off
greenIntensity = 255 - (color - 256); // green on to off
blueIntensity = (color - 256);        // blue off to on
}
else // color >= 512       // zone 3
{
redIntensity = (color - 512);         // red off to on
greenIntensity = 0;                   // green is always off
blueIntensity = 255 - (color - 512);  // blue on to off
}

analogWrite(RED_PIN, redIntensity);
analogWrite(BLUE_PIN, blueIntensity);
analogWrite(GREEN_PIN, greenIntensity);
}


/ STEPPER MOTOR RANDOM

#include <Stepper.h>
Stepper stepper;
void setup()
{ 
}
void loop()
{
    if (stepper.distanceToGo() == 0)
    {
// Random change to speed, position and acceleration
// Make sure we dont get 0 speed or accelerations
delay(1000);
stepper.moveTo(random(15) % 200);
stepper.setMaxSpeed((random(10) % 200) + 1);
stepper.setAcceleration((random(5) % 200) + 1);
    }
    stepper.run();
}




/ STEPPER MOTOR ROTATION


#include <Stepper.h>
Stepper motor1(200, 9, 10);
void setup()
{
  motor1.setSpeed(60);
}
void loop()
{
  motor1.step(600);
  delay(500);
  motor1.step(-600);
  delay(500);
}

Thank you, Thank you, Thank you in advance :slight_smile:

Hi,

I'm new at this too, but from what I've read so far I'd look at using 'millis' which I understand is a timer that you read in milliseconds.

Basically you'd set a variable in each section to note the 'start time' and when the time reaches a set value in each section it would run that bit of code.

There are details in the reference guide: millis() - Arduino Reference

Regards
Peter

Yep, combining code that's not blocking is pretty easy. But the micro controller can only do a single thing at a time. Even if that single thing is being blocked (by for example delay() ). SO lesson learned, never use delay() again :wink: Unless for a quick and dirty test or when you really know you can get away with it (now AND in the future).

Hello again internet,

I’m back and still don’t know what I’m doing. I’m just wondering if anyone has any suggestions, tips, links, advice, ANYTHING for me to make my code less of absolute failure. I don’t know what I’m doing, but I can tell that this is not what it is supposed to look like.

Also, what variable, etc. am I supposed to be putting into the const int ledPin at the beginning?

//Constants
const int buttonPin = 1;     
const int ledPin =  ????;   

//Variables
int buttonState = 0;
int flag=0;

void setup() {
  //Input or output?
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT_PULLUP);   
}

void loop(){
  //Read button state (pressed or not pressed?)
  buttonState = digitalRead(buttonPin);

  //If button pressed...
  if (buttonState == LOW) { 
    //...ones, turn led on!
    if ( flag == 0){
      digitalWrite(ledPin, HIGH);
      flag=1; //change flag variable
    }
    //...twice, turn led off!


int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;

void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}

void loop() {
analogWrite(ledPin1, random(15)+100);
analogWrite(ledPin2, random(50)+200);
analogWrite(ledPin3, random(100)+300);
time = millis(random(100));
}



const int RED_PIN = 5;
const int GREEN_PIN = 6;
const int BLUE_PIN = 7;
int DISPLAY_TIME = 2;  // In milliseconds
 void setup()
{pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
 void loop()
{mainColors();
showSpectrum();
}
 void mainColors()
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
   time = millis(1000);
 // Red (turn just the red LED on):
 digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
   time = millis(1000);
 // Green (turn just the green LED on):
 digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
   time = millis(1000);
 // Blue (turn just the blue LED on):
 digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
   time = millis(1000);
 // Yellow (turn red and green on):
 digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
 delay(1000);
 // Cyan (turn green and blue on):
 digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
   time = millis(1000);
 // Purple (turn red and blue on):
 digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
   time = millis(1000);
// White (turn all the LEDs on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
   time = millis(1000);
}
void showSpectrum()
{
int x;  // define an integer variable called "x"
  
for (x = 0; x < 768; x++)
 
{
showRGB(x);  // Call RGBspectrum() with our new x
   time = millis(10);   // Delay for 10 ms (1/100th of a second)
}
}

void showRGB(int color)
{
int redIntensity;
int greenIntensity;
int blueIntensity;

if (color <= 255)          // zone 1
{
redIntensity = 255 - color;    // red goes from on to off
greenIntensity = color;        // green goes from off to on
blueIntensity = 0;             // blue is always off
}
else if (color <= 511)     // zone 2
{
redIntensity = 0;                     // red is always off
greenIntensity = 255 - (color - 256); // green on to off
blueIntensity = (color - 256);        // blue off to on
}
else // color >= 512       // zone 3
{
redIntensity = (color - 512);         // red off to on
greenIntensity = 0;                   // green is always off
blueIntensity = 255 - (color - 512);  // blue on to off
}

analogWrite(RED_PIN, redIntensity);
analogWrite(BLUE_PIN, blueIntensity);
analogWrite(GREEN_PIN, greenIntensity);
}



#include <Stepper.h>
Stepper stepper;
void setup()
{ 
}
void loop()
{
    if (stepper.distanceToGo() == 0)
    {
// Random change to speed, position and acceleration
// Make sure we dont get 0 speed or accelerations
   time = millis(1000);
stepper.moveTo(random(15) % 200);
stepper.setMaxSpeed((random(10) % 200) + 1);
stepper.setAcceleration((random(5) % 200) + 1);
    }
    stepper.run();
}



#include <Stepper.h>
Stepper motor1(200, 9, 10);
void setup()
{
  motor1.setSpeed(60);
}
void loop()
{
  motor1.step(600);
   time = millis(500);;
  motor1.step(-600);
   time = millis(500);
}



    else if ( flag == 1){
      digitalWrite(ledPin, LOW);
      flag=0; //change flag variable again 
    }    
  }
  delay(200); //Small delay

}

Thanks in advance ^-^

for output/input pin(s) definition, use

const byte

// like const byte led = 7; 
[/code[

I'm suggesting to the Moderator to merge this with your other Thread - to which you never responded.

What have you learned from the earlier Replies?

...R

Also note only one setup function and one loop function.

Them’s the rules.

Okay, that’s probably a good idea, to merge it.

What have I learnt? Well, I have learnt that delays can’t be used in the codes when there are multiple code segments involved, so one must use millis. And that I have to set a variable at the beginning of each section to establish the ‘start time’ so the code knows when to run each section, but as everything is supposed to be running at once I’m not sure if this is still necessary…?

Also, that all of the constants need to be defined at the beginning of the code and that the setup can only occur once.

Finally, I’ve tried to make the code activated by a button, so there’s that too.

// constants won't change
const int buttonPin = 1; // button pin

const int ledPin1 = 2; // flickering led
const int ledPin2 = 3;
const int ledPin3 = 4;

const int RED_PIN = 5; // colour changing led
const int GREEN_PIN = 6;
const int BLUE_PIN = 7;
int DISPLAY_TIME = 2;  // In milliseconds

#include <Stepper.h>

stepper2.setSpeed(60);

// variables will change
int buttonState = 0;        

void setup() {
 {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}

{
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}


}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);




void loop() {
analogWrite(ledPin1, random(15)+100);
analogWrite(ledPin2, random(50)+200);
analogWrite(ledPin3, random(100)+300);
time = millis(random(100));
}

{mainColors();
showSpectrum();
}
 void mainColors()
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
   time = millis(1000);
 // Red (turn just the red LED on):
 digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
   time = millis(1000);
 // Green (turn just the green LED on):
 digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
   time = millis(1000);
 // Blue (turn just the blue LED on):
 digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
   time = millis(1000);
 // Yellow (turn red and green on):
 digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
 delay(1000);
 // Cyan (turn green and blue on):
 digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
   time = millis(1000);
 // Purple (turn red and blue on):
 digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
   time = millis(1000);
// White (turn all the LEDs on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
   time = millis(1000);
}
void showSpectrum()
{
int x;  // define an integer variable called "x"
  
for (x = 0; x < 768; x++)
 
{
showRGB(x);  // Call RGBspectrum() with our new x
   time = millis(10);   // Delay for 10 ms (1/100th of a second)
}
}

void showRGB(int color)
{
int redIntensity;
int greenIntensity;
int blueIntensity;

if (color <= 255)          // zone 1
{
redIntensity = 255 - color;    // red goes from on to off
greenIntensity = color;        // green goes from off to on
blueIntensity = 0;             // blue is always off
}
else if (color <= 511)     // zone 2
{
redIntensity = 0;                     // red is always off
greenIntensity = 255 - (color - 256); // green on to off
blueIntensity = (color - 256);        // blue off to on
}
else // color >= 512       // zone 3
{
redIntensity = (color - 512);         // red off to on
greenIntensity = 0;                   // green is always off
blueIntensity = 255 - (color - 512);  // blue on to off
}

analogWrite(RED_PIN, redIntensity);
analogWrite(BLUE_PIN, blueIntensity);
analogWrite(GREEN_PIN, greenIntensity);
}



#include <Stepper.h>
Stepper stepper1;

{
    if (stepper1.distanceToGo() == 0)
    {
// Random change to speed, position and acceleration
// Make sure we dont get 0 speed or accelerations
   time = millis(1000);
stepper1.moveTo(random(15) % 200);
stepper1.setMaxSpeed((random(10) % 200) + 1);
stepper1.setAcceleration((random(5) % 200) + 1);
    }
    stepper1.run();
}



#include <Stepper.h>
Stepper stepper2(200, 9, 10);

{
  stepper2.step(600);
   time = millis(500);;
  stepper2.step(-600);
   time = millis(500);
}


  } else {
    // turn off
ledPin1 = 0; // flickering led
ledPin2 = 0;
ledPin3 = 0;

 RED_PIN = 0; // colour changing led
 GREEN_PIN = 0;
 BLUE_PIN = 0;
DISPLAY_TIME = 0;  

  }
}
 time=millis(200); //Small delay

}

Thank you

sketch_nov10a:45: error: a function-definition is not allowed here before '{' token

Also what is this error? Why does it do this? Why isn't a function-definition allowed before a '{' token?

You have tried to define some functions inside loop. Worse, one of them is called loop too. Move them outside. to be first class functions on their own.

void Loop still shows more than once. And the {} connected with that are misplaced or absent.
And it is customary to put declarations and includes at the top, before SetUp.

@InkyKitten

TOPIC MERGED.

Please READ THIS POST to help you get the best out of the forum.

Bob.

What do you expect this line to do?

time = millis(random(100));

millis() doesn’t take parameters, see man page:

Syntax
time = millis()

Parameters
None

Returns
Number of milliseconds passed since the program started. Data type: unsigned long.

Hi,

as a Newbie here too I would copy that code to a notepad file then add it to the IDE in small sections at a time and test that each part works OK without breaking earlier code.

That way I'd build up to the full sketch and be clearing problems in the last bit that I copied before adding the next section.

I'd also have a flowchart or at least a full written list of what I was trying to do, to try to keep the 'logic flow' in order.

Peter

Thank you everyone, I will update with my improved code soon.