0% found this document useful (0 votes)
34 views

Sample Setting Up Two or More Void

The document defines constants for pin assignments and sets up multiple pins as outputs in the setup function. It then defines multiple void loops to control lights, motors, and scheduling of tasks. The loops control lights with a blinking pattern, control motor direction based on pin states, and allow scheduling of tasks to blink LEDs at different rates and respond to serial commands.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Sample Setting Up Two or More Void

The document defines constants for pin assignments and sets up multiple pins as outputs in the setup function. It then defines multiple void loops to control lights, motors, and scheduling of tasks. The loops control lights with a blinking pattern, control motor direction based on pin states, and allow scheduling of tasks to blink LEDs at different rates and respond to serial commands.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Sample setting up two or more void

const int red1 = 10;


const int red2 = 11;
const int blue1 = 12;
const int blue2 = 13;
const int front1 = 3;
const int front2 = 4;
const int back1 = 5;
const int back2 = 6;
const int enablePWMPin = 9;
const int controlPinA = 8;
const int controlPinB = 7;
int buzzer = A0;

void setup() {
pinMode(red1, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(blue1, OUTPUT);
pinMode(blue2, OUTPUT);
pinMode(front1, OUTPUT);
pinMode(front2, OUTPUT);
pinMode(back1, OUTPUT);
pinMode(back2, OUTPUT);
pinMode(enablePWMPin, OUTPUT);
pinMode(controlPinA, OUTPUT);
pinMode(controlPinB, OUTPUT);

digitalWrite(enablePWMPin, LOW);
digitalWrite(front1, LOW);
digitalWrite(front2, LOW);
digitalWrite(back1, LOW);
digitalWrite(back2, LOW);
digitalWrite(red1, LOW);
digitalWrite(red2, LOW);
digitalWrite(blue1, LOW);
digitalWrite(blue2, LOW);
noTone(buzzer);
}

void loop() {
digitalWrite(enablePWMPin, HIGH);
digitalWrite(controlPinA, HIGH);
digitalWrite(controlPinB, LOW);
delay(3000);
digitalWrite(controlPinA, LOW);
digitalWrite(controlPinB, HIGH);
delay(3000);
}

void loop() {
digitalWrite(red1, HIGH);
digitalWrite(red2, HIGH);
digitalWrite(blue1, LOW);
digitalWrite(blue2, LOW);
delay(250);
digitalWrite(red1, LOW);
digitalWrite(red2, LOW);
digitalWrite(blue1, HIGH);
digitalWrite(blue2, HIGH);
delay(250);
}

void loop() {
int directionA = digitalRead(controlPinA);
int directionB = digitalRead(controlPinB);
if (directionA == HIGH && directionB == LOW) {
digitalWrite(front1, HIGH);
digitalWrite(front2, HIGH);
digitalWrite(back1, LOW);
digitalWrite(back2, LOW);
} else if (directionA == LOW && directionB == HIGH) {
digitalWrite(back1, HIGH);
digitalWrite(back2, HIGH);
digitalWrite(front1, LOW);
digitalWrite(front2, LOW);
} else {
digitalWrite(front1, LOW);
digitalWrite(front2, LOW);
digitalWrite(back1, LOW);
digitalWrite(back2, LOW);
}
}

void loop() {

lights1();
lights2();
motor();

void lights1() {

static unsigned long lastTime = 0;


const long interval = 3000;
static bool state = 0;

unsigned long now = millis();

if ( now - lastTime > interval && state == 0) {


state = 1;
lastTime = now;
digitalWrite(enablePWMPin, HIGH);
digitalWrite(controlPinA, HIGH);
digitalWrite(controlPinB, LOW);
}

if ( now - lastTime > interval && state == 1) {


state = 0;
lastTime = now;
digitalWrite(controlPinA, LOW);
digitalWrite(controlPinB, HIGH);
}
}

void lights2() {

static unsigned long lastTime = 0;


const long interval = 250;
static bool state = 0;

unsigned long now = millis();

if ( now - lastTime > interval && state == 0) {


state = 1;
lastTime = now;
digitalWrite(red1, HIGH);
digitalWrite(red2, HIGH);
digitalWrite(blue1, LOW);
digitalWrite(blue2, LOW);
}

if ( now - lastTime > interval && state == 1) {


state = 0;
lastTime = now;
digitalWrite(red1, LOW);
digitalWrite(red2, LOW);
digitalWrite(blue1, HIGH);
digitalWrite(blue2, HIGH);
}

void motor() {
int directionA = digitalRead(controlPinA);
int directionB = digitalRead(controlPinB);
if (directionA == HIGH && directionB == LOW) {
digitalWrite(front1, HIGH);
digitalWrite(front2, HIGH);
digitalWrite(back1, LOW);
digitalWrite(back2, LOW);
} else if (directionA == LOW && directionB == HIGH) {
digitalWrite(back1, HIGH);
digitalWrite(back2, HIGH);
digitalWrite(front1, LOW);
digitalWrite(front2, LOW);
} else {
digitalWrite(front1, LOW);
digitalWrite(front2, LOW);
digitalWrite(back1, LOW);
digitalWrite(back2, LOW);
}
}
// Include Scheduler since we want to manage multiple tasks.
#include <Scheduler.h>

int led1 = 13;


int led2 = 12;
int led3 = 11;

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

// Setup the 3 pins as OUTPUT


pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);

// Add "loop2" and "loop3" to scheduling.


// "loop" is always started by default.
Scheduler.startLoop(loop2);
Scheduler.startLoop(loop3);
}

// Task no.1: blink LED with 1 second delay.


void loop() {
digitalWrite(led1, HIGH);

// IMPORTANT:
// When multiple tasks are running 'delay' passes control to
// other tasks while waiting and guarantees they get executed.
delay(1000);

digitalWrite(led1, LOW);
delay(1000);
}

// Task no.2: blink LED with 0.1 second delay.


void loop2() {
digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led2, LOW);
delay(100);
}

// Task no.3: accept commands from Serial port


// '0' turns off LED
// '1' turns on LED
void loop3() {
if (Serial.available()) {
char c = Serial.read();
if (c=='0') {
digitalWrite(led3, LOW);
Serial.println("Led turned off!");
}
if (c=='1') {
digitalWrite(led3, HIGH);
Serial.println("Led turned on!");
}
}
// IMPORTANT:
// We must call 'yield' at a regular basis to pass
// control to other tasks.
yield();
}

You might also like