modify the programming to carry out the amplification of the circuit in Arduino
ID: 2085695 • Letter: M
Question
modify the programming to carry out the amplification of the circuit in Arduino 1
const int led1 = 13;
const int led2 = 10;
const int led3 = 7;
int lectura;
void setup() {
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
}
void loop() {
lectura=analogRead(A0);
if (lectura<50){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
}
else if (lectura<200){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,LOW);
}
else if (lectura<400){
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
}
else {
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
}
delay(500);
}
Extension:
Perform a circuit under the following characteristics:
• Light at maximum intensity, turn on the three LED's from left to right, with a
warning tone of 1s.
• Light at medium intensity, the three LEDs turn on, with a tone of different sound with respect to the
first, with a duration of 500 ms.
• Low intensity light, sequence all three LEDs from right to left, with one tone
of warning of 250 ms and must be different with respect to the previous cases.
Explanation / Answer
// 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();
}
(or)
void setup() {
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(11, LOW);
delay(1000);
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
void setup() {
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void blink (const byte which)
{
digitalWrite(which, HIGH);
delay(1000);
digitalWrite(which, LOW);
delay(1000);
} // end of blink
void loop() {
blink (11);
blink (12);
blink (13);
}
void setup() {
for (int i = 11; i <= 13; i++)
pinMode(i, OUTPUT);
}
void blink (const byte which)
{
digitalWrite(which, HIGH);
delay(1000);
digitalWrite(which, LOW);
delay(1000);
} // end of blink
void loop() {
for (int i = 11; i <= 13; i++)
blink (i);
}
There is a whole world of stuff to learn. Enjoy the process!
shareimprove this answer
answered Sep 4 '15 at 21:28
Nick Gammon
21.9k52978
thnksssssssss :) – user3099225 Sep 7 '15 at 11:17
add a comment
up vote 0 down vote
Have you considered connecting another LED and turning one on and the other off sequentially?
void setup() {
pinMode(13, OUTPUT);
pinmode(NewLEDPin, OUTPUT);
}
void loop() {
digitalWrite(NewLEDPin,LOW);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(NewLEDPin,HIGH);
digitalWrite(13, LOW);
delay(1000);
}
or
// Loops is 1 on the first call.
digitalWrite(13, LOW);
Loops = Loops + 1;
// Loops is now 2
if (Loops < 3)
{
// So, we enter here...
digitalWrite(13, HIGH);
delay(2000);
}
else
{
// but not here
digitalWrite(13, LOW);
exit(0);
}
// Next call:
// Turn off the light.
digitalWrite(13, LOW);
Loops = Loops + 1;
// Loops is now 3
if (Loops < 3)
{
// So we don't enter here
digitalWrite(13, HIGH);
delay(2000);
}
else
{
// but we enter here
digitalWrite(13, LOW);
// Which exits
exit(0);
}
So you turn on the LED once, then turn it off and exit.
If you adjust the loop counter, you will turn off the LED and then immediately turn it on again, which will just look like it's on for a longer period.
You probably want to do a whole on/off cycle on each loop - something like this:
int LedPin = 13;
int Loops = 0;
void setup() {
pinMode(LedPin, OUTPUT);
digitalWrite(LedPin, LOW);
}
void loop() {
Loops = Loops + 1;
if (Loops <= 3)
{
digitalWrite(LedPin, HIGH);
delay(2000);
digitalWrite(LedPin, LOW);
delay(2000);
}
else
{
exit(0);
}
}
or
//LED Pin Variables
int ledPins[] = {2,3,4,5,6,7,8,9}; //An array to hold the pin each LED is connected to
//i.e. LED #0 is connected to pin 2, LED #1, 3 and so on
//to address an array use ledPins[0] this would equal 2
//and ledPins[7] would equal 9
int lightModulo = 0; // for which modulo to light
/*
* setup() - this function runs once when you turn your Arduino on
* We the three control pins to outputs
*/
void setup()
{
//Set each pin connected to an LED to output mode (pulling high (on) or low (off)
for(int i = 0; i < 8; i++){ //this is a loop and will repeat eight times
pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output
} //the code this replaces is below
Serial.begin(9600);
}
/*
* loop() - this function will start after setup finishes and then repeat
* we call a function called oneAfterAnother(). if you would like a different behaviour
* uncomment (delete the two slashes) one of the other lines
*/
void loop() // run over and over again
{
turnOnEvenOdd();
}
// turn on even LEDs, then odd LEDs.
// nice to have them set up as yellow/red
void turnOnEvenOdd() {
int delayTime = 250;
// print desired modulo.
Serial.println(lightModulo);
for (int i = 0; i < 8; i++) {
if ((i % 2) == lightModulo) {
// on
// when desired modulo is 1 (odd), odds will light
// when desired modulo is 0 (even), evens will light
digitalWrite(ledPins[i], HIGH);
} else {
// off
// ...and the others will be turned off
digitalWrite(ledPins[i], LOW);
}
}
// switch lightModulo to the other light
lightModulo--; // decrease. 1 turns to 0, 0 turns to -1
lightModulo = abs(lightModulo); // when it's -1 (from 0), abs to 1
dela
y(delayTime);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.