Hi everybody
I got a huge problem concerning my school project.
i am controlling an ROV using a arduino Mega, whose purpose is to control 3 L298N hooked up to 4 DC engines and 2 halogen light bulbs.
The L298N are activated by momentary switches. 9, where 8 are to the motors and 1 are for the lights. they are connected on digital inputs 36-44
The speed of which the motors are going to be runing on are determined by two 100K potentiometers,one for light one for motorspeed , their inputs are connected to A0-A1. where the inputs are beeing read. those values are mapped (0 - 1023 converted to 0 - 255) and so they can be sent as an output.
a visual aid is added for the user to se how high, or low, the value is. this is done by two rows of seven LED's. they are controlled on output 22-35. when the value is at a certain point a ''high'' signal is sent to an ULN2003A darlington transistor array, which turns the LED's on.
to help you understand how i control this i have drawn a simple sketch, showing the hookup
The switches are connected with a pulldown resistor, so the instead of a floating value the value is pulled down to zero.
this hookup is maid on a PCB, and testet working, each switch, pot and LED is testet working propper. the dual H-bridges are also working fine.
THE PROBLEM IS THE CODE
i can get the led's flashing when the pots are at a certain value, this is done by a series of if statements.
now my thoughts were i could do the same with controling the engines, i just had to enable the circuit on the L298N and send a PWM signal to input 1 or 2 on it.
the problem is, it will only work if there is only one if statement in the code when concerning the L298N (since the LEDs and darlington arrays are working perfectly).
for an example. pushing the ''forward'' switch activates the two engines pushing forward. no problem their.
but when i write a new if statement for, lets say reverse, the DC motor won't respond. and yes, the l298N boards are tested and working.
and then i can't figure out a code for the light so when the switch is pushed the lights stay on, until the switch is pushed again the lights go off. this i find tricky to do, since i can't work out a proper code that will handle the debounce of the switch, and making it read the analog value from the pot when activated at the same time.
so i am wondering if it is the code, i could really need some adivce on maybe another command i could use to easen this.
i am fairly new to this arduino programming so feel free to comment and tell me if i am totally of course using on if statements.
const int analogLightPin = A0; // pin that the sensor is attached to
const int analogMotorPin = A1;
void setup() {
// initialize the LED pin as an output:
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(28, OUTPUT);
pinMode(29, OUTPUT);
pinMode(30, OUTPUT);
pinMode(31, OUTPUT);
pinMode(32, OUTPUT);
pinMode(33, OUTPUT);
pinMode(34, OUTPUT);
pinMode(35, OUTPUT);
pinMode(36, INPUT);
pinMode(37, INPUT);
pinMode(38, INPUT);
pinMode(39, INPUT);
pinMode(40, INPUT);
pinMode(41, INPUT);
pinMode(42, INPUT);
pinMode(43, INPUT);
pinMode(44, INPUT);
// initialize serial communications:
Serial.begin(9600);
}
void loop() {
// read the value of the potentiometer:
int analogMotorValue = analogRead(analogMotorPin);
int analogLightValue = analogRead(analogLightPin);
if (analogLightValue > 170) {
digitalWrite(22, HIGH);
}
else {
digitalWrite(22, LOW);
}
if (analogLightValue > 340) {
digitalWrite(23, HIGH);
}
else {
digitalWrite(23, LOW);
}
if (analogLightValue > 510) {
digitalWrite(24, HIGH);
}
else {
digitalWrite(24, LOW);
}
if (analogLightValue > 680) {
digitalWrite(25, HIGH);
}
else {
digitalWrite(25, LOW);
}
if (analogLightValue > 850) {
digitalWrite(26, HIGH);
}
else {
digitalWrite(26, LOW);
}
if (analogLightValue > 900) {
digitalWrite(27, HIGH);
}
else {
digitalWrite(27, LOW);
}
if (analogLightValue > 1000) {
digitalWrite(28, HIGH);
}
else {
digitalWrite(28, LOW);
}
if (analogMotorValue > 170) {
digitalWrite(29, HIGH);
}
else {
digitalWrite(29, LOW);
}
if (analogMotorValue > 340) {
digitalWrite(30, HIGH);
}
else {
digitalWrite(30, LOW);
}
if (analogMotorValue > 510) {
digitalWrite(31, HIGH);
}
else {
digitalWrite(31, LOW);
}
if (analogMotorValue > 680) {
digitalWrite(32, HIGH);
}
else {
digitalWrite(32, LOW);
}
if (analogMotorValue > 850) {
digitalWrite(33, HIGH);
}
else {
digitalWrite(33, LOW);
}
if (analogMotorValue > 900) {
digitalWrite(34, HIGH);
}
else {
digitalWrite(34, LOW);
}
if (analogMotorValue > 1000) {
digitalWrite(35, HIGH);
}
else {
digitalWrite(35, LOW);
}
int outputMotorValue = map(analogMotorValue, 0, 1023, 0, 255);
if (digitalRead(36) == HIGH)
{
digitalWrite(45, HIGH);
digitalWrite(46, HIGH);
analogWrite(2, outputMotorValue);
analogWrite(4, outputMotorValue);
}
else
{
digitalWrite(45, LOW);
digitalWrite(46, LOW);
}
if (digitalRead(37) == HIGH)
{
digitalWrite(45, HIGH);
digitalWrite(46, HIGH);
analogWrite(3, outputMotorValue);
analogWrite(5, outputMotorValue);
}
else
{
digitalWrite(45, LOW);
digitalWrite(46, LOW);
}
if (digitalRead(38) == HIGH)
{
digitalWrite(45, HIGH);
digitalWrite(46, HIGH);
analogWrite(3, outputMotorValue);
analogWrite(4, outputMotorValue);
}
else
{
digitalWrite(45, LOW);
digitalWrite(46, LOW);
}
if (digitalRead(39) == HIGH)
{
digitalWrite(45, HIGH);
digitalWrite(46, HIGH);
analogWrite(2, outputMotorValue);
analogWrite(5, outputMotorValue);
}
else
{
digitalWrite(45, LOW);
digitalWrite(46, LOW);
}
if (digitalRead(40) == HIGH)
{
digitalWrite(48, HIGH);
analogWrite(9, outputMotorValue);
}
else
{
digitalWrite(48, LOW);
}
if (digitalRead(41) == HIGH)
{
digitalWrite(48, HIGH);
analogWrite(10, outputMotorValue);
}
else
{
digitalWrite(48, LOW);
}
if (digitalRead(42) == HIGH)
{
digitalWrite(47, HIGH);
analogWrite(6, outputMotorValue);
}
else
{
digitalWrite(47, LOW);
}
if (digitalRead(43) == HIGH)
{
digitalWrite(47, HIGH);
analogWrite(8, outputMotorValue);
}
else
{
digitalWrite(47, LOW);
}
int outputLightValue = map(analogLightValue, 0, 1023, 0, 255);
if (digitalRead(44) == HIGH)
{
digitalWrite(49, HIGH);
analogWrite(11, outputLightValue);
}
else
{
digitalWrite(49, LOW);
}
Serial.println(analogMotorValue);
Serial.println(analogLightValue);
btw, the serial readings are for my check on my laptop that the led's are working
i really need to get this working by monday, so could really need some advice
Sincerly,
Mikkel