Hi,
So I am trying to use C code to control a motor based on the contents of a string(I believe that this is called "bare-metal" but I could be mistaken.)
When I upload my C code, it does not work. However, I wrote and uploaded an equivalent code using the Arduino language, and that works. I believe that the issue with that D2, D3, and D4 are not being written as HIGH in the C code, because I am not measuring any power from there. I checked and those pins' pinModes are OUTPUT, so that is not the issue.
For context, I am using an Arduino Nano and am using this datasheet. To control the motor, I am using a DRV8833. I added the Arduino code and C code below, as well as the pin connections. I appreciate any advice you could give me.
Pins
Nano|DRV8833|Motor|IR sensor
GND-----------------------------GND
5V --------------------------------VCC
5 ----------------------------------D0
2 --------IN1
7 --------IN2
VIN -----VCC
GND----GND
----------OUT1---------Red
-----------OUT2 --------Black
Arduino:
#include <Arduino.h>
void control(bool A, bool B, bool C, int act){//control motors
int true_act = digitalRead(5);//to read the IR sensor. 1 if positive, 0 otherwise
if((act == 2) || (true_act == act)){
if(A){
digitalWrite(2, HIGH);//turn motor on
Serial.print("A");
}else{
digitalWrite(2, LOW);//turn motor off
Serial.print("a");
}
if(B){
digitalWrite(3, HIGH);//turn motor on
Serial.print("B");
}else{
digitalWrite(3, LOW);//turn motor off
Serial.print("b");
}
if(C){
digitalWrite(4, HIGH);//turn motor on
Serial.print("C");
}else{
digitalWrite(4, LOW);//turn motor off
Serial.print("c");
}
delay(5000);
Serial.println("");
}
}
void setup(){
Serial.begin(57600);
delay(100);
Serial.println("Starting setup()");
//pinModes
pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT);//for D2, D3, D4 being output
pinMode(5, INPUT);//for D5 being input
pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT);
digitalWrite(6, LOW); digitalWrite(7, LOW); digitalWrite(8, LOW);
char* code = "2abc";
int L = 4;//number of characters in code
int activation = -1;//if activation = 2, then do it regardless of the IR sensor. If 0 or 1, do it when the IR sensor matches
bool motorA = false; bool motorB = false; bool motorC = false;
//code processing
Serial.println("Processing code");
for(int i = 0; i < L; i++){
Serial.print("Character ");
Serial.print(" is ");
Serial.print(" is: ");
Serial.println(*(code + i));
switch(*(code + i)){
case 'a':
motorA = true;
break;
case 'b':
motorB = true;
break;
case 'c':
motorC = true;
break;
case '2':
if(activation != -1){
control(motorA, motorB, motorC, activation);
}
activation = 2;
motorA = false; motorB = false; motorC = false;
break;
case '1':
if(activation != -1){
control(motorA, motorB, motorC, activation);
}
activation = 1;
motorA = false; motorB = false; motorC = false;
break;
case '0':
if(activation != -1){
control(motorA, motorB, motorC, activation);
}
activation = 0;
motorA = false; motorB = false; motorC = false;
break;
}
}
if(activation != -1){
control(motorA, motorB, motorC, activation);
}
activation = -1;
motorA = false; motorB = false; motorC = false;
}
void loop(){
setup(); //for debugging purposes
}
C code:
#include <Arduino.h>
#include <stdbool.h>
#define F_CPU 20000000L
void control(bool A, bool B, bool C, int act){//control motors
int true_act = ((PIND & (1 << PD5)) > 0);//to read the IR sensor. 1 if positive, 0 otherwise
if((act == 2) || (true_act == act)){
if(A){
PORTD |= (1 << PD2);//turn motor on
}else{
PORTD &= ~(1 << PD2);//turn motor off
}
if(B){
PORTD |= (1 << PD3);//turn motor on
}else{
PORTD &= ~(1 << PD3);//turn motor off
}
if(C){
PORTD |= (1 << PD4);//turn motor on
}else{
PORTD &= ~(1 << PD4);//turn motor off
}
delay(5000);
}
}
//1 is output, 0 is input
//DATASHEET: https://docs.arduino.cc/resources/datasheets/A000005-datasheet.pdf
void setup(){
//pinModes
DDRD |= ((1 << PD2) | (1 << PD3) | (1 << PD4));//mask for D2, D3, D4 being output
DDRD &= ~(1 << PD5);//mask for D5 being input
DDRD |= ((1 << PD6) | (1 << PD7));//mask for D6, D7, D8 being output. We will use these as ground for the motors
DDRB |= (1 << PB0);//pin D8 is the start of the B register
PORTD &= ~(1 << PD6); PORTD &= ~(1 << PD7); PORTB &= ~(1 << PB0);
char* code = "2abc";
int L = 4;//number of characters in code
int activation = -1;//if activation = 2, then do it regardless of the IR sensor. If 0 or 1, do it when the IR sensor matches
bool motorA = false; bool motorB = false; bool motorC = false;
//code processing
for(int i = 0; i < L; i++){
switch(*(code + i)){
case 'a':
motorA = true;
break;
case 'b':
motorB = true;
break;
case 'c':
motorC = true;
break;
case '2':
if(activation != -1){
control(motorA, motorB, motorC, activation);
}
activation = 2;
motorA = false; motorB = false; motorC = false;
break;
case '1':
if(activation != -1){
control(motorA, motorB, motorC, activation);
}
activation = 1;
motorA = false; motorB = false; motorC = false;
break;
case '0':
if(activation != -1){
control(motorA, motorB, motorC, activation);
}
activation = 0;
motorA = false; motorB = false; motorC = false;
break;
}
}
}
/*
void loop(){
setup(); //for debugging purposes
}
*/