Program on Arduino not working on Metro Board

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Bubby0626
 
Posts: 4
Joined: Mon Nov 22, 2021 5:53 pm

Program on Arduino not working on Metro Board

Post by Bubby0626 »

So I have been able to execute the program on the Arduino Uno below. Basically what the program does is turn on these LEDs/motors (I've been using LEDs) in an on/off pattern while being able to be controlled by a potentiometer. When I tried it on the Metro, I have no trouble compiling it, but it doesn't work at all. Would someone know why this is happening?

Code:

Code: Select all

#define LED_1_PIN 5
#define LED_2_PIN 6
#define LED_3_PIN 3
#define LED_4_PIN 9
#define LED_5_PIN 10
#define LED_6_PIN 11
const int potPin = A0;
int readValue = 0;
int writeValue;
int brightness = 0;
int motorState = 0;

int inputVal = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(potPin, INPUT);
  pinMode(LED_1_PIN, OUTPUT);
  pinMode(LED_2_PIN, OUTPUT);
  pinMode(LED_3_PIN, OUTPUT);
  pinMode(LED_4_PIN, OUTPUT);
  pinMode(LED_5_PIN, OUTPUT);
  pinMode(LED_6_PIN, OUTPUT);
}

void Potentiometer()
{
  readValue = analogRead(potPin);
  brightness = (255./1023.) * readValue;
  if (readValue > 0) 
  {
    vibrate();
  }
}

void vibrate()
{
  if(motorState == 0) {
    analogWrite(LED_1_PIN, brightness);
    analogWrite(LED_2_PIN, brightness);
    analogWrite(LED_3_PIN, brightness);
    analogWrite(LED_4_PIN, 0);
    analogWrite(LED_5_PIN, 0);
    analogWrite(LED_6_PIN, 0);

    motorState = motorState + 1;
  }
  else if(motorState == 1)
  {
    analogWrite(LED_1_PIN, 0);
    analogWrite(LED_2_PIN, 0);
    analogWrite(LED_3_PIN, 0);
    analogWrite(LED_4_PIN, brightness);
    analogWrite(LED_5_PIN, brightness);
    analogWrite(LED_6_PIN, brightness);

    motorState = 0;
  }
}

void loop()
{
  static unsigned long previousMillis = 0;
  if (millis() - previousMillis >= 500)
  {
    previousMillis += 500;
   Potentiometer();
  }
}

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Program on Arduino not working on Metro Board

Post by dastels »

Is your board setting correct?

Dave

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Program on Arduino not working on Metro Board

Post by mikeysklar »

Which model of the Adafruit Metro are you using?

User avatar
RogierWeekers
 
Posts: 6
Joined: Wed Jan 26, 2022 8:43 pm

Re: Program on Arduino not working on Metro Board

Post by RogierWeekers »

Maybe your calculation is wrong.
brightness = (255./1023.) * readValue;

analogRead gives a 12 bit (max 4095) value, and i would cast readValue to float before calculation to make sure it is not treated as an integer.

brightness = (255.0/4096.0) * (float) readvalue

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Program on Arduino not working on Metro Board

Post by dastels »

Good catch. The '328 has 10-bit ADCs. If you have a SAMD based Metro, the ADCs are 12 bit.

Dave

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Program on Arduino not working on Metro Board

Post by mikeysklar »

SAMD Metros would also be 3v3 based so possibly some wiring changes are needed. Still don't know which model of Metro is being used here.

Locked
Please be positive and constructive with your questions and comments.

Return to “Metro, Metro Express, and Grand Central Boards”