Try this. Revised from post #6 now that I'm home and have access to my PC and electronics bench. Read all the notes, take the time to understand what's happening. Any questions, ask.
Note that all the pins on the Arduino have been moved around for various reasons (assuming you were using an Uno R3). Again, see the notes.
/*
====== GENERAL NOTE ==============
I moved all the pins around and changed some stuff up
so you can learn. Most of the changes are commonly
accepted conventions in Arduino and their basis is well
founded and for good reason I won't go into here.
A few of the changes are stylistic changes I made since
that's how I roll.
The reason for the pin changes explained below.
====== NOTE ON INTERRUPTS =======
The arduino Uno R3, often folks' first introductory board into
Arduino, has two hardware interrupts: vector 0 and 1.
Vector 0 corresponds to digital pin 2 and vector 1 corresponds
to digital pin 3.
Therefore, in its simplest use, these pins should not be shared
with other devices.
That is why I moved the global pin definitions below around.
Don't share pins unless you really know what you're doing.
======= NOTE ON SERIAL =========
Serial is a necessary tool in Arduino projects.
It is typically used for code debugging and also
great for general info to the designer or user.
In void setup(), I added the line "Serial.println("myProject_V1");"
to illustrate this. As your code grows and changes, it's useful
to have the Arduino tell you what sketch it's running,
so replace the text in this line with whatever your sketch is called.
You should also use Serial.println("someVal = ");Serial.println(someValue);
often throughout your code as you develop it. In fact, every time something
changes, if you do this, when things don't work as expected (and they won't)
it will really save a lot of time and headache trying to narrow down
the issue. Trust me, it's 99% user error, not the Arduino, causing
the issues. Ask me how I know that...
======== HARDWARE AND TEST RESULTS ========
Tested on Arduino Mega, will work the same on Uno R3.
Tested with the following hardware:
Potentiometer; led; pushbutton with no external resistors;
and Elegoo remote communicating with IR Receiver model TSOP38238.
Motors were not used in testing, led used instead to simulate speed control
By Hallowed31
2024-11-22
*/
#include <IRremote.hpp> // libraries are conventionally at the top of a sketch
const int analogSensor = A0; // you should name your devices
const int interruptButton = 2;
const int IR_RECEIVE_PIN = 7; // byte isn't wrong but do you why it was byte?
const int led = 11; // PWM pin to allow brightness control
const int enable = 10; //Motor
const int motor2 = 9;
const int motor1 = 6; // pins 10, 9 and 6 are also PWM capable
volatile byte state = 0; // thanks to Delta_G for the assist
unsigned long currentMillis; // unsigned long because you don't want the clock to run out
unsigned long previousMillis; // store last time we checked millis() free running clock
const int delayTime = 1000; // const because we always want the delay this long
int Value = 0;
int Brightness = 0;
int LedBrightness = 0;
int Freq = 0;
void hardwareInterrupt() { // Interrupt service routine (ISR) for a hardware event
state++;
if (state >= 3) {
state = 0;
}
}
void setup() {
Serial.begin(9600);
Serial.println("myProject_V1"); // see note
Serial.println(); // blank line to readability
delay(1000); // good use of blocking delay - a little time to read this
pinMode(analogSensor, INPUT);
pinMode(interruptButton, INPUT_PULLUP); // uses Uno internal resistor but logic reversed
pinMode(IR_RECEIVE_PIN, INPUT); // use the name you gave it, not the pin number
pinMode(enable, OUTPUT);
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
attachInterrupt(digitalPinToInterrupt(interruptButton), hardwareInterrupt, FALLING);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); //activate remote
}
void loop() {
noInterrupts();
if (IrReceiver.decode()) {
if (IrReceiver.decodedIRData.command == 0x45) {
Serial.println("Power"); // power button
Serial.println();
delay(250); // short 1/4 second delay to read...
state = 1;
} else if (IrReceiver.decodedIRData.command == 0x40) {
Serial.println("Play"); // play button
Serial.println();
delay(250); // ...and also limit extra button press signals
state = 2;
} else if (IrReceiver.decodedIRData.command == 0x16) {
Serial.println("Resume Default"); // button 0
Serial.println();
delay(250);
state = 0;
}
IrReceiver.resume();
}
interrupts();
switch (state) {
case 0:
defaultOps();
break;
case 1:
digitalWrite(enable, LOW);
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
digitalWrite(led, LOW);
break;
case 2:
digitalWrite(enable, HIGH);
digitalWrite(motor1, HIGH);
digitalWrite(motor2, HIGH);
digitalWrite(led, HIGH);
break;
default:
defaultOps();
break;
}
}
void defaultOps() {
// interesting you're using "brightness" to
// control the speed of a motor.
Value = analogRead(analogSensor);
Brightness = map(Value, 0, 1023, 0, 100); //Analog to Brightness
LedBrightness = map(Value, 0, 1023, 0, 127); // from 0 - half bright
analogWrite(led, LedBrightness);
// start our named (currentMillis), built in, free running, no blocking timer (millis() function)
currentMillis = millis();
if (currentMillis - previousMillis > delayTime) {
previousMillis = currentMillis;
Serial.print("Brightness: ");
Serial.print(Brightness); //Print Brightness - how do you know? ^
Serial.print("\tLedBrightness: "); // the \t is a control character, tab right
Serial.println(LedBrightness); //Print Brightness - how do you know? ^
Serial.println(); // blank line
// delay(1000); // this is a long blocking delay that blocks hardware events. Not ideal.
}
Freq = map(Brightness, 0, 100, 0, 950) + 50; //Brightness controlling fan
if (Brightness <= 50) {
digitalWrite(enable, HIGH);
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
} else (digitalWrite(enable, LOW));
}