DISABLING VOIDS IN ARDUINO - arduino

Ok, basically I'm trying to enable and disable voids in the code. Here is the code:
what I'm trying to create here is a dimmer. I want to enable and disable some of the voids to create that. Also when the value of "brightness" changes in one of the "for" loops, I can't transfer that value to other loops. Please help
int sensorPin = A0;
int sensorValue = 0;
int LED = 10;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
if ( analogRead < 30){
Down() = false;
}
if ( analogRead > 31) {
Up() = false;
}
}
void Up () {
for ( int brightness = 0; sensorValue < 30; brightness = brightness + 5 ) {
analogWrite(LED, brightness);
delay(100);
}
}
void Down () {
for ( int brightness = brightness; sensorValue > 31; brightness = brightness - 5 ) {
analogWrite(LED, brightness);
delay(100);
}
}

Still does not know what you want do.
But if I undestand it right (from the comments), the code should be:
int sensorPin = A0;
int sensorValue = 0;
uint8_t lightBrightness = 0;
int LED = 10;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
// Serial.println(sensorValue);
if ( sensorValue < 30){
if(lightBrightness < 255) lightBrightness++;
analogWrite(LED, lightBrightness );
}
if ( sensorValue > 31) {
if(lightBrightness > 0) lightBrightness--;
analogWrite(LED, lightBrightness );
}
delay(100); // << just to see fading the light
}

Related

AttachIntrerrupt stops NRF24L01+ from working - ARDUINO

If I remove attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING); The code works. But once its added, the radio.available doesnt let anything under it run.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
struct InputData // define stuct
{
int x;
int y;
};
InputData data;
// Motor A connections
int motor_enA = 9;
int motor_in1 = 10;
int motor_in2 = 6;
int encoder1 = 2;
int encoder2 = 3;
int counter = 0;
int angle = 0;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
// Set all the motor control pins to outputs
pinMode(motor_enA, OUTPUT);
pinMode(motor_in1, OUTPUT);
pinMode(motor_in2, OUTPUT);
// Turn off motors - Initial state
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
analogWrite(motor_enA, 255);
pinMode (encoder1, INPUT);
pinMode (encoder2, INPUT);
attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING);
}
void loop() {
readEncoder();
if (radio.available()) {
radio.read(&data, sizeof(data));
// Serial.println(data.y);
if (data.y > 5) {
digitalWrite(motor_in1, HIGH);
digitalWrite(motor_in2, LOW);
}
else if (data.y < -5) {
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, HIGH);
}
else {
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
}
}
if(counter>1){
counter=0;
angle+=2;
}else if(counter<-1){
counter=0;
angle-=2;
}
Serial.print("Position: ");
Serial.println(angle);
}
void readEncoder()
{
if(digitalRead(encoder1)==HIGH){
int b = digitalRead(encoder2);
if(b>0){
counter++;
}
else{
counter--;
}
}
}
I have tried removing and adding the line, as described above^^
as mentioned by Hcheung, make counter volatile and remove readEncoder(); from loop.
I simplify a bit ISR readEncoder();
volatile int counter = 0;
[....]
void readEncoder() {
//if(digitalRead(encoder1)==HIGH){ //we are precisely here because digitalRead(encoder1) = HIGH !
if(digitalRead(encoder2)) counter++;
else counter--;
}

why my audio sound is not playing in my arduino code but if i play it separately it started working good

I am trying to set the timer after which the audio will automatically played once but it is not playing anything just a noise but when i run the audio program separately it work perfectly good
This is my code please help me out where i am doing wrong.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <TMRpcm.h>
#include <SPI.h>
int timer1_counter;
#define SD_ChipSelectPin 4
TMRpcm tmrpcm;
unsigned long time_now = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ledPin = 13;
const byte interruptPin1 = 2;
const byte interruptPin2 = 3;
int counter2 = 0;
volatile byte state = LOW;
int count = 0;
int limit = 0;
bool TimerFlag = false;
int deviceTime = 0;
int set = 5;
bool soundplayflag = false;
void setup()
{
lcd.begin();
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(interruptPin1, INPUT_PULLUP);
pinMode(interruptPin2, INPUT_PULLUP);
pinMode(set, INPUT_PULLUP);
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Please Select:");
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 31250; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
// initialize timer1
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
if (soundplayflag == true)
{
counter2 = counter2+1;
Serial.println(counter2/2);
}
}
void loop()
{
//unsigned long currentMillis = millis();
int up = digitalRead(interruptPin1);
int down = digitalRead(interruptPin2);
int setbutton = digitalRead(set);
delay(230);
if (up == 0)
{
Serial.println("Entering up");
count++;
if (count >= 0) {
lcdprint(count);
}
}
else if (down == 0)
{
Serial.println("Entering down");
count--;
if (count >= 0) {
lcdprint(count);
}
}
else if (setbutton == 0)
{
Serial.println("Entering set");
soundplayflag=true;
deviceTime = count;
}
if (deviceTime > 0)
{
if (deviceTime == counter2)
{
soundplayflag=false;
Serial.println("Hello world");
PlaySound();
counter2 = 0;
}
}
}
int lcdprint(int a)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Please Select:");
lcd.setCursor (7,1);
lcd.print(a);
Serial.println(a);
lcd.setCursor (10,1);
lcd.print("Min");
}// end of lcdprint(int a)
void PlaySound()
{
for (int i = 0; i < 10; i++)
{
tmrpcm.setVolume(5);
tmrpcm.play("3.wav");
delay(1000);
}// end of for loop
}// end of void PlaySound()
my expected output is it should play the sound when i set the time
It seems TMRpcm uses Timer1, which conflicts with your TIMER1_COMPA_vect. That would explain why it's working if you run nothing else.
Maybe try to use #define USE_TIMER2?

Arduino Combination Button?

I am trying to set up an Arduino Uno plus a RelayShield along with 6 push buttons(standard), and 3 LED's(red, yellow, green). Now what I have code to do is to take in a button push combination that is 6 pushes long(order only matters for what is set in the array). I have everything wired up to a breadboard and all that other jazz. My problem is, none of the LED's are working(I followed detailed instructions from instructables - minus the code), and it doesn't matter how many button pushes there are, only button 3 triggers the relay.... Now, for simplicities sake, I didn't wire up all 6 buttons, and I just shortened the array down to 3 and adjusted accordingly(I didn't want to hook up 6 buttons right now). Can someone verify this code for me? Or tell me what is wrong with it? Thanks in advance!
//button pins
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
const int button5 = 6;
const int button6 = 7;
const int grnLed = 9;
const int redLed = 10;
const int yellowLed = 11;
const int openRelay = 8;
// how long is our code, kinda personal
const int codelen = 3;
//pin code values must match button pin values
char PIN[codelen] = {
'1', '2', '3'
};
// attempted combo
char attempt[codelen] = {
'0', '0', '0'
};
// attempt count
int z = 0;
void setup() {
// you've been setup
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(button6, INPUT);
pinMode(openRelay, OUTPUT);
// set pullup resistor for buttons
digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
digitalWrite(button4, HIGH);
digitalWrite(button5, HIGH);
digitalWrite(button6, HIGH);
// set openRelay state to open or closed
digitalWrite(openRelay, LOW);
}
void correctPIN()
{
pulseLED(grnLed, 3000);
digitalWrite(openRelay, HIGH);
delay(2000);
digitalWrite(openRelay, LOW);
delay(2000);
z = 0;
}
void incorrectPIN()
{
pulseLED(redLed, 3000);
z = 0;
}
void checkPIN()
{
int correct = 0;
int i;
for (i = 0; i < codelen; i++)
{
if (attempt[i] == PIN[i])
{
correct++;
}
}
if (correct == codelen)
{
correctPIN();
}
else
{
incorrectPIN();
}
for (int zz = 0; zz < codelen; zz++)
{
attempt[zz] = '0';
}
}
void checkButton(int button){
if (digitalRead(button) == LOW)
{
while (digitalRead(button) == LOW) { } // do nothing
//convert int to string for tracking/compare
char buttStr = button + '0';
attempt[z] = buttStr;
z++;
//light up led so we know btn press worked
pulseLED(yellowLed, 500);
}
}
void pulseLED(int ledpin, int msec) {
digitalWrite(ledpin, HIGH);
delay(msec);
digitalWrite(ledpin, LOW);
}
void loop() {
// check buttons
checkButton(button1);
checkButton(button2);
checkButton(button3);
checkButton(button4);
checkButton(button5);
checkButton(button6);
//if number of buttons pressed, z, matches code/pin length then check
if (z >= codelen)
{
checkPIN();
}
}

invalid conversion from 'int*' to 'uint8_t

im having a problem, i just bought an arduino and i was wondering if anyone could help, here is my code. (i am just trying to get two leds to fade in and out).
int ledCount = 2;
int ledPins [ ] = {11,12 };
int brightness = 0;
int delayTime = 10;
void setup() {
pinMode(ledPins, OUTPUT);
}
void loop() {
while(brightness < 255)
{
analogWrite(ledPins, brightness);
delay(delayTime);
brightness = brightness + 1;
}
while(brightness > 0)
{
analogWrite(ledPins, brightness);
delay(delayTime);
brightness = brightness - 1;
}
}
You are passing an array to analogWrite or pinMode, where it is expecting a uint8_t.
Arduino pin manipulation functions will only handle a single pin at a time. There are ways around that, by directly manipulating the AVR/ARM GPIO registers, but those can be finicky (not recommended for use unless you really need speed).
The reason it says int * is because under the hood, arrays in C/C++ are represented as pointers.
If you want to analogWrite or pinMode to both LEDs, you will have to call the function once for each LED. Example:
analogWrite(ledPins[0], brightness);
analogWrite(ledPins[1], brightness);
Or
for(int currentLED = 0;currentLED < ledCount;i++){
analogWrite(ledPins[currentLED], brightness);
}
In the context of your program:
int ledCount = 2;
int ledPins [] = {11, 12};
int brightness = 0;
int delayTime = 10;
#define INCREASE 1
#define DECREASE 2
int brightness_change = INCREASE;
void setup(){
for(int i = 0;i < ledCount;i++){
pinMode(i, OUTPUT);
}
}
void loop(){
while(brightness < 255 && brightness_change == INCREASE){
brightness = brightness + 1;
}
while(brightness > 0 && brightness_change == DECREASE){
brightness = brightness - 1;
}
if(brightness == 255){
brightness_change = DECREASE;
}
if(brightness == 0){
brightness_change = INCREASE;
}
for(int current_led = 0;current_led < ledCount;current_led++){
analogWrite(current_led, brightness;
}
delay(delayTime;
}
Not tested, but it should work.

Arduino Switch and LED dimmer code

I have to make a program where a switch causes an led to change brightness each time the switch is clicked. The code below causes the LED to change brightness as needed but it does not stop changing if the switch is held down. I need it to only change once each time the switch is clicked. How do I make it change once when clicked and not loop through the other brightnesses?
const int ledPin = 10;
int brightness = 0;
boolean up = 1;
int steps = 0;
int inpin = 2;
int reading;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode (inpin, INPUT);
}
void loop() {
do{
reading = digitalRead(inpin);
}while (reading != HIGH);
analogWrite(ledPin, brightness);
delay(1000);
if (up)
{
brightness = brightness + 63;
}
else {
brightness = brightness - 63;
}
steps = steps + 1;
if (steps == 4)
{
up = !up;
steps = 0;
}
}
Once the brightness has been changed, wait for the switch to be released.

Resources